gpt4 book ai didi

javascript - AngularJS : WebSocket is closed before the connection is established in Google Chrome

转载 作者:行者123 更新时间:2023-12-03 08:03:28 25 4
gpt4 key购买 nike

在 Google Chrome 中,我无法与我的服务器建立套接字连接。有时在控制台中得到这个(黄色):

WebSocket connection to 'ws://www.mywebsite.com/socket.io/?EIO=3&transport=websocket&sid=u_g7zfNFByPmMnnfAAAH' failed: WebSocket is closed before the connection is established.

在此之后(但并非总是如此):

POST http://www.mywebsite.com/socket.io/?EIO=3&transport=polling&t=1451326926027-5&sid=8MlGZTYdQ256ECoAAAAJ 400 (Bad Request)

得到这个回应:

{"code":1,"message":"Session ID unknown"}

我必须刷新多次才能使页面建立连接。在 Firefox 中,相同的代码在大多数情况下都可以正常工作,但有时我会遇到这个问题。

Firefox can't establish a connection to the server at ws://www.mywebsite.com/socket.io/?EIO=3&transport=websocket&sid=tDQpo6ynbIik6FUUAAAS.

instance = new WebSocket(uri);

socket.io.js (line 5310)

The connection to ws://www.mywebsite.com/socket.io/?EIO=3&transport=websocket&sid=tDQpo6ynbIik6FUUAAAS was interrupted while the page was loading.

instance = new WebSocket(uri);

如果我在本地主机中运行我的服务器,代码在两个浏览器中都可以正常工作,没有任何问题。我的服务器位于在 nginx 代理下运行的 node.js 中。我通过以下配置配置了 nginx 以支持套接字:-

    proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

如何确保我的 websocket 能够在不刷新页面的情况下进行连接?我使用 btford 以 Angular 实现套接字.

最佳答案

我可以知道您的网站托管服务器

如果它具有动态 session 或动态 token ,例如在每次与服务器的调用中都会发生更改,那么您可能会遇到此问题,捕获您的动态 session 并将其附加到 URL 中或将其添加到 header 中,这可能会有所帮助

例如

WS://www.mywebsite.com;jsessionid=rbgeger377377

更新答案

我对ubuntu服务器不太了解,

但是在 Web 套接字中,最初会发生握手,因为 session 将在所有请求和响应过程中创建和维护

如果您的 session 在几分钟内过期,下一个 session 可能会取代您仍在执行请求的旧 session

如果你有两个并发服务,Javascript 就是异步的,

一旦 session 更改,当发起下一个请求时,该 session 将在您的请求中不可用,但您的服务器认为它已经更改,这就是您收到错误的原因,这么想,

我不知道 firefox 和 chrome 背后的确切原因,可能是如果你禁用了 chrome 中的缓存,下一个线程很可能会失败

在Javascript中,一个很好的功能是双向绑定(bind),一旦你在响应头中获得了新的 session ,如果你将它映射到url,它会自动绑定(bind)到下一个请求,使用它你可以避免这样的问题

通过在设定的时间间隔内清除 cookie 和缓存可能会帮助您调试问题

关于javascript - AngularJS : WebSocket is closed before the connection is established in Google Chrome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34498869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com