javascript - Emit to a specific client with Sockets -
i'm trying make little party system practice sockets, i've come snag, methodology party system be
on page load, ask username, populate clients new username anchor id of username, when anchor clicked, send specific client request invite them party.
i've gotten part sending link, far have:
socket.on('adduser', function(username) { socket.username = username; socket.clientid = socket.id; usernames[username] = username; console.log(socket.id); socket.emit('updatechat', 'server', 'you have connected.'); io.sockets.emit('updateusers', usernames); console.log(usernames); });
for creating new users and
socket.on('updateusers', function(data) { var users = document.getelementbyid('users'); users.innerhtml = ''; (var key in data) { var = document.createelement('a'); a.id = key; a.href='#'; a.innerhtml = key; users.appendchild(a); users.appendchild(document.createelement('br')); document.getelementbyid(key).addeventlistener('click', function() { inviteuser.call(this); }); } });
for client side
i'm getting confused on how send emit specific user instead of everybody,
normally socket.emit
send emit user requested whatever, need send else
i thinking of storing socket.id object , sending request id matches username, how can emit specific id?
tl;dr: how can send emit specific socket.id
request.
thanks guys :)
well have grab client , can either go simple way:
var io = io.listen(server); io.clients[sessionid].send();
which may break, hardly doubt it, it's possibility io.clients might changed, use above caution
or keep track of clients yourself, therefore add them own clients object in connection listener , remove them in disconnect listener.
Comments
Post a Comment