if (s.sid != this.st.user.sid && !identityAsked[s.sid])
{
identityAsked[s.sid] = true;
- this.send("askidentity", {target:s.sid});
+ this.send("askidentity", {target:s.sid, page:s.page || "/"});
}
if (!this.people[s.sid])
this.$set(this.people, s.sid, {id:0, name:"", pages:[s.page || "/"]});
if (!s.page) //peer is in Hall
this.send("askchallenge", {target:s.sid});
else //peer is in Game
- this.send("askgame", {target:s.sid});
+ this.send("askgame", {target:s.sid, page:s.page});
});
break;
}
if (data.code == "connect")
this.send("askchallenge", {target:data.from});
else
- this.send("askgame", {target:data.from});
+ this.send("askgame", {target:data.from, page:data.page});
}
else
{
if (this.people[data.from].id == 0)
{
this.newConnect[data.from] = true; //for self multi-connects tests
- this.send("askidentity", {target:data.from});
+ this.send("askidentity", {target:data.from, page:data.page || "/"});
}
break;
case "disconnect":
return result;
}
+// Helper to safe-send some message through a (web-)socket:
+function send(socket, message)
+{
+ if (!!socket && socket.readyState == 1)
+ socket.send(JSON.stringify(message));
+}
+
module.exports = function(wss) {
// Associative array page --> sid --> tmpId --> socket
// "page" is either "/" for hall or "/game/some_gid" for Game,
Object.keys(clients[page][k]).forEach(x => {
if (k == sid && x == tmpId)
return;
- clients[page][k][x].send(JSON.stringify(Object.assign(
- {code:code, from:sid}, obj)));
+ send(clients[page][k][x], Object.assign({code:code, from:sid}, obj));
});
});
};
};
const messageListener = (objtxt) => {
let obj = JSON.parse(objtxt);
- if (!!obj.target)
- {
- // Check if receiver is connected, because there may be some lag
- // between a client disconnects and another notice.
- if (Array.isArray(obj.target))
- {
- if (!clients[page][obj.target[0]] ||
- !clients[page][obj.target[0]][obj.target[1]])
- {
- return;
- }
- }
- else if (!clients[page][obj.target])
- return;
- }
switch (obj.code)
{
// Wait for "connect" message to notify connection to the room,
// Self multi-connect: manual removal + disconnect
const doKill = (pg) => {
Object.keys(clients[pg][obj.sid]).forEach(x => {
- clients[pg][obj.sid][x].send(JSON.stringify({code: "killed"}));
+ send(clients[pg][obj.sid][x], {code: "killed"});
});
delete clients[pg][obj.sid];
};
if (k != obj.sid)
{
Object.keys(clients[pg][k]).forEach(x => {
- clients[pg][k][x].send(JSON.stringify(Object.assign(
- {code:code, from:obj.sid}, o)));
+ send(clients[pg][k][x], Object.assign({code:code, from:obj.sid}, o));
});
}
});
if (k != sid)
sockIds.push(k);
});
- socket.send(JSON.stringify({code:"pollclients", sockIds:sockIds}));
+ send(socket, {code:"pollclients", sockIds:sockIds});
break;
}
case "pollclientsandgamers": //from Hall
});
}
});
- socket.send(JSON.stringify({code:"pollclientsandgamers", sockIds:sockIds}));
+ send(socket, {code:"pollclientsandgamers", sockIds:sockIds});
break;
}
case "askgame":
case "askfullgame":
{
- const tmpIds = Object.keys(clients[page][obj.target]);
+ // DEBUG:
+ //console.log(sid + " " + page + " " + obj.code + " " + obj.target + " " + obj.page);
+ //console.log(clients);
+ const pg = obj.page || page; //required for askidentity and askgame
+ const tmpIds = Object.keys(clients[pg][obj.target]);
if (obj.target == sid) //targetting myself
{
const idx_myTmpid = tmpIds.findIndex(x => x == tmpId);
tmpIds.splice(idx_myTmpid, 1);
}
const tmpId_idx = Math.floor(Math.random() * tmpIds.length);
- clients[page][obj.target][tmpIds[tmpId_idx]].send(
- JSON.stringify({code:obj.code, from:[sid,tmpId]}));
+ send(clients[pg][obj.target][tmpIds[tmpId_idx]], {code:obj.code, from:[sid,tmpId,page]});
break;
}
case "startgame":
Object.keys(clients[page][obj.target]).forEach(x => {
if (obj.target != sid || x != tmpId)
- {
- clients[page][obj.target][x].send(JSON.stringify(
- {code:obj.code, data:obj.data}));
- }
+ send(clients[page][obj.target][x], {code:obj.code, data:obj.data});
});
break;
case "game":
case "identity":
case "lastate":
- clients[page][obj.target[0]][obj.target[1]].send(JSON.stringify(
- {code:obj.code, data:obj.data}));
+ {
+ const pg = obj.target[2] || page; //required for identity and game
+ send(clients[pg][obj.target[0]][obj.target[1]], {code:obj.code, data:obj.data});
break;
+ }
}
};
const closeListener = () => {