-
Notifications
You must be signed in to change notification settings - Fork 0
/
index copy.ts
81 lines (71 loc) · 2.32 KB
/
index copy.ts
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
import {Category, Challenge, CTF} from './models';
/*
* WEBSOCKET
*/
const webSocketsServerPort = 8345;
const webSocketServer = require('websocket').server;
const http = require('http');
// Spinning the http server and the websocket server.
const server = http.createServer();
server.listen(webSocketsServerPort);
const wsServer = new webSocketServer({
httpServer: server
});
// Generates unique ID for every new connection
const getUniqueID = () => {
const s4 = () => Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
return s4() + s4() + '-' + s4();
};
// I'm maintaining all active connections in this object
const clients = {};
// I'm maintaining all active users in this object
const users = {};
const sendMessage = (json) => {
// We are sending the current data to all connected clients
Object.keys(clients).map((client) => {
clients[client].sendUTF(json);
});
}
const typesDef = {
USER_EVENT: "userevent",
}
wsServer.on('request', function(request) {
var userID = getUniqueID();
console.log((new Date()) + ' Recieved a new connection from origin ' + request.origin + '.');
// You can rewrite this part of the code to accept only the requests from allowed origin
const connection = request.accept(null, request.origin);
clients[userID] = connection;
console.log('connected: ' + userID + ' in ' + Object.getOwnPropertyNames(clients));
connection.on('message', function(message) {
if (message.type === 'utf8') {
const dataFromClient = JSON.parse(message.utf8Data);
const json = { type: dataFromClient.type };
if (dataFromClient.type === typesDef.USER_EVENT) {
users[userID] = dataFromClient;
json.data = { users };
}
sendMessage(JSON.stringify(json));
}
});
// user disconnected
connection.on('close', function(connection) {
console.log((new Date()) + " Peer " + userID + " disconnected.");
const json = { type: typesDef.USER_EVENT };
json.data = { users };
delete clients[userID];
delete users[userID];
sendMessage(JSON.stringify(json));
});
});
/*
* EXPRESS
*/
const express = require('express')
const app = express();
const expressPort = 9999
app.listen(expressPort, () => {
console.log(`Server listening at http://localhost:${expressPort}`);
});
app.get('/ctfs', (req, res) => {
res.send({...CTF.getAll()});
});