Hi, I am currently making a multiplayer FPS game and I am having trouble with the networking side of it. I am currently trying to use socket.emit, but I cannot get the server or client to send or receive information. I know that the server and client connect to each other correctly, as I get a client has connected log when a person does. My scripts are not outputting any errors as far as I know. I also checked to see if it was a version issue, but when I make a copy of the tutorial and force update its networking scripts to the versions I am using the code still works.
Here is my code for the networking script:
var Network = pc.createScript('network');
Network.id = null;
Network.socket = null;
var builded = null;
// initialize code called once per entity
Network.prototype.initialize = function() {
var socket = io.connect("https://lolshotime.glitch.me");
Network.socket = socket;
var builded = function(name, x, y, z, tag){
Network.socket.emit("player:build", {name: name});
};
socket.emit("Init");
this.app.on("player:build", builded);
};
// update code called every frame
Network.prototype.update = function(dt) {
};
// swap method called for script hot-reloading
// inherit your script state here
// Network.prototype.swap = function(old) { };
// to learn more about script anatomy, please read:
// https://developer.playcanvas.com/en/user-manual/scripting/
Here is my code on glitch:
var server = require('http').createServer();
var options = {
cors: true
}
var io = require('socket.io')(server, options);
io.sockets.on('connection', function(socket) {
console.log("Client has connected!");
io.sockets.on('Init', function(){
console.log("yayay");
});
});
io.sockets.on("player:build", function(data){
console.log(data.name);
});
console.log ('Server started.');
server.listen(3000);
Here is the link to my game: https://playcanvas.com/editor/scene/1171515
Here is the link to my glitch: https://glitch.com/edit/#!/lolshotime?path=server.js%3A20%3A20
Press 1 to destroy(on by default) and 2 to build.
(Please excuse my lack of knowledge, this is my first multiplayer game and I am having a hard time grasping the concepts of node.js networking.)