开发者

Socket.io issue: initializing a ton of clients using 'xhr-polling'

开发者 https://www.devze.com 2023-02-01 04:42 出处:网络
I\'m trying to get socket.io set up and I\'m running into what I\'m 90% sure is a problem. I am running my server, it says its connected then I get a flurry of new clients that come in after 2-3 secon

I'm trying to get socket.io set up and I'm running into what I'm 90% sure is a problem. I am running my server, it says its connected then I get a flurry of new clients that come in after 2-3 seconds. Here's my terminal output:

21 Dec 17:36:53 - socket.io ready - accepting connections
21 Dec 17:37:13 - Initializing client with transport "websocket"
21 Dec 17:37:13 - Client 4786583543755114 connected
21 Dec 17:37:18 - Client 4786583543755114 disconnected
21 Dec 17:37:18 - Initializing client with transport "xhr-polling"
21 Dec 17:37:18 - Client 377916906028986 connected
21 Dec 17:37:18 - Initializing client with transport "xhr-polling"
21 Dec 17:37:18 - Client 3885312571655959 connected
21 Dec 17:37:18 - Initializing client with transport "xhr-polling"
21 Dec 17:37:18 - Client 38271573395468295 connected

And many many more every 1-2 seconds. I'm using safari, which supports websockets and that is evident by the first response.

Here's my server code:

server = http.createServer(function(req, res){ 
    // your normal server code 
    res.writeHead(200, {'Content-Type': 'text/html'}); 

    var path = url.parse(req.url).pathname; 

    // console.log(__dirname + path);
    fs.readFile(__di开发者_如何学Pythonrname + path, function(error, data) {
        res.end(data); 
    }); 


});

server.listen(80);

// socket.io, I choose you
var socket = io.listen(server);

socket.on('connection', function(client){
    // new client is here!
    client.on('message', function(){  });
    client.on('disconnect', function(){  });
});

.. And client code:

    <script> 

        var socket = new io.Socket();
        socket.connect();

        socket.on('message', function(obj){
            alert('got some data ' + obj);
        });

        socket.on('connect', function() {
            console.log("We've connected!");
            socket.send('some data');
        })


    </script>

Any lead or help would be fantastic. Thanks!


Well it turned out to be a REALLY minor fix that made all the difference - don't forget to add the HTML5 doctype <!doctype html> to the top of your client!

0

精彩评论

暂无评论...
验证码 换一张
取 消