websocket-rails: Server emits http:// protocol; client expects ws:// protocol

我怕爱的太早我们不能终老 提交于 2019-12-12 03:15:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!