Problem with glitch[Solved]


#1

I’m trying to communicate with the glitch server but i seem to keep getting the same error(I properly installed socket.io onto the server)

glitch error: TypeError: Cannot read property ‘on’ of undefined
glitch code

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

io.sockets.on('connection', function(socket) {
    console.log("Client has connected!");
});
io.socket.on ('playerJoined', function (name) {
           ^
         (error)
    console.log (name);
});

console.log ('Server started.');
server.listen(3000); 

playcanvas code

var Network = pc.createScript('Network');

// initialize code called once per entity
Network.prototype.initialize = function() {
    this.socket = io.connect('https://gun-game-server.glitch.me');
  
};

// update code called every frame
Network.prototype.update = function(dt) {
    this.socket.emit ('playerJoined', 'John');
};

#2

Shouldn’t it be io.sockets.on? (note the extra s).


#3

O_O Internal screaming
Im mad at myself


#4

I now can’t seem to get glitch to revive info from play canvas using emit


#5

Do you mean ‘can’t’?


#6

(i type too fast) yes i mean can’t. can you help?


#7

Haven’t used sockets but it looks odd to emit every frame.

The tutorial project here does it once in the init function. https://developer.playcanvas.com/en/tutorials/real-time-multiplayer/


#8

let me check
:slight_smile:


#9

it is still not doing anything


#10

The server code is unfortunately incorrect. The tutorial does have the full server code example at the bottom: https://developer.playcanvas.com/en/tutorials/real-time-multiplayer/

My example project: https://playcanvas.com/project/590282/overview/socketio-test
And Glitch code: https://glitch.com/edit/#!/automatic-custard


#11

i also figured it out. i had it outside of the io.sockets.on()function