"websocket was aborted during page load" in Firefox for Socket.io

Error: The connection to <websocket> was interrupted while the page was loading. Source File: localhost/socket.io/node_modules/socket.io-client/dist/socket.io.js Line: 2371 

I am new to socket.io and I tried to search but did not receive a response.

Websocket is interrupted when I refresh a page in Firefox. Therefore, the server side is waiting for client authorization.

Here is the code:

 server.js--> var app = require('http').createServer(handler) , io = require('socket.io').listen(app) , fs = require('fs') app.listen(8080); function handler (req, res) { fs.readFile(__dirname + '/index.html', function (err, data) { if (err) { res.writeHead(500); return res.end('Error loading index.html'); } res.writeHead(200); res.end(data); }); } io.sockets.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { //alert(JSON.stringify(data)); console.log(data); }); }); 

index.html

 <script src="node_modules/socket.io-client/dist/socket.io.js"></script> <script> var socket = io.connect('http://localhost:8080'); socket.on('news', function (data) { alert(JSON.stringify(data)); console.log(data); socket.emit('my next event', { my: 'data' }); }); </script> 
+43
websocket
Jan 03 '13 at 14:03
source share
4 answers

This is because you are not closing your open websocket.

This code will fix this error:

 $(window).on('beforeunload', function(){ socket.close(); }); 
+40
May 21 '14 at 13:15
source share

This seems like an open bug in Firefox (as of 2015-03-29):

https://bugzilla.mozilla.org/show_bug.cgi?id=712329

The workaround (for now) is to call close () on websocket on beforeunload, as Alexander noted.

Update 2016-04 . According to Bugzilla, this will be fixed in Firefox 48

+12
Mar 29 '15 at 13:13
source share

I just ran the Socket.IO tutorials and I ran into this exact problem. I tried the published solutions, but they didn't seem to work at all.

After some screams and some screams and some rubber deviations, I finally realized what the problem was. The problem is that it tries to connect to the socket before the socket variables have been correctly initialized. Javascript boo boo # 1.

If you configure your file to include jQuery, then wrap your functions as follows:

  <script src="/socket.io/socket.io.js"></script> <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script> <script> $(function(){ var socket = io.connect('http://localhost:8080'); socket.on('news', function (data) { alert(JSON.stringify(data)); console.log(data); socket.emit('my next event', { my: 'data' }); }); }); </script> 

You will have much more success.

+2
Feb 11 '15 at 9:56
source share

What effect does this have on your application? I suppose it's just not great to see an error in the console.

The problem is that you see Firefox loggin this error, and you can not do anything about it. It is not possible to fix this error with the try...catch or through websocket.onerror / websocket.onclose .

See: How to intercept a connection to WebSocket?

on this topic:

0
Jan 03 '13 at 17:58
source share



All Articles