/
TCPClientServer.ts
136 lines (115 loc) · 3.87 KB
/
TCPClientServer.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import ConnectionManager from './ConnectionManager';
import TCPClientSession, { MockWebSocket } from './TCPClientSession';
import Message from '../message/Message';
import Msg_Auth from '../message/Msg_Auth';
// import TCPClientMonitor from './TCPClientMonitor';
import WebSocket = require('ws');
const fs = require('fs');
const https = require('https');
export default class TCPClientServer {
public socketServer: WebSocket.Server;
public nextClientID: number;
// public TCP_m: TCPClientMonitor;
public clients: Map<TCPClientSession, WebSocket | MockWebSocket> = new Map<TCPClientSession, WebSocket | MockWebSocket>();
public connectionManager: ConnectionManager;
public host: string;
public port: number;
constructor(port: number, connectionManager: ConnectionManager) {
this.port = port;
this.connectionManager = connectionManager;
this.start();
}
// this.clients.forEach((socket: WebSocket, client: TCPClientSession) => {
//
// });
public restart(): void {
this.clients.forEach((socket: WebSocket, client: TCPClientSession) => {
client.dispose();
});
this.clients = new Map<TCPClientSession, WebSocket>();
}
public start(): void {
var processRequest = function( req: any, res: any ) {
res.writeHead(200);
res.end("OK\n");
};
var cfg = {
ssl: false,
port: 9696,
//FIXME: the path should be relative
ssl_key: "certs/key.pem",
ssl_cert: "certs/certificate.pem",
};
var httpsServer = https.createServer({
// providing server with SSL key/cert
key: fs.readFileSync( cfg.ssl_key ),
cert: fs.readFileSync( cfg.ssl_cert )
}, processRequest ).listen( cfg.port );
console.log(`SocketServer: starting server: host ${this.host}, port ${this.port}`);
this.socketServer = new WebSocket.Server({
server: httpsServer,
});
// this.socketServer = new WebSocket.Server({
// port: 9696
// });
this.socketServer.on('connection', (socket: any, req: any) => { //FIXME: WebSocket
console.log(`SocketServer: on connection ${req.headers.host}`);
var terms = req.headers.host.split(':');
socket.host = terms[0];
socket.port = Number(terms[1]);
this.onConnection(socket);
});
this.socketServer.on('error', (error: any) => {
console.log(`SocketServer: on error: `, error);
// this.killServer();
});
}
onConnection(socket: WebSocket | MockWebSocket): TCPClientSession {
let client: TCPClientSession = new TCPClientSession(this, socket);
// console.log(`${client.ip} : ${client.port} connected to the server.`);
this.clients.set(client, socket);
let authMsg: Msg_Auth = new Msg_Auth({
command: 'connected'
});
client.sendMessage(authMsg);
return client;
}
addClientSession(clientSession: TCPClientSession, socket: WebSocket | MockWebSocket): void {
this.clients.set(clientSession, socket);
}
removeClientSession(clientSession: TCPClientSession): void {
this.clients.delete(clientSession);
clientSession.dispose();
}
// removeClientSessionWithSocket(socket: WebSocket | MockWebSocket): void {
// this.clients.forEach((testSocket: WebSocket, clientSession: TCPClientSession) => {
// if (socket == testSocket) {
// this.clients.delete(clientSession);
// clientSession.dispose();
// }
// });
// }
public broadcastMessage(message: Message): void {
this.clients.forEach((socket: WebSocket, clientSession: TCPClientSession) => {
clientSession.sendMessage(message);
});
}
private killServer(): void {
try {
this.socketServer.close();
console.log("Server Stopped");
} catch (err) {
console.log("Error while stopping Server:");
console.log(err);
}
}
public removeTCPClient(client: TCPClientSession): void {
console.log(`Removing client: ${client.userUUID}`);
client.dispose();
this.clients.delete(client);
}
public timeoutTCPClient(client: TCPClientSession): void {
console.log("Timing out client: " + client.ip + ":" + client.port);
this.clients.delete(client);
}
}