Socket.io does not work on Firefox & Chrome

后端 未结 3 1067
谎友^
谎友^ 2021-01-15 04:37

I\'m trying to develop a simple chat application. Here is my chat.js file.

var app = require(\'http\').createServer(handler)
, io = require         


        
相关标签:
3条回答
  • 2021-01-15 04:45

    Thankyou ebohlman for your time but I solved the problem. In my chat.js I added the following line.

    io.configure('development', function(){
      io.set('transports', ['xhr-polling']);
    });
    

    Now my chat.js looks like.

    var app = require('http').createServer(handler)
    , io = require('socket.io').listen(app)
    , fs = require('fs');
    
    app.listen(8124);
    
    io.configure('development', function(){
      io.set('transports', ['xhr-polling']);
    });
    
    function handler (req, res) {
    fs.readFile(__dirname + '/chat.html',
    function (err, data) {
    if (err) {
    res.writeHead(500);
    return res.end('Error loading chat.html');
    }
    res.writeHead(200);
    res.end(data);
    });
    }
    io.sockets.on('connection', function (socket) {
        socket.on('addme',function(username) {
            socket.username = username;
            socket.emit('chat', 'SERVER', 'You have connected');
            socket.broadcast.emit('chat', 'SERVER', username + ' is on deck');
        });
        socket.on('sendchat', function(data) {
            io.sockets.emit('chat', socket.username, data);
        });
        socket.on('disconnect', function() {
            io.sockets.emit('chat', 'SERVER', socket.username + ' has left the building');
        });
    });
    

    But I still don't know what might have caused the error. If you know please explain!

    0 讨论(0)
  • 2021-01-15 04:55

    I encountered the same issues when trying to publish to AppFog. It's caused by websockets not being supported by AppFog yet, so your fix worked by forcing socket.io to use long polling/AJAX. AppFog is working on supporting websockets according to their website, but no dates have been given.

    Most cloud PaaS providers that I have found do NOT support websockets yet unfortunately. One notable exception that I am testing out now is Nodejitsu: http://nodejitsu.com/

    0 讨论(0)
  • 2021-01-15 04:56

    I think this will help to know about the issue, and how it fixes I searched lot for it and finally I got it

    What does "xhr-polling" config do in socket.io?

    thank you

    0 讨论(0)
提交回复
热议问题