-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
82 lines (72 loc) · 2.29 KB
/
index.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const { Server: WebSocket } = require("ws");
const crypto = require("crypto");
// Start listening websocket on port
const port = 80;
const wss = new WebSocket({ port: port });
wss.on('listening', () => {
console.log(`Matchmaker started listening on port ${port}`);
});
wss.on('connection', async (ws) => {
if (ws.protocol.toLowerCase().includes("xmpp")) {
return ws.close();
}
// create hashes
const ticketId = crypto.createHash('md5').update(`1${Date.now()}`).digest('hex');
const matchId = crypto.createHash('md5').update(`2${Date.now()}`).digest('hex');
const sessionId = crypto.createHash('md5').update(`3${Date.now()}`).digest('hex');
// you can use setTimeout to send the websocket messages at certain times
setTimeout(Connecting, 200/* Milliseconds */);
setTimeout(Waiting, 1000); // 0.8 Seconds after Connecting
setTimeout(Queued, 2000); // 1 Second after Waiting
setTimeout(SessionAssignment, 6000); // 4 Seconds after Queued
setTimeout(Join, 8000); // 2 Seconds after SessionAssignment
function Connecting() {
ws.send(JSON.stringify({
"payload": {
"state": "Connecting"
},
"name": "StatusUpdate"
}));
}
function Waiting() {
ws.send(JSON.stringify({
"payload": {
"totalPlayers": 1,
"connectedPlayers": 1,
"state": "Waiting"
},
"name": "StatusUpdate"
}));
}
function Queued() {
ws.send(JSON.stringify({
"payload": {
"ticketId": ticketId,
"queuedPlayers": 0,
"estimatedWaitSec": 0,
"status": {},
"state": "Queued"
},
"name": "StatusUpdate"
}));
}
function SessionAssignment() {
ws.send(JSON.stringify({
"payload": {
"matchId": matchId,
"state": "SessionAssignment"
},
"name": "StatusUpdate"
}));
}
function Join() {
ws.send(JSON.stringify({
"payload": {
"matchId": matchId,
"sessionId": sessionId,
"joinDelaySec": 1
},
"name": "Play"
}));
}
});