我认为一旦连接打开,请求和响应就已经发生,所以此时发送标头为时已晚。此外,标题必须以空行结尾,您将其省略。
根据演示,您甚至不必在客户端或服务器中设置标头——ruby 模块自动处理服务器端的标头,html5 自动处理客户端的标头。我认为这应该有效:
需要“em-websocket-server”
类 EchoServer < EM::WebSocket::Server
def on_connect EM::WebSocket::Log.debug "Connected" 表示“我感觉到了连接”。结尾
def on_receive msg puts "RECEIVED: #{msg}" send_message msg end
结尾
EM.run do myhost = "0.0.0.0" myport = 8000 puts "Starting WebSocket server. Listening on port #{myport}..." EM.start_server myhost, myport, EchoServer end
.html 文件:
<!DOCTYPE html> <html> <head><title>Test</title>
<script type="text/javascript">
var myWebSocket = new WebSocket("ws://localhost:8000");
myWebSocket.onopen = function(evt) {
console.log("Connection open. Sending message...");
myWebSocket.send("Hello WebSockets!"); };
myWebSocket.onmessage = function(evt) {
console.log(evt.data);
myWebSocket.close(); };
myWebSocket.onclose = function(evt) {
console.log("Connection closed."); };
myWebSocket.onerror = function(err) {
alert(err.name + " => " + err.message); } </script>
</head> <body> <div>Hello</div> </body> </html>
它在 Safari 5.1.9(这是一个较旧的浏览器)中确实有效:我在服务器和客户端上都看到了预期的输出。但是,该代码在 Firefox 21 中不起作用:我收到错误消息...
Firefox can't establish a connection to the server at ws://localhost:8000/.
var myWebSocket = new WebSocket("ws://localhost:8000");
我注意到在 Firebug 和 Safari 开发者工具中,服务器不发送 Sec-WebSocket-Accept 标头:
Response Headers
Connection Upgrade
Upgrade WebSocket
WebSocket-Location ws://localhost:8000/
WebSocket-Origin null
Request Headers
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Cache-Control no-cache
Connection keep-alive, Upgrade
DNT 1
Host localhost:8000
Origin null
Pragma no-cache
Sec-WebSocket-Key r9xT+ywe533EHF09wxelkg==
Sec-WebSocket-Version 13
Upgrade websocket
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:21.0) Gecko/20100101 Firefox/21.0
我尝试过的任何方法都无法使代码在 Firefox 21.0 中运行。为了检查 Firefox 21.0 是否支持 websockets,我去了:
http://www.websocket.org/echo.html
它说我的浏览器确实支持 websockets。
你有什么理由必须使用 em-websocket-server 模块吗?在 github 上对该模块的最后一次修改是三年前。每当您require rubygems
在 ruby 代码中看到时,都会提醒您该代码已过时。我尝试了较新的 em-websocket 模块,并且能够在 Firefox 21.0 和 Safari 5.1.9 上使用 websocket 成功地来回传输数据:
需要'em-websocket'
我的主机=“0.0.0.0”我的端口=8000
EM.run { puts "监听端口 #{myport}..."
EM::WebSocket.run(:host => myhost, :port => myport, :debug => false) 做 |ws|
ws.onopen do |handshake|
path = handshake.path
query_str = handshake.query
origin = handshake.origin
puts "WebSocket opened:"
puts "\t path \t\t -> #{path}"
puts "\t query_str \t -> #{query_str}"
puts "\t origin \t -> #{origin}"
end
ws.onmessage { |msg|
ws.send "Pong: #{msg}"
}
ws.onclose {
puts "WebSocket closed"
}
ws.onerror { |e|
puts "Error: #{e.message}"
}
结尾 }
相同的客户端代码。现在响应标头包括 Sec-WebSocket-Accept:
Response Headers
Connection Upgrade
Sec-WebSocket-Accept LyIm6d+kAAqkcTR744tVK9HMepY=
Upgrade websocket
Request Headers
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Cache-Control no-cache
Connection keep-alive, Upgrade
DNT 1
Host localhost:8000
Origin null
Pragma no-cache
Sec-WebSocket-Key pbK8lFHQAF+arl9tFvHn/Q==
Sec-WebSocket-Version 13
Upgrade websocket
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:21.0) Gecko/20100101 Firefox/21.0
在您的代码中,我认为您没有设置任何标题。相反,您只是来回发送恰好包含看起来像标题的字符的消息。显然,您的浏览器需要在响应中包含 Sec-WebSocket-Accept 标头才能允许连接,并且当 em-websocket-server 模块未能在响应中设置该标头时,您的浏览器会拒绝连接。
em-websockets-server 的相关源代码如下所示:
module EM
module WebSocket
module Protocol
module Version76
# generate protocol 76 compatible response headers
def response
response = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n"
response << "Upgrade: WebSocket\r\n"
response << "Connection: Upgrade\r\n"
response << "Sec-WebSocket-Origin: #{origin}\r\n"
response << "Sec-WebSocket-Location: #{scheme}://#{host}#{path}\r\n"
if protocol
response << "Sec-WebSocket-Protocol: #{protocol}\r\n"
end
response << "\r\n"
response << Digest::MD5.digest(keyset)
response
end
如您所见,它没有设置 Sec-WebSocket-Accept 标头。该代码位于名为 Version76 的模块中,在 google 中搜索 websockets 版本 76 会产生一个过时的协议(其中包含请求和响应的示例):
https://datatracker.ietf.org/doc/html/draft-hixie-thewebsocketprotocol-76
这是当前的 websockets 协议(其中还包含请求和响应的示例):
https://www.rfc-editor.org/rfc/rfc6455
结论:em-websockets-server 已过时。