Obtain a list of clients connecting to Socket.io from the client

Asked 2 years ago, Updated 2 years ago, 135 views

I would like to get a list of clients connecting to Socket.io from my client.

var http=require("http");
var server = http.createServer(function(req,res){
    res.write("Hello World!""); // ・・・ ①
    res.end();//...2
});

// Preparing Socketio
vario=require('socket.io')(server);

// Client Connection Processing
io.on('connection', function(socket){
    console.log("client connected!!!")

    // Client Disconnect Action
    socket.on('disconnect', function(){
        console.log("client disconnected!!")
    });
    // Receive from client (socket.on)
    socket.on("from_client", function(obj){
        console.log(obj)
    });
});

server.listen (8080);

Part ②2 of

// Obtaining Connected Client Information    
var clients =io.sockets.clients();
// Retrieve the number of connected clients 
var count = clients.length;

if(count!==0){
    for(vari=0;i<count;i++){
        res.write(clients[i]);
        res.write("</br>");
        res.end();
    }
}

I would like to see a list if the client accesses the http://<node.js server domain >//.
However, an error occurs because no instance of io has been created.

If you know how to deal with it, please let me know.

node.js socket.io

2022-09-29 22:12

1 Answers

I think io's instance has been created since ②2 is in the callback function.
However, when I tried it here (socket.io 1.3.6), there was an error because there was no clients function. I got client information through io.eio.clientsCount and io.eio.clients.
I don't know if it's the right way to take it, but...


2022-09-29 22:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.