问题
From Using the JavaScript Client:
Notice the lack of a protocol prefix on the server URL. Do not add http:// or ws:// to the URL that you pass to the new dispatcher. WebSocketRails will choose the best available transport for you and prepend the correct prefix automatically.
I'm using WebSocketRails for the first time. I'm getting this error in the client:
WebSocket connection to 'ws://localhost:3000/websocket' failed: Connection closed before receiving a handshake response
If I navigate my browser to http://localhost:3000/websocket
, I get the messages that I am expecting. If I navigate to the ws
URL, I get ERR_DISALLOWED_URL_SCHEME
. So it appears that the server is producing the messages, but the client is trying the wrong protocol.
How do I force the client to use the http
protocol rather than ws
, or force the server to broadcast it with ws
rather than http
?
回答1:
Finally found the answer by digging into the source code. The second parameter of the constructor is called use_websockets
and defaults to true
. Setting it to false
apparently forces it to use http
instead of ws
.
var dispatcher = new WebSocketRails('localhost:3000/websocket',false);
来源:https://stackoverflow.com/questions/35484589/websocket-rails-server-emits-http-protocol-client-expects-ws-protocol