Sometimes, when I reload the page, it appears an error:
WebSocket connection to 'ws://127.0.0.1:45/' failed: Connection closed before receiving a handshake response
I'm using Alchemy Websocket library for C#.
And I can only fix deleting the cookies and local storage. How can I fix that?
Asked
Active
Viewed 3,898 times
0

user3211337
- 75
- 4
- 11
-
Does your page has code for closing connection to websocket when onUnload event occured? Your error looks that the websocket server is not ready to be connected. – Fumu 7 Nov 04 '14 at 06:20
-
Actually no, is that a websocket event? – user3211337 Nov 04 '14 at 06:24
-
'onUnload' event is client-side event which is raised when web page is closed or reloaded. The usage of websocket is 'connect to websocket server', 'receive data from websocket' and 'close connection'. If you reload , usage sequence may not be finished, and the websocket server may left in status in process. – Fumu 7 Nov 04 '14 at 06:35
-
Can you give me an example of "onUnload" closing the websocket? – user3211337 Nov 04 '14 at 06:42
-
It may be related with this: http://stackoverflow.com/questions/25713445/why-firefox-sometimes-opens-two-different-sockets-on-server-side – vtortola Nov 04 '14 at 10:13
-
Can you connect from another tab or with another browser after you get that error? It would be great if you show some code. – vtortola Nov 04 '14 at 10:14
-
I'm using Alchemy WebSockets, while the browser shows this error: `WebSocket connection to 'ws://127.0.0.1:45/' failed: Connection closed before receiving a handshake response`, my C# server shows `System.InvalidOperationException: Sequence contains no elements at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source) at H.Sockets.WebSocketsServer.OnDisconnect(UserContext context)`, so, the problem is not on the server. – user3211337 Nov 04 '14 at 15:46
-
My C# code is this: http://pastebin.com/FSKRHj91 – user3211337 Nov 05 '14 at 05:26
1 Answers
0
Ok, define your socket as a "global variable", presumably at the top of the document. Now this socket variable is available in the whole document. When a user clicks the x on the tab/browser window (or reloads the page), you want the window.onunload event to fire, so define that somewhere - and call the close method of your global socket variable.
var _socket = new WebSocket("ws://127.0.0.1/example");
window.onunload = function () {
_socket.close();
}

Amund Midtskog
- 209
- 1
- 8
-
Well...then I believe the error must be somewhere else. Perhaps Alchemy Websocket does something the client side of the websocket does not expect. When it comes to websockets and .net, the only library I found truly stable was SignalR - it's simply magnificent. – Amund Midtskog Nov 04 '14 at 18:23
-
Just curious, does this error happen only when you refresh the page? And another thing, you might want to try swapping out "window.onunload" with "window.onbeforeunload". I'm not quite familiar with the last one mentioned, but it might be worth a try (as the whole context of the page is untouched at that point). – Amund Midtskog Nov 04 '14 at 18:38
-
Yes, if I refresh the page or close(and then return) the error happens. I tried both them, none worked for me. Gonna try SignalR. – user3211337 Nov 04 '14 at 18:48
-
Cool, this might get you going: http://stackoverflow.com/questions/21512587/signalr-2-0-2-creating-persistentconnection/25304790#25304790 – Amund Midtskog Nov 04 '14 at 18:52
-