繁体   English   中英

使用em-websocket gem无法将连接更改为'ws://'RUBY

[英]Using em-websocket gem can't change connection to 'ws://' RUBY

我是Ruby的新手。

我正在尝试使用WebSocket连接。 因此,我使用em-websocket gem。 我还使用Thin Web服务器。 我做了一切,就像例子告诉我的那样。 所以请帮帮我。

但是服务器不断返回我:

HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
X-UA-Compatible: IE=Edge
ETag: "7592ed842deb971babc6640ff75207fb"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: f2d12b00aa2eb202b24c309ce0570da0
X-Runtime: 0.008190
Connection: close
Server: thin 1.5.0 codename Knife

客户要求:

GET /home/webSocket HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:3000
Origin: http://localhost:3000
Pragma: no-cache
Cache-Control: no-cache
Sec-WebSocket-Key: i6Efmjpxmz2GOFpjduxoyA==
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: x-webkit-deflate-frame

客户端代码:

$(document).ready(
    function() {
        ws = new WebSocket("ws://localhost:3000/home/webSocket");
        ws.onopen = function() {
          alert('open');
          ws.send("hello server");
        };
        ws.onmessage = function(evt) { alert(evt.data); };
        ws.onclose = function() { alert('close'); };

    });

这是服务器端代码:

def webSocket
    require "rubygems"
    require "em-websocket"

    EventMachine.run {
     EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8000) do |ws|
     ws.onopen { |handshake|
       puts "WebSocket opened #{{
          :path => handshake.path,
          :query => handshake.query,
          :origin => handshake.origin,
        }}"

        ws.send "Hello Client!"
      }
      ws.onmessage { |msg|
        ws.send "Pong: #{msg}"
      }
      ws.onclose {
        puts "WebSocket closed"
      }
      ws.onerror { |e|
        puts "Error: #{e.message}"
      }
      end
    }
  end

EventMachine正在侦听端口8000: EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8000)但您尝试连接到端口3000: ws = new WebSocket("ws://localhost:3000/home/webSocket");

更改它以连接到端口8000:

ws = new WebSocket("ws://localhost:8000/home/webSocket");

尽管不需要额外的路径,除非您特别想将/home/webSocket给EventMachine。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM