-->

problems with socket.io on IE8 and 9

2020-07-26 11:21发布

问题:

Socket.io works perfectly on every platform except IE8 and 9, which is a client requirement. could you guys help with this issue?

i've been reading all similar issues but most of the solutions i found till now can't solve this problem on IE8.

here's the structure:

SERVER-SIDE

var ip = 'xxx.xxx.xxx.xxx';
var ipPort = '8081';

var app = require('http').createServer(handler);
var io = require('socket.io')(app);
var fs = require('fs')

app.listen(ipPort, function() {
//console.log('Listening at: http://'+ ip +':'+ ipPort +'');
});

//io.set('transports', ['websocket','flashsocket', 'htmlfile', 'xhr-polling', 'jsonp-polling']);

function handler (req, res) {
 fs.readFile(__dirname + '/test.html',
  function (err, data) {
    if (err) {
    res.writeHead(500);
    return res.end('Error loading index.html');
    }

  res.writeHead(200);
  res.end(data);
 });
}

CLIENT-SIDE

<html>
<body>
</body>
   <script src="/socket.io/socket.io.js"></script>
   <script>
    alert("hi");

   //var socket = io.connect('xxx.xxx.xxx.xxx:8081',{transports:'jsonp-polling');
   var socket = io.connect(''xxx.xxx.xxx.xxx:8081',{secure: true});

   socket.on('news', function (data) {
    alert("hello data:"+data[0]);
   socket.emit('my other event', { my: data });
   });  

   socket.on('connect', function () {
     alert("hello socket");
   });

   </script>
</html>

i get no errors at all, just no response.. i placed an alert() on socket.on('news') and doesn't get any feedback, BUT THEN i added socket.on('connect') and placed the alert() inside and it pops up!!

hope there's someone out there that pass through this issue before and solve it. I'm on win7 and node(v0.10.30) socket.io(v1.0.6) thanks very much for all the help.

回答1:

the one solution i found to this problem was to install add express to the server-side; and all miraculously works! (npm install express)

wondering if we all need to install express in order to avoid these issues.. mmmhh!

EDIT: apparently there were two other issues inside the code in the client-side:

i added <!DOCTYPE html> at the header of the page, and moved the socket.io external script link and code to the end of the tag. and all problems were gone.



回答2:

I noticed problems with using express and socket.io together (possibly; not 100% sure of the real cause) with IE8, if you reload a webpage with a socket on it, sometimes the new socket won't funtion properly

It would manifest by the socket server losing packets, and occasionally with express not loading web pages either; restarting the server was needed to fix this. It was easy to observe just be rapidly reloading a web page with a socket.io client on it which tried to send messages back and forth (our testing framework does this).

I suspect express since socket.io seems to hook into express, and loading the webpage itself seemed to have issues occasionally.

While investigating I noticed sockets were not disconnecting automatically on page unload and would instead seem to timeout on the server side to disconnect.

Explicitly disconnecting this way seems to fix it (translate to your favorite framework as needed):

$(window).on("beforeunload", function() {
    socket.disconnect();
});