projects
/
vchess.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Fix populate.sql
[vchess.git]
/
server
/
sockets.js
diff --git
a/server/sockets.js
b/server/sockets.js
index
f904311
..
e791293
100644
(file)
--- a/
server/sockets.js
+++ b/
server/sockets.js
@@
-24,6
+24,7
@@
module.exports = function(wss) {
// or "/mygames" for Mygames page (simpler: no 'people' array).
// tmpId is required if a same user (browser) has different tabs
let clients = {};
// or "/mygames" for Mygames page (simpler: no 'people' array).
// tmpId is required if a same user (browser) has different tabs
let clients = {};
+ // NOTE: only purpose of sidToPages = know when to delete keys in idToSid
let sidToPages = {};
let idToSid = {};
wss.on("connection", (socket, req) => {
let sidToPages = {};
let idToSid = {};
wss.on("connection", (socket, req) => {
@@
-41,7
+42,7
@@
module.exports = function(wss) {
if (k == sid && x == tmpId) return;
send(
clients[page][k][x].socket,
if (k == sid && x == tmpId) return;
send(
clients[page][k][x].socket,
- Object.assign({ code: code, from:
sid
}, obj)
+ Object.assign({ code: code, from:
[sid, tmpId]
}, obj)
);
});
});
);
});
});
@@
-67,11
+68,10
@@
module.exports = function(wss) {
const doDisconnect = () => {
deleteConnexion();
// Nothing to notify when disconnecting from MyGames page:
const doDisconnect = () => {
deleteConnexion();
// Nothing to notify when disconnecting from MyGames page:
- if (page != "/mygames" && (!clients[page] || !clients[page][sid])) {
- // I effectively disconnected from this page:
+ if (page != "/mygames") {
notifyRoom(page, "disconnect");
if (page.indexOf("/game/") >= 0)
notifyRoom(page, "disconnect");
if (page.indexOf("/game/") >= 0)
- notifyRoom("/", "gdisconnect", { page:page });
+ notifyRoom("/", "gdisconnect", { page:
page });
}
};
const messageListener = (objtxt) => {
}
};
const messageListener = (objtxt) => {
@@
-83,7
+83,7
@@
module.exports = function(wss) {
case "connect": {
notifyRoom(page, "connect");
if (page.indexOf("/game/") >= 0)
case "connect": {
notifyRoom(page, "connect");
if (page.indexOf("/game/") >= 0)
- notifyRoom("/", "gconnect", { page:page });
+ notifyRoom("/", "gconnect", { page:
page });
break;
}
case "disconnect":
break;
}
case "disconnect":
@@
-121,28
+121,47
@@
module.exports = function(wss) {
break;
}
case "pollclients": {
break;
}
case "pollclients": {
- // From
Hall or
Game
- let sockIds =
[]
;
+ // From Game
+ let sockIds =
{}
;
Object.keys(clients[page]).forEach(k => {
Object.keys(clients[page]).forEach(k => {
- // Avoid polling myself: no new information to get
- if (k != sid) sockIds.push(k);
+ sockIds[k] = {};
+ Object.keys(clients[page][k]).forEach(x => {
+ // Avoid polling my tmpId: no information to get
+ if (k != sid || x != tmpId)
+ sockIds[k][x] = { focus: clients[page][k][x].focus };
+ });
});
send(socket, { code: "pollclients", sockIds: sockIds });
break;
}
case "pollclientsandgamers": {
// From Hall
});
send(socket, { code: "pollclients", sockIds: sockIds });
break;
}
case "pollclientsandgamers": {
// From Hall
- let sockIds =
[]
;
+ let sockIds =
{}
;
Object.keys(clients["/"]).forEach(k => {
Object.keys(clients["/"]).forEach(k => {
- // Avoid polling myself: no new information to get
- if (k != sid) sockIds.push({sid:k});
+ sockIds[k] = {};
+ Object.keys(clients[page][k]).forEach(x => {
+ // Avoid polling my tmpId: no information to get
+ if (k != sid || x != tmpId) {
+ sockIds[k][x] = {
+ page: "/",
+ focus: clients[page][k][x].focus
+ };
+ }
+ });
});
// NOTE: a "gamer" could also just be an observer
Object.keys(clients).forEach(p => {
if (p.indexOf("/game/") >= 0) {
Object.keys(clients[p]).forEach(k => {
});
// NOTE: a "gamer" could also just be an observer
Object.keys(clients).forEach(p => {
if (p.indexOf("/game/") >= 0) {
Object.keys(clients[p]).forEach(k => {
- // 'page' indicator is needed for gamers
- if (k != sid) sockIds.push({ sid:k, page:p });
+ if (!sockIds[k]) sockIds[k] = {};
+ Object.keys(clients[p][k]).forEach(x => {
+ if (k != sid || x != tmpId) {
+ sockIds[k][x] = {
+ page: p,
+ focus: clients[p][k][x].focus
+ };
+ }
+ });
});
}
});
});
}
});
@@
-190,6
+209,9
@@
module.exports = function(wss) {
clients[page][rid][rtmpId].socket,
{ code: "askfullgame", from: [sid,tmpId] }
);
clients[page][rid][rtmpId].socket,
{ code: "askfullgame", from: [sid,tmpId] }
);
+ } else {
+ // I'm the only person who have the game for the moment:
+ send(socket, { code: "fullgame", data: { empty: true } });
}
}
break;
}
}
break;
@@
-217,7
+239,9
@@
module.exports = function(wss) {
case "drawoffer":
case "rematchoffer":
case "draw":
case "drawoffer":
case "rematchoffer":
case "draw":
- notifyRoom(page, obj.code, {data: obj.data}, obj.excluded);
+ // "newgame" message can provide a page (corr Game --> Hall)
+ notifyRoom(
+ obj.page || page, obj.code, {data: obj.data}, obj.excluded);
break;
case "rnewgame":
break;
case "rnewgame":
@@
-295,6
+319,7
@@
module.exports = function(wss) {
case "getfocus":
case "losefocus":
case "getfocus":
case "losefocus":
+ clients[page][sid][tmpId].focus = (obj.code == "getfocus");
if (page == "/") notifyRoom("/", obj.code, { page: "/" }, [sid]);
else {
// Notify game room + Hall:
if (page == "/") notifyRoom("/", obj.code, { page: "/" }, [sid]);
else {
// Notify game room + Hall:
@@
-312,8
+337,8
@@
module.exports = function(wss) {
case "lastate":
{
const pg = obj.target[2] || page; //required for identity and game
case "lastate":
{
const pg = obj.target[2] || page; //required for identity and game
- // NOTE: if in game we ask identity to opponent still in Hall,
- //
but leaving Hall, clients[pg] or clients[pg][target] could be undefined
+ // NOTE: if in game we ask identity to opponent still in Hall,
but
+ //
leaving Hall, clients[pg] or clients[pg][target] could be undef.
if (!!clients[pg] && !!clients[pg][obj.target[0]]) {
send(
clients[pg][obj.target[0]][obj.target[1]].socket,
if (!!clients[pg] && !!clients[pg][obj.target[0]]) {
send(
clients[pg][obj.target[0]][obj.target[1]].socket,