IamThatGuy Posted October 14, 2014 Share Posted October 14, 2014 So I have my server running and I can't figure out how to send to the specific socket.ID. The code in dispatchGameMessage may look funny bcuz I been trying various things to send to the 2 people in the Game. Google isn't helping much with this. And now some code snipplets:// varsvar express = require('express');var app = express();var server = require('http').createServer(app);var io = require('socket.io')(server);var port = 3000; server.listen(port, function () { console.log('Server listening at port %d', port);}); // Routingapp.use(express.static(__dirname)); // usernames which are currently connected to the chatvar i = 0;var channels = {};var channelRefs = {};var numUsers = 0;var clientHoles = new Array();var found = false; io.on('connection', function (socket) { // when the client emits "C" - Create Channel/Join, this listens and executes socket.on('C', function (data) { console.log("Data Connect" + socket.id, port); found = false;//increase # of clients connected++numUsers; // we store the numUsers as socket session usernamesocket.username = data; addedUser = true; //first user connectedif(numUsers == 1){channels[data] = socket.id;socket.emit('W', "W");}else{//go through every connection availablefor(i = 1; i < numUsers; i++){console.log(channels[data], port);//if channel has a player in itif(channels.hasOwnProperty(data) && channels[data] != -1){socket.emit('W', socket.id);channelRefs[data] = {id1: channels[data], id2: socket.id};channels[data] = -1; //game in progress/finished//this may also be the problemdispatchGameMessage(data, "S", socket);found = true;break;}} //if channel not found, create itif(found == false){socket.emit('W', socket.id);channels[data] = socket.id;}}}); });}); //the problem is in here =]// Sends game command to channel (Game In Progress)function dispatchGameMessage (chan_name, data, sock) {//channelRefs[chan_name].id1 and channelRefs[chan_name].id2 are different if(data == "C"){console.log("ID 1 = " + channelRefs[chan_name].id1);console.log("ID 2 = " + channelRefs[chan_name].id2);}//bleeeeeeeeeeeeif(data.length == 1){//sock.broadcast(data, data);sock.socket(channelRefs[chan_name].id2).emit(data, channelRefs[chan_name].id2); sock.socket(socket(channelRefs[chan_name].id1)).emit(data, channelRefs[chan_name].id1); }} No matter what I do, it sends the message be the client(socketID) that sent the message. Quote Link to comment Share on other sites More sharing options...
bulkan Posted October 14, 2014 Share Posted October 14, 2014 Your channels implementation seems like rooms in socket.io why not just use rooms ? http://socket.io/docs/rooms-and-namespaces/#rooms IamThatGuy 1 Quote Link to comment Share on other sites More sharing options...
Bruno Assarisse Posted October 14, 2014 Share Posted October 14, 2014 I agree, rooms would be a more adequate solution. Anyway, to send data to a specific socket:io.to(socket.id).emit("event", data);The io.to() method is used to emit message to a room, and every socket is placed in a room named after its ID (which is documented on the link @bulkan provided). IamThatGuy 1 Quote Link to comment Share on other sites More sharing options...
IamThatGuy Posted October 14, 2014 Author Share Posted October 14, 2014 thanks guys. i didnt know about rooms. Quote Link to comment Share on other sites More sharing options...
IamThatGuy Posted October 14, 2014 Author Share Posted October 14, 2014 Is there a built in function/method to get the # of people in a certain room? Quote Link to comment Share on other sites More sharing options...
Gio Posted October 15, 2014 Share Posted October 15, 2014 Yes, but it depends on your version of socket.ioWith newer versions you can doObject.keys(io.nsps[namespace].adapter.rooms[room]).lengthWhere namespace is the namespace you are using, or '/' which is the default namespace, and room is the name of your room.With older versions (when namespaces didn't exist) you'd do:io.sockets.clients(room).length IamThatGuy 1 Quote Link to comment Share on other sites More sharing options...
IamThatGuy Posted October 16, 2014 Author Share Posted October 16, 2014 Thanks @Gio I guess I got one more question regarding the sockets for anyone who can answer. Say I got 2 people in a room. How can I get the socket.id of a certain position or index of a certain room. Since my game only needs 2 players per room, when "player 1" sends a command, I would like to emit a message saying its "player 1" sending the command back to "game players" instead of there socket.id which I am currently using and checking if the socket id is them on client. Sending 1 or 2 which would represent the player would shorten the socket length when dispatching to room. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.