-
Notifications
You must be signed in to change notification settings - Fork 1
/
sMain.js
62 lines (50 loc) · 1.71 KB
/
sMain.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const express = require('express');
const app = express();
const nPortNumber = process.env.PORT || 3000;
const server = app.listen(nPortNumber, () => console.log(`Listening at port: ${nPortNumber}`))
const socketio = require('socket.io');
const io = socketio(server);
app.use ('/', express.static('public'))
io.sockets.on('connection', OnNewConnection)
const gameCacheObj = require("./sGameCache.js");
const mainMenuObj = require("./sMainMenu.js");
const gameObj = require("./sGame.js");
mainMenuObj.Init(io, gameCacheObj, gameObj);
gameObj.Init(io, gameCacheObj, mainMenuObj);
function OnNewConnection(socket) {
const strFullUrl = socket.handshake.headers.referer;
const strBaseUrl = ParseSubUrl (strFullUrl, socket.handshake.headers.host);
if (strBaseUrl === "" || strBaseUrl === "index.html")
{
mainMenuObj.OnNewConnection (socket);
}
else if (strBaseUrl === "game.html")
{
gameObj.OnNewConnection (socket);
}
else{
console.log ("Unknown Url: " + strFullUrl);
}
}
//////////
//Eg:
//strFull: localhost:3000/game.html?a=1
//strDomain: localhost:3000
//Ret value: game.html
function ParseSubUrl (strFullUrl, strDomain)
{
let nIndex = strFullUrl.indexOf (strDomain);
if (nIndex == -1) { console.log ("Main: Parse Error 1: " + strFullUrl); return; }
nIndex += strDomain.length + 1; //This should point to index of game.html?a=1
const strSubUrlWithVar = strFullUrl.slice (nIndex, strFullUrl.length);
const nIndexVarStart = strSubUrlWithVar.indexOf ("?");
if (nIndexVarStart === -1)
{
return strSubUrlWithVar;
}
else
{
const strBaseUrl = strSubUrlWithVar.slice (0, nIndexVarStart);
return strBaseUrl;
}
}