Skip to content
Advertisement

Socket.io, Broadcast Emit, Seems To Use Only Latest Socket Connected

I’ve been doing a lot of reading on how different people implement socket.io in their multiplayer games so that I can use it in mine. Granted, I’m a noob with the networking layer for this and I desperately need to learn.

Code Context:

-Using Phaser3

-Multiplayer game

-Load all existing players

-Broadcast player joined

-Update player position <— My issue derives from here

Issue:

-Seems to only update position of the newest socket that joins and broadcasts it

-Might not be important but incoming socket.id seems to replace the last socket.id it was set to as id, and I think this since the argument for “run this if incoming socket is not equal to your socket” does not run

-In general, not updating the state of other players in game (not moving the others)

server.js:

JavaScript

relevant client:

JavaScript

in game.js update() function (standard event loop for phaser3 library) I am updating client position with the “updatePos()” function above.

question:

What is it that I am doing wrong? And why is it wrong?

Advertisement

Answer

In this handler in the client where the client is being notified that another client has joined:

JavaScript

You are setting this.socketID = socketID0. I don’t think you want to be doing that because you’re overwriting your own socketID with the id of the last client that joined. That will mess up all future notifications from your client because you will be pretending to have the socketID of the last player to join. I think you should just remove that line entirely.

If you need to initialize your own this.socketID somewhere, then you will need to do that in some other place where it’s only your own socketID and only done once upon connection.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement