Hi there
Anyone have an experience to make multipayer game with Phaser 3 and Socket.io ?
I have a problem with socket.io and phaser, especially if player reconnect again or restart the game scenes.
Player -> Die -> Gameover -> Reconnect/restart -> (Error)
I have 2 scenes, first one for “main game” and second one for “game GUI” to show player stats and game popup.
All variable (including socket) and game functions are stored inside create(), so there are no global functions and variable.
then if the game or scenes are restarted, it’s take a delay about 3 seconds (freeze), then the game error are appear.
all error are happen inside socket.on(‘emit’)
How to fix this?
It’s not socketio problem, i fix this here Event emitter , Can't communicate after scene restart/start again
I think what causing this problem is a players group
var players = this.physics.add.group();
this.physics.add.overlap(players, bullets, collision, null, this);
On first play
After restart scenes/restart game
Any help ?
I dont see any code but I read a bit about socket io in the past, is the code built so that the game is run on the server side?
Where are you maintaining your socket connection client-side? You want to keep it sort of “above” your Phaser code if that makes sense. Because things move in and out of memory in Phaser, particularly when you change Scenes. Your connection may be getting disposed. Or if the connection is living inside of some kind of Phaser entity, something may be going on in the background that is separating you from it.
If you can post how you are connecting with Socket io and the enclosing code we can try to see if we can spot the problem.
Yes, main script are handled by a server
Yes, the problem is at client-side.
Now, i moved socket variable at top or outside of any classes / function, so, socket are loaded once.
let socket = io.connect('http://localhost:8081/', {
reconnection: false,
});
I look at agar.io and similar games, i think there is no disconnected from server even player are die, except if player close the game. socket are configured once on play button clicked. so, i follow this mechanism.
create(){
socket.on('newPlayer', function (id) {
addPlayer(self, id);
});}
Socket events are configured inside create() at “main_game” scene, so, this will re-configured each scene start/restart.
yes, I’m, not an expert really, but create is called as? once the HTML is refreshed… and what if you try to listen for events continuously ? does it help? like if for example…does ur new player fire on server side or client as to speak? I know not too much but If I were you I would console.log and try different setups to make the code run smoothly as you want it and listen for the events. and see if the new player gets added the idea is to have important say locations of objects on the server side… and say object An overlap B emit an event to all players, start an explosion with x and yon all screens same time… and client-side send direction to serverside, with its child object.
I don’t know if it explains ur problem but I can’t test anything right now
but say, new player, for example, emit from someone starts the game with some location x y. it emits something to the server who emits an object x y to all players screens. i still think it is something with the create function that is not working as you expect it, try like console log something when on add player, and open a new window to see if it works anyway I am not an expert.=)
I really appreciate your help.
But, actually it’s does’t solve my problem, i will create a separate topic. I think it’s not socket.io that causing this problem, just a bad implementation.
Ok I will check a bit later, I am sorry if it was no help, but its different thinking to make multiplayer games, I will try someday I am just concerned about the lag and stuff in socket.io on the live server, however, there are good tutorials online, but not very advanced… Like pretty simple add a new player and object, and keeping the logic on the server. and I don’t know if any help but maybe check if you have the correct “this”.
I see your scene is undefined but anyway in the future you need to share code for easier help like a playground.
cheers!
I not sharing my code because it’s already complex, currently there are 1400 line of code and i don’t really know what specific classes or functions that causing this bugs.
All multiplayer game just about a hack/tricky to make it run look smooth