I changed something in the server/client's code, and now after 1 or 2 messages, the client loses connection(Even if I make a new extremely simple client and send really simple messages), receives code 1002 from the server, throwing no errors(server also throws no error). And the server doesn't fire the close event.
My server code is this:
The client code is:
Did I mistype something? If I build another server that just echoes the client's message back to him, it works, so I believe it isn't a firewall problem. What can be wrong?
EDIT: After around 3 to 4 hours searching the problem, I've found out I mistyped "playersalive++" and typed instead "playeralive++", which wasn't throwing errors, but would BREAK THE WHOLE SERVER-CLIENT SYSTEM.
My server code is this:
Code:
var ws = require("nodejs-websocket")
var playercounter= new Array(101);
playercounter.fill(0)
var giveID=0
var disconnectthis
var string
var numberofplayers=0
var serverfull=false
var playersalive=0
var server = ws.createServer(function (connection) {
connection.playerid = null
connection.on("text", function (str) {
if(str==="ping"){
console.log("pinged")
connection.sendText("online")
connection.close()
}
else if (connection.playerid === null) {
for(var i=1;i<=101;i++){
if(i===101){
connection.sendText("serverfull")
console.log("Server is full")
connection.close()
}else if(playercounter[i]===0 && i<101){
giveID=i
playercounter[i]=1
i=101
}
}
connection.playerid = giveID
numberofplayers++
playeralive++
for(var o=1;o<101;o++){
if(playercounter[o]===1){
connection.send("spawn:"+Number(playercounter[o]))
}
}
connection.sendText("id:"+giveID)
broadcast("spawn:"+giveID)
console.log("New player joined, ID:"+giveID)
broadcast("eventid:"+giveID)
if(numberofplayers>10){
setTimeout(()=>{
broadcast("begin")
serverlock=true
},3000)
}
}
else if(str.startsWith("arrow")){
broadcast(str)
}
else if(str.startsWith("dead")){
broadcast(str)
playeralive--
}
else{broadcast("update:"+connection.playerid+":"+str)}
})
connection.on("close", function(){
numberofplayers--
playeralive--
disconnectthis=Number(connection.playerid)
playercounter[disconnectthis]=0
broadcast("dead:"+connection.playerid)
console.log("Player under ID "+connection.playerid+" disconnected")
})
}).listen(80)
function broadcast(str) {
server.connections.forEach(function (connection) {
connection.sendText(str)
})
}
console.log("Server running!")
The client code is:
Code:
var update
var lastID=0
var contadordeeventos=1
var eventmaxcounter=0
window.coordinates
window.connection = new WebSocket("ws://localhost:80")
connection.onopen = function () {
console.log("Connected")
intervalo=setInterval(()=>{
coordinates=$gameVariables.value(4)+":"+$gameVariables.value(5);
coordinates=coordinates.toString()
connection.send(coordinates);
},16)
}
connection.onmessage = function (event) {
update=event.data
update=update.toString().split(":")
if(update[0].startsWith("update")){
if(Number(update[1])!==Number($gameVariables.value(10))){
try{$gameMap.event(Number(update[1])).setDestination(Number(update[2]), Number(update[3]))
}
catch(e){
}
//This is just to make sure that other players that joined before will be visible
}
}
else if(update[0].startsWith("id")){//If the first part starts with ID
$gameVariables.setValue(10,Number(update[1]))/*We set the variable ID 10 to the our ID. This will help us make sure that there's not an event copying our movement*/
}
else if(update[0].startsWith("eventid")){//If the first part starts with eventid
$gameVariables.setValue(11,0)/*We give the variable with that ID a value equal to the ID. This way, we can make sure that the 3 events we made in the map will only be visible and move when there's a player with ID equal to their ID*/
$gameSelfSwitches.setValue([1, Number(update[1]), 'A'], false);
}
else if(update[0].startsWith("spawn")){
if(Number(update[1])!==Number($gameVariables.value(10)) ){
if(eventmaxcounter<100){
Yanfly.SpawnEventAt(2, 1, 5, 5, true)
eventmaxcounter++
}
}
}
else if(update[0].startsWith("arrow")){
if(Number(update[3])!==Number($gameVariables.value(10))){
Yanfly.SpawnEventAt(2, 3, Number(update[1]), Number(update[2]), true)
$gameVariables.setValue(8,Number(update[4]))
}
}
else if(update[0].startsWith("dead")){
$gameVariables.setValue(11,Number(update[1]))
}
}
document.addEventListener("keypress", function(event) {
if (event.repeat) return
if (event.keyCode ===97) {
$gameTemp.reserveCommonEvent(2);
}
})
Did I mistype something? If I build another server that just echoes the client's message back to him, it works, so I believe it isn't a firewall problem. What can be wrong?
EDIT: After around 3 to 4 hours searching the problem, I've found out I mistyped "playersalive++" and typed instead "playeralive++", which wasn't throwing errors, but would BREAK THE WHOLE SERVER-CLIENT SYSTEM.