-
Notifications
You must be signed in to change notification settings - Fork 0
/
WSServer.js
120 lines (91 loc) · 3.18 KB
/
WSServer.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
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
/* WSServer.js
FPI, 2021-09-15
*/
const http = require('http');
const WebSocketServer = require('websocket').server;
class WSServer {
/*
*/
static log( funcName, txt, level) {
console.log( new Date().yyyymmddhhmmsslll() + ` > WSServer > ${funcName} > ${txt}`);
}
/* wsHttpPort: 9081 ?
*/
static Initialize( wsHttpPort, OnMessageHandler, OnDisconnect, callBackInitialized) {
const funcName = "Initialize";
WSServer.wsHttpPort = wsHttpPort;
WSServer.OnMessageHandler = OnMessageHandler;
WSServer.OnDisconnect = OnDisconnect;
// clients wevSockets
WSServer.connections = [ ];
WSServer.log( funcName, "WebSocketServer http Server...", "info");
var _webSocketHttpServer = http.createServer( function(request, response) {
// process HTTP request. Since we're writing just WebSockets
// server we don't have to implement anything.
});
_webSocketHttpServer.listen( WSServer.wsHttpPort, (err) => {
if (err) {
WSServer.log( funcName, `_webSocketHttpServer.listen > something bad happened > ${err}`, 'warn')
return ;
}
WSServer.log( funcName, `_webSocketHttpServer.listen > server is listening on ${WSServer.wsHttpPort}`, 'info');
if (callBackInitialized) callBackInitialized(undefined);
});
// create the server
var _wsServer = new WebSocketServer({
httpServer: _webSocketHttpServer
});
// WebSocket server
_wsServer.on('request', function(request) {
let connection = request.accept( null, request.origin);
// declare un nouveau client connecté
let index = WSServer.connections.push( connection) - 1;
let context = {
indexWsClient: index,
remoteAddress: connection.socket._peername.address,
port: connection.socket._peername.port
};
WSServer.log( funcName, `_wsServer.accept new connection ${JSON.stringify( context)}`);
context.connection = connection;
/**/
// This is the most important callback for us, we'll handle
// all messages from users here.
connection.on( 'message', function(message) {
// WSServer.OnMessageHandler( connection, message);
WSServer.OnMessageHandler( context, message);
});
/**/
connection.on('close', function(connection) {
// recopie
// context.origin = connection.orign;
// pour minimiser les logs
context.connection = undefined;
WSServer.log( funcName, `_wsServer.close connection ${JSON.stringify( context)}`);
WSServer.OnDisconnect( context );
// remove user from the list of connected clients
WSServer.connections.splice( context.indexWsClient, 1);
});
});
}
/*
*/
static BroadcastToWsClients( jsnMessage) {
const funcName = "BroadcastToWsClients";
// s'il n'y a pas besoin de WSServer ou s'il n'y a aucun client
if ( ! WSServer.connections
|| WSServer.connections.length == 0) {
// WSServer.log( funcName, `no client connected`, 'warn');
} else {
for (let i=0; i < WSServer.connections.length; i++) {
WSServer.BroadcastToWsClient( WSServer.connections[i], jsnMessage);
}
}
}
/*
*/
static BroadcastToWsClient( connection, jsnMessage) {
let strMsg = JSON.stringify( jsnMessage);
connection.sendUTF(strMsg);
}
}
module.exports = WSServer;