websocket-rails:服务器发出 http://协议;客户期望 ws://协议 [英] websocket-rails: Server emits http:// protocol; client expects ws:// protocol

查看:46
本文介绍了websocket-rails:服务器发出 http://协议;客户期望 ws://协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自使用 JavaScript 客户端:

请注意服务器 URL 上缺少协议前缀.不要将 http://或 ws://添加到您传递给新调度程序的 URL.WebSocketRails 将为您选择最佳的可用传输并自动添加正确的前缀.

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.

我是第一次使用 WebSocketRails.我在客户端收到此错误:

I'm using WebSocketRails for the first time. I'm getting this error in the client:

WebSocket 连接到 'ws://localhost:3000/websocket' 失败:在收到握手响应之前连接已关闭

WebSocket connection to 'ws://localhost:3000/websocket' failed: Connection closed before receiving a handshake response

如果我将浏览器导航到 http://localhost:3000/websocket,我会收到我期望的消息.如果我导航到 ws URL,我会得到 ERR_DISALLOWED_URL_SCHEME.所以看起来服务器正在生成消息,但客户端正在尝试错误的协议.

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.

如何强制客户端使用 http 协议而不是 ws,或者强制服务器使用 ws 而不是广播它http?

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?

推荐答案

终于通过挖掘源代码找到了答案.构造函数的第二个参数称为use_websockets,默认为true.将其设置为 false 显然会强制它使用 http 而不是 ws.

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);

这篇关于websocket-rails:服务器发出 http://协议;客户期望 ws://协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆