mla Posted February 2, 2018 Share Posted February 2, 2018 It seems that to send a private message to a player, you are supposed to use socket.to(socket.id).emit. I need to emit an image to a specific player so am using this concept, however it doesn't seem to work. Would anyone know how to emit an image to a specific client? Thanks! Quote Link to comment Share on other sites More sharing options...
mla Posted February 2, 2018 Author Share Posted February 2, 2018 Okay, I think I just answered my own question. I need a room.id for each and then broadcast to the room. If anyone knows a better way, please do let me know. Thanks, Laura Quote Link to comment Share on other sites More sharing options...
mla Posted February 2, 2018 Author Share Posted February 2, 2018 Update on this - when I emit a private message it works, when I emit a private picture on canvas, it doesn't work. Any ideas why this would be? Thanks! Quote Link to comment Share on other sites More sharing options...
GameDev Quest Posted February 4, 2018 Share Posted February 4, 2018 Can you provide sample code? Quote Link to comment Share on other sites More sharing options...
mla Posted February 5, 2018 Author Share Posted February 5, 2018 server code: var myroom = 'room' + playerNumber; socket.join(myroom); io.sockets.in(myroom).emit('showCard', {x: newPlayer.getX, y: newPlayer.getY, objCard: newPlayer.getobjCard}); client code: socket.on('showCard', function(data){ var objCard = new Image(); objCard.onload = function() { context.drawImage(objCard, data.x+100, data.y); console.log('image card'); } objCard.src = data.objCard; }); I emit 'message' and it works, but not when I try to show an image. If I simply use, io.sockets.emit, the image displays, but to all rooms. I only want it in one room. Quote Link to comment Share on other sites More sharing options...
mla Posted February 9, 2018 Author Share Posted February 9, 2018 Figured it out. Emit an image in a room does work. I just had my socket.on, placed in the wrong file. Added to the correct client file and it all works great! 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.