无法在Firefox上建立Websocket安全连接

时间:2014-05-21 06:07:59

标签: javascript python firefox websocket tornado

我被Firefox搞砸了。我无法使Websocket工作。我使用Tornado Websocket并通过以下代码初始化它:

app = Application([(r'/mypath/ws', WSHandler)])
http_server = HTTPServer(app, ssl_options={
                "certfile": "~/certs/websocket.crt",
                "keyfile": "~/certs/websocket.key"
            })
http_server.listen("443")

我在Javascript端初始化它是这样的:

var WS = new WebSocket("wss://websocket.localhost/mypath/ws");

此代码在Chrome上正常运行,同时我自己创建了证书并在HTTPS下运行该页面。但Firefox一直这么说:

Firefox can't establish a connection to the server at wss://websocket.localhost/mypath/ws.

我谷歌并发现了太多的想法,但没有任何一个为我工作:(

任何帮助将不胜感激。

6 个答案:

答案 0 :(得分:6)

如果它是自签名证书,则浏览器不会显示接受证书的对话框(如果它仅用于websocket)。您必须首先访问同一服务器上的普通页面才能查看并接受证书警告,然后您可以创建安全的websocket。

答案 1 :(得分:1)

我通过ProxyPass解决了我的问题。我使用Tornado创建了一个非安全的Websocket服务器,并在特定端口(如3232)上运行它:

app = Application([(r'/ws/', WSHandler)])
ws_server = HTTPServer(app)
ws_server.listen("3232")

然后我在Apache conf中写了一个proxypass并使用mod_proxy_wstunnel:

ProxyPass /ws/ ws://127.0.0.1:3232/ws/
ProxyPassReverse /ws/ ws://127.0.0.1:3232/ws/

我在前端创建Websocket客户端,如下所示:

var WS = new WebSocket("wss://websocket.localhost:81/ws/")

在这种情况下,我可以使用https在安全连接上创建连接,并且我的端口为81,并且我的proxypass将任何Websocket请求重定向到本地侦听的端口3232.这不是一个确切的解决方案,主要是一种解决方法。但它对我来说很好。

答案 2 :(得分:1)

尝试在Firefox中打开此网址https://websocket.localhost/mypath/ws并首先接受证书。

答案 3 :(得分:1)

如果它是自签名证书,则仅用于websocket的浏览器将不会显示对话框以接受证书。
您必须首先访问所请求的URL以查看并接受证书警告,然后才能创建安全的Websocket。

例如,如果您的websocket网址为:
wss:// localhost:44300 / OpenWebSocket
然后访问:
https://localhost:44300/OpenWebSocket
并接受证书警告

答案 4 :(得分:0)

我已经解决了这个问题,在Firefox的高级偏好设置中添加了证书异常。

答案 5 :(得分:0)

我把头发拉过这个头发一段时间了。我根据不同的网络浏览器得到了各种神秘的错误消息,这些消息听起来都像证书异常。我已经在Firefox和Chrome中做过例外,

原来我的Javascript中的子协议字符串中有拼写错误!

纠正子协议字符串使一切变得更好。 有关WebSockets和使用子协议的更多信息:https://developer.mozilla.org/en-US/docs/Web/API/WebSocket