admin管理员组文章数量:1313746
I wrote the simple chat application using plain nodejs and Websocket module. Everything works fine, but if the page refreshed connection state changes to closed
and description says that Remote peer is going away
. I know that it standard code of RFC 6455, but why is my connection not updating with the page so the chat continue to work. How to handle page refreshing on client side?
I wrote the simple chat application using plain nodejs and Websocket module. Everything works fine, but if the page refreshed connection state changes to closed
and description says that Remote peer is going away
. I know that it standard code of RFC 6455, but why is my connection not updating with the page so the chat continue to work. How to handle page refreshing on client side?
1 Answer
Reset to default 7Refreshing a page in a browser, closes and releases ALL resources associated with the original page and then loads a fresh copy of the page and runs any Javascript in the page again.
So, if your page initialization code includes opening a webSocket to your server, then that webSocket will be closed when the page is reloaded.
This is what will happen upon the initial page load and then a refresh:
- User requests your page
- Browser loads HTML for that page
- Browser runs Javascript in that page
- That Javascript creates a webSocket connection to your server
- User presses refresh
- Browser closes down all resources associated with the original page, including closes the webSocket connection.
- Browser reloads the original HTML for the page
- Browser runs Javascript in that page again
- That Javascript creates a new webSocket connection to your server
How to handle page refreshing on client side?
After refresh, the browser will run the Javascript in your page and that Javascript should just open a new webSocket connection and should establish a new chat connection.
FYI, that code you referenced has at least one bug. For example, the server-side code that removes a client from the server-side index does not work properly. It remembers the index for when the client connection was added to the array and assumes that the index never changes, but that's just wrong. As clients are added/removed from the array and they connect/disconnect that index can change. You're going to have to fix/debug that code if you use it.
While it can be made to work using an Array
, I would probably use a Set
myself and then it would be easier to remove an item too.
本文标签: javascriptWhy is my WebSocket connection closing after I refresh the pageStack Overflow
版权声明:本文标题:javascript - Why is my WebSocket connection closing after I refresh the page? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741957459a2407068.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论