[SOLVED] Multiplayer tutorial, socket.on error

Hello,

I follow this tutorial : https://developer.playcanvas.com/en/tutorials/real-time-multiplayer/

At this part :

No problem without the fonction:

  • no image possible

Is this normal socket.js give alot of error ?

  • no image possible

It’s probably should be simple but im new to js.

Hello @qtestman and welcome,

I haven’t tried this example myself but I think, seeing from the screen shot, the issue is that you are using socket outside of lines 4 to 6.

Something like this should work:

io.sockets.on('connection', function(socket) {
    console.log('Client has connected');

    socket.on('playerJoined', function(name){
        console.log(name);
    })
}
1 Like

Thank, it worked, here is the full code

var server = require('http').createServer();
var io = require('socket.io')(server);

io.sockets.on('connection', function(socket) {
    console.log('Client has connected');

    socket.on('playerJoined', function(name){
        console.log(name);
    })
});
console.log ('Server started.');
server.listen(3000);

So i guess, it run the function inside of io.socket.on

1 Like