Sign in to follow this  
mla

socket.to(socket.id).emit - Not Working

Recommended Posts

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!

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.