简体   繁体   English

为什么 Firefox 不等待 WebSocket 连接?

[英]Why doesn't Firefox wait for WebSocket connection?

I'm writing an application that first tries to open a WebSocket connection (to make sure no others are open; address collision checking) before firing off a custom protocol that will launch a one-time WebSocket server with the address that the browser tells it.我正在编写一个应用程序,它首先尝试打开 WebSocket 连接(以确保没有其他连接处于打开状态;地址冲突检查),然后再触发自定义协议,该协议将使用浏览器告诉它的地址启动一次性 WebSocket 服务器. All communication is done over localhost and some arbitrary port number, say 3000. I'm not doing anything special, just attempting to open a WebSocket:所有通信都是通过本地主机和一些任意端口号完成的,比如 3000。我没有做任何特别的事情,只是尝试打开一个 WebSocket:

var socket = new WebSocket("ws://localhost:3000/MyApp/");
socket.onclose = function(e) { console.error(e); }

When testing in Chrome, the WebSocket will actually stay in the CONNECTING state for a little bit, which is ideal, since it gives us some time to actually launch the app through the custom protocol.在 Chrome 中进行测试时,WebSocket 实际上会保持在 CONNECTING 状态一段时间,这是理想的,因为它给了我们一些时间通过自定义协议实际启动应用程序。 But in Firefox, the WebSocket immediately closes with code 1006 and I can't figure out why.但是在 Firefox 中,WebSocket立即以代码 1006 关闭,我不知道为什么。

I've tried changing the about:config network.websocket.timeout.open setting to be 1000 (from 20), but that doesn't help.我已经尝试将 about:config network.websocket.timeout.open 设置更改为 1000(从 20 开始),但这无济于事。 I've also found this related post: Websockets - chrome and firefox differences?我还发现了这个相关的帖子: Websockets - chrome 和 firefox 的区别? . . That hasn't lead me to finding an answer, either.这也没有让我找到答案。

What am I missing?我错过了什么?

Update 11/16/21 21 年 16 月 11 日更新
I'm using the Dev Tools in Chrome and FF to check out the requests.我正在使用 Chrome 和 FF 中的开发工具来检查请求。 The weird thing is that Chrome is actually sending a request header as you'd expect, but in FF, the request is completely empty (0 Bytes).奇怪的是,Chrome 实际上按照您的预期发送请求标头,但在 FF 中,请求完全为空(0 字节)。 Maybe this is a problem with FF not supporting debugging native WebSockets (no wrapper libraries in use)?也许这是 FF 不支持调试本机 WebSockets(没有使用包装库)的问题? Is there some FF setting that nixes the request?是否有一些 FF 设置可以拒绝请求? But even more confusing is that the browser would hit the close event without ever hitting the open event.但更令人困惑的是,浏览器会在未命中 open 事件的情况下触发 close 事件。

Update 11/17/21 21 年 17 月 11 日更新
I realize that maybe this has something to do with launching a Custom Protocol Handler?我意识到这可能与启动自定义协议处理程序有关? I noticed that it will wait a second to try and connect to a web server if no CPH is launched, but then when I do launch a CPH, that is when it immediately closes the WebSocket.我注意到,它会等待第二次尝试连接到Web服务器,如果没有CPH推出,但后来当我做启动CPH,就是当它立即关闭WebSocket的。 The CPH is launched via a link targeting "_parent". CPH 是通过指向“_parent”的链接启动的。

It looks like the custom protocol is causing FF to stop trying to connect early.看起来自定义协议导致 FF 停止尝试提前连接。 I created an anchor element, <a>, in the JS code and called "click()" on it after constructing it.我在 JS 代码中创建了一个锚元素 <a>,并在构造它后在它上面调用了“click()”。 No matter what target I gave it (eg _self), it would cause the connection attempt to stop.无论我给它什么目标(例如_self),它都会导致连接尝试停止。

So long story short, initiating a link, whether it be a a.click, window.open, or location.replace, will cause Firefox to nix any currently polling WebSockets!长话短说,启动一个链接,无论是 a.click、window.open 还是 location.replace,都会导致 Firefox 拒绝任何当前轮询的 WebSockets!

The workaround is to just use an iframe to launch the custom protocol.解决方法是仅使用 iframe 来启动自定义协议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM