请问:socket.io如何在ie9以下的浏览器使用呢?
发布于 2个月前 作者 pauky 606 次浏览 来自 问答

想做推送的功能,看到下面这个简易聊天室,运行了下,在低版本ie下用不了。请问如何解决呢?* 前台代码:

<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script src="/socket.io/socket.io.js"></script>
    <script>
        $(function(){
            // var iosocket = io.connect();
            if (/Firefox\/\s/.test(navigator.userAgent)){
                var iosocket = io.connect('localhost:5000',{transports:['xhr-polling']}); 
            } 
            else if (/MSIE (\d+.\d+);/.test(navigator.userAgent)){
                var iosocket = io.connect('localhost:5000',{transports:['jsonp-polling']}); 
            } 
            else {
                var iosocket = io.connect('localhost:5000'); 
            }
            iosocket.on('connect', function () {
                $('#incomingChatMessages').append($('<li>Connected</li>'));
  
                iosocket.on('message', function(message) {
                    $('#incomingChatMessages').append($('<li></li>').text(message));
                });
                iosocket.on('disconnect', function() {
                    $('#incomingChatMessages').append('<li>Disconnected</li>');
                });
            });
  
            $('#outgoingChatMessage').keypress(function(event) {
                if(event.which == 13) {
                    event.preventDefault();
                    iosocket.send($('#outgoingChatMessage').val());
                    $('#incomingChatMessages').append($('<li></li>').text($('#outgoingChatMessage').val()));
                    $('#outgoingChatMessage').val('');
                }
            });
        });
    </script>
</head>
<body>
Incoming Chat:&nbsp;<ul id="incomingChatMessages"></ul>
<br />
<input type="text" id="outgoingChatMessage">
</body>
</html>

服务器代码:

var fs = require('fs'),
    http = require('http'),
    socketio = require('socket.io');
      
var server = http.createServer(function(req, res) {
    res.writeHead(200, { 'Content-type': 'text/html'});
    res.end(fs.readFileSync(__dirname + '/index.html'));
}).listen(5000, function() {
    console.log('Listening at: http://localhost:5000');
});
socketio.listen(server).on('connection', function (socket) {
    socket.on('message', function (msg) {
        console.log('Message Received: ', msg);
        socket.broadcast.emit('message', msg);
    });
});
7 回复

socket.io可以优雅降低的,IE9以下没问题。 我没看你的代码,但我觉得你要找你自己代码的原因,不要找socket.io的

websocket html5支持才能用,这个没解没办法的

楼上说错了 socket.io 不等于websocket

node.js聊天室 参照了这个,在低版本的ie就可以了。其中原理,继续学习中…

socket.io需要配置不同的协议已支持不同版本的浏览器,理论上从IE6开始就能支持
var io = require('socket.io').listen(server);
require('./config/sio')(io);

config/sio.js中如下配置: sio.configure(function() { //sio.enable(‘browser client minification’); // send minified client //sio.enable(‘browser client etag’); // apply etag caching logic based on version number //sio.enable(‘browser client gzip’); // gzip the file sio.set('log level’, 1); // reduce logging

    // enable all transports (optional if you want flashsocket support, please note that some hosting
    // providers do not allow you to create servers that listen on a port different than 80 or their
    // default port)
    sio.set('transports', ['websocket', 'flashsocket', 'htmlfile', 'xhr-polling', 'jsonp-polling']);
    //sio.set('store', store);
    sio.set('store', new RedisStore({
        redisPub : pub
        , redisSub : sub
        , redisClient : client
    }));
    sio.set('authorization', function(data, accept) {
        // check if there's a cookie header
        var cookies = data.headers.cookie;
        if (cookies) {
            cookies = cookie.parse(cookies);
            //cookies = utils.parseSignedCookies(cookies, 'dfdfdfdfgegsgkkafkjgoijgmv');
            cookies = utils.parseJSONCookies(cookies);
            data.sessionID = cookies['express.sid'];
        } else {
            // if there isn't, turn down the connection with a message
            // and leave the function.
            return accept('No cookie transmitted.', false);
        }
        // accept the incoming connection
        accept(null, true);
    });
});

@gotolnc 一看就是没玩过socket.io的。尽量不要误导新人

socket.io在低版本ie使用轮询,我试过一次是,低级IE不支持JSON对象,你去下载个JOSN2.js在前端页面上socket.io.js之前引入(这个js因为用到JSON对象但是没写兼容性代码所以低级IE报错了)

回到顶部