Socket.io custom client ID

后端 未结 11 863
栀梦
栀梦 2020-12-04 07:59

I\'m making a chat app with socket.io, and I\'d like to use my custom client id, instead of the default ones (8411473621394412707, 1120516437992682114

11条回答
  •  死守一世寂寞
    2020-12-04 08:30

    You can create an array on the server, and store custom objects on it. For example, you could store the id created by Socket.io and a custom ID sent by each client to the server:

    var util = require("util"),
        io = require('/socket.io').listen(8080),
        fs = require('fs'),
        os = require('os'),
        url = require('url');
    
        var clients =[];
    
        io.sockets.on('connection', function (socket) {
    
            socket.on('storeClientInfo', function (data) {
    
                var clientInfo = new Object();
                clientInfo.customId         = data.customId;
                clientInfo.clientId     = socket.id;
                clients.push(clientInfo);
            });
    
            socket.on('disconnect', function (data) {
    
                for( var i=0, len=clients.length; i

    in this example, you need to call storeClientInfo from each client.

    
    

    Hope this helps.

提交回复
热议问题