Fix session rebind and share between https and wss
Signed-off-by: James Ketrenos <james_eikona@ketrenos.com>
This commit is contained in:
parent
33e16ac17a
commit
34206b40ad
@ -285,6 +285,7 @@ const MediaAgent = () => {
|
|||||||
update = true;
|
update = true;
|
||||||
peers[name] = {
|
peers[name] = {
|
||||||
local: true,
|
local: true,
|
||||||
|
muted: true,
|
||||||
attributes: {
|
attributes: {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -80,6 +80,7 @@ const sessionParser = session({
|
|||||||
});
|
});
|
||||||
|
|
||||||
app.use(sessionParser);
|
app.use(sessionParser);
|
||||||
|
app.locals.sessionParser = sessionParser;
|
||||||
|
|
||||||
const index = require("./routes/index");
|
const index = require("./routes/index");
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ const debug = {
|
|||||||
audio: false,
|
audio: false,
|
||||||
get: true,
|
get: true,
|
||||||
set: true,
|
set: true,
|
||||||
update: true
|
update: false//true
|
||||||
};
|
};
|
||||||
|
|
||||||
let gameDB;
|
let gameDB;
|
||||||
@ -496,7 +496,7 @@ const loadGame = async (id) => {
|
|||||||
if (game) {
|
if (game) {
|
||||||
try {
|
try {
|
||||||
game = JSON.parse(game);
|
game = JSON.parse(game);
|
||||||
console.log(`${info} Creating backup of games/${id}`);
|
console.log(`${info}: Creating backup of games/${id}`);
|
||||||
await writeFile(`games/${id}.bk`, JSON.stringify(game));
|
await writeFile(`games/${id}.bk`, JSON.stringify(game));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(`Attempting to load backup from games/${id}.bk`);
|
console.log(`Attempting to load backup from games/${id}.bk`);
|
||||||
@ -2822,7 +2822,7 @@ const part = (peers, session, id) => {
|
|||||||
|
|
||||||
|
|
||||||
const getName = (session) => {
|
const getName = (session) => {
|
||||||
return session.name ? session.name : "Unnamed";
|
return session.name ? session.name : session.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
const saveGame = async (game) => {
|
const saveGame = async (game) => {
|
||||||
@ -2989,21 +2989,34 @@ const sendWarning = (session, warning) => {
|
|||||||
session.ws.send(JSON.stringify({ type: 'warning', warning }));
|
session.ws.send(JSON.stringify({ type: 'warning', warning }));
|
||||||
}
|
}
|
||||||
|
|
||||||
router.ws("/ws/:id", async (ws, req) => {
|
router.ws("/ws/:id", (ws, req) => {
|
||||||
|
/* Connect the WebSocket to the app's sessionParser */
|
||||||
|
req.app.locals.sessionParser(req, {}, () => {
|
||||||
|
if (!req.session.player_id) {
|
||||||
|
req.session.player_id = crypto.randomBytes(16).toString('hex');
|
||||||
|
console.log(`[${req.session.player_id.substring(0, 8)}]: wss - New session connected`);
|
||||||
|
} else {
|
||||||
|
console.log(`[${req.session.player_id.substring(0, 8)}]: wss - Existing session being used`);
|
||||||
|
}
|
||||||
|
wsConnect(ws, req);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const wsConnect = async (ws, req) => {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
const gameId = id;
|
const gameId = id;
|
||||||
if (!req.session.player_id) {
|
if (!req.session.player_id) {
|
||||||
req.session.player_id = crypto.randomBytes(16).toString('hex');
|
throw new Error(`player_id not set from http load`);
|
||||||
}
|
}
|
||||||
const short = `[${req.session.player_id.substring(0, 8)}]`;
|
const short = `[${req.session.player_id.substring(0, 8)}]`;
|
||||||
ws.id = short;
|
ws.id = short;
|
||||||
|
|
||||||
console.log(`${short}:${gameId} - New connection from client.`);
|
console.log(`${short}: Game ${gameId} - New connection from client.`);
|
||||||
if (!(id in audio)) {
|
if (!(id in audio)) {
|
||||||
audio[id] = {}; /* List of peer sockets using session.name as index. */
|
audio[id] = {}; /* List of peer sockets using session.name as index. */
|
||||||
console.log(`${short}:${id} - New Game Audio`);
|
console.log(`${short}: Game ${id} - New Game Audio`);
|
||||||
} else {
|
} else {
|
||||||
console.log(`${short}:${id} - Already has Audio`);
|
console.log(`${short}: Game ${id} - Already has Audio`);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Setup WebSocket event handlers prior to performing any async calls or
|
/* Setup WebSocket event handlers prior to performing any async calls or
|
||||||
@ -3121,7 +3134,7 @@ router.ws("/ws/:id", async (ws, req) => {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 'game-update':
|
case 'game-update':
|
||||||
console.log(`${short}:${id}:${getName(session)} - full game update.`);
|
console.log(`${short}: <- game-update ${getName(session)} - full game update.`);
|
||||||
sendGameToPlayer(game, session);
|
sendGameToPlayer(game, session);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -3182,7 +3195,7 @@ router.ws("/ws/:id", async (ws, req) => {
|
|||||||
update.players = game.players;
|
update.players = game.players;
|
||||||
break;
|
break;
|
||||||
case 'color':
|
case 'color':
|
||||||
console.log(`${session.id}: -> Returning color as ${session.color} for ${session.name}`);
|
console.log(`${session.id}: -> Returning color as ${session.color} for ${getName(session)}`);
|
||||||
update.color = session.color;
|
update.color = session.color;
|
||||||
break;
|
break;
|
||||||
case 'timestamp':
|
case 'timestamp':
|
||||||
@ -3276,13 +3289,13 @@ router.ws("/ws/:id", async (ws, req) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
resetDisconnectCheck(game, req);
|
resetDisconnectCheck(game, req);
|
||||||
console.log(`${short}:WebSocket connect from game ${id}:${getName(session)}`);
|
console.log(`${short}: Game ${id} - WebSocket connect from ${getName(session)}`);
|
||||||
|
|
||||||
if (session.keepAlive) {
|
if (session.keepAlive) {
|
||||||
clearTimeout(session.keepAlive);
|
clearTimeout(session.keepAlive);
|
||||||
}
|
}
|
||||||
session.keepAlive = setTimeout(() => { ping(session); }, 2500);
|
session.keepAlive = setTimeout(() => { ping(session); }, 2500);
|
||||||
});
|
};
|
||||||
|
|
||||||
const debugChat = (game, preamble) => {
|
const debugChat = (game, preamble) => {
|
||||||
preamble = `Degug ${preamble.trim()}`;
|
preamble = `Degug ${preamble.trim()}`;
|
||||||
@ -3695,7 +3708,6 @@ const createGame = (id) => {
|
|||||||
/* Look for a new game with random words that does not already exist */
|
/* Look for a new game with random words that does not already exist */
|
||||||
while (!id) {
|
while (!id) {
|
||||||
id = randomWords(4).join('-');
|
id = randomWords(4).join('-');
|
||||||
console.log(`Looking for ${id}`);
|
|
||||||
try {
|
try {
|
||||||
/* If file can be read, it already exists so look for a new name */
|
/* If file can be read, it already exists so look for a new name */
|
||||||
accessSync(`games/${id}`, fs.F_OK);
|
accessSync(`games/${id}`, fs.F_OK);
|
||||||
@ -3704,6 +3716,7 @@ const createGame = (id) => {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
console.log(`${info} creating ${id}`);
|
||||||
|
|
||||||
const game = {
|
const game = {
|
||||||
id: id,
|
id: id,
|
||||||
@ -3737,9 +3750,11 @@ router.post("/", (req, res/*, next*/) => {
|
|||||||
const game = createGame();
|
const game = createGame();
|
||||||
if (!req.session.player_id) {
|
if (!req.session.player_id) {
|
||||||
req.session.player_id = crypto.randomBytes(16).toString('hex');
|
req.session.player_id = crypto.randomBytes(16).toString('hex');
|
||||||
|
console.log(`[${req.session.player_id.substring(0, 8)}]: https - New session connected`);
|
||||||
|
} else {
|
||||||
|
console.log(`[${req.session.player_id.substring(0, 8)}]: https - Existing session being used`);
|
||||||
}
|
}
|
||||||
const session = getSession(game, req.session);
|
const session = getSession(game, req.session);
|
||||||
console.log(`${session.id}: - load game via http`);
|
|
||||||
saveGame(game);
|
saveGame(game);
|
||||||
return res.status(200).send(getFilteredGameForPlayer(game, session));
|
return res.status(200).send(getFilteredGameForPlayer(game, session));
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user