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;
|
||||
peers[name] = {
|
||||
local: true,
|
||||
muted: true,
|
||||
attributes: {
|
||||
}
|
||||
};
|
||||
|
@ -80,6 +80,7 @@ const sessionParser = session({
|
||||
});
|
||||
|
||||
app.use(sessionParser);
|
||||
app.locals.sessionParser = sessionParser;
|
||||
|
||||
const index = require("./routes/index");
|
||||
|
||||
|
@ -19,7 +19,7 @@ const debug = {
|
||||
audio: false,
|
||||
get: true,
|
||||
set: true,
|
||||
update: true
|
||||
update: false//true
|
||||
};
|
||||
|
||||
let gameDB;
|
||||
@ -496,7 +496,7 @@ const loadGame = async (id) => {
|
||||
if (game) {
|
||||
try {
|
||||
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));
|
||||
} catch (error) {
|
||||
console.log(`Attempting to load backup from games/${id}.bk`);
|
||||
@ -2822,7 +2822,7 @@ const part = (peers, session, id) => {
|
||||
|
||||
|
||||
const getName = (session) => {
|
||||
return session.name ? session.name : "Unnamed";
|
||||
return session.name ? session.name : session.id;
|
||||
}
|
||||
|
||||
const saveGame = async (game) => {
|
||||
@ -2989,21 +2989,34 @@ const sendWarning = (session, 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 gameId = 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)}]`;
|
||||
ws.id = short;
|
||||
|
||||
console.log(`${short}:${gameId} - New connection from client.`);
|
||||
console.log(`${short}: Game ${gameId} - New connection from client.`);
|
||||
if (!(id in audio)) {
|
||||
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 {
|
||||
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
|
||||
@ -3121,7 +3134,7 @@ router.ws("/ws/:id", async (ws, req) => {
|
||||
break;
|
||||
|
||||
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);
|
||||
break;
|
||||
|
||||
@ -3182,7 +3195,7 @@ router.ws("/ws/:id", async (ws, req) => {
|
||||
update.players = game.players;
|
||||
break;
|
||||
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;
|
||||
break;
|
||||
case 'timestamp':
|
||||
@ -3276,13 +3289,13 @@ router.ws("/ws/:id", async (ws, 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) {
|
||||
clearTimeout(session.keepAlive);
|
||||
}
|
||||
session.keepAlive = setTimeout(() => { ping(session); }, 2500);
|
||||
});
|
||||
};
|
||||
|
||||
const debugChat = (game, preamble) => {
|
||||
preamble = `Degug ${preamble.trim()}`;
|
||||
@ -3695,7 +3708,6 @@ const createGame = (id) => {
|
||||
/* Look for a new game with random words that does not already exist */
|
||||
while (!id) {
|
||||
id = randomWords(4).join('-');
|
||||
console.log(`Looking for ${id}`);
|
||||
try {
|
||||
/* If file can be read, it already exists so look for a new name */
|
||||
accessSync(`games/${id}`, fs.F_OK);
|
||||
@ -3704,6 +3716,7 @@ const createGame = (id) => {
|
||||
break;
|
||||
}
|
||||
}
|
||||
console.log(`${info} creating ${id}`);
|
||||
|
||||
const game = {
|
||||
id: id,
|
||||
@ -3737,9 +3750,11 @@ router.post("/", (req, res/*, next*/) => {
|
||||
const game = createGame();
|
||||
if (!req.session.player_id) {
|
||||
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);
|
||||
console.log(`${session.id}: - load game via http`);
|
||||
saveGame(game);
|
||||
return res.status(200).send(getFilteredGameForPlayer(game, session));
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user