Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
147
rated 0 times [  150] [ 3]  / answers: 1 / hits: 71505  / 13 Years ago, sun, october 9, 2011, 12:00:00

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). Is there any ways of sending the custom identifier when connecting or just using something to track a custom name for each ID? Thanks!


More From » node.js

 Answers
15

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<len; ++i ){
var c = clients[i];

if(c.clientId == socket.id){
clients.splice(i,1);
break;
}
}

});
});


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



<script>
var socket = io.connect('http://localhost', {port: 8080});

socket.on('connect', function (data) {
socket.emit('storeClientInfo', { customId:000CustomIdHere0000 });
});
</script>


Hope this helps.


[#89717] Friday, October 7, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jimmieo

Total Points: 515
Total Questions: 102
Total Answers: 110

Location: Kazakhstan
Member since Mon, Sep 26, 2022
2 Years ago
;