-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
69 lines (58 loc) · 2.04 KB
/
index.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
import Server from './server';
import WSService from './WSService';
import Client from './Client';
import { MessageEvent } from 'ws';
import fs from 'fs';
const serv = new Server();
const websocketMessageProcessor = (wsService: WSService, event: MessageEvent) => {
const { data } = event;
console.log(data);
// Binary type = 'arrayBuffer'
if (data instanceof ArrayBuffer) {
console.log('Handling binary data as ArrayBuffer');
}
// Binary type = 'blob'
if (data instanceof Buffer) {
console.log('Handling binary data as "Buffer"');
}
// Plain Text string was sent
if (typeof data === 'string') {
// Try Parse JSON string
try {
var jsonObject = JSON.parse(data);
console.log('Received JSON string');
} catch (e) {
console.log('Received standard string');
}
}
// Repeat the Message 100x times (just for fun)
// for (let index = 0; index < 100; index++) {
wsService.broadcast(data);
// }
};
const RelayWSService = new WSService('RelayWebsocketService', 8080, 'arraybuffer', websocketMessageProcessor);
serv.add(RelayWSService);
const clientRequestHandler = (request, response) => {
switch(request.method) {
case 'GET':
switch(request.url){
case '/':
// Serves the Websocket Client that connects to the WS Server on 8080
// @todo: use a templating engine to add in configured WS server IP/Port automatically
response.end(fs.readFileSync('./client/index.html'));
break;
default:
response.end('Bad Request');
break;
}
break;
default:
response.end('Bad Request');
break;
}
};
const WebClient = new Client('WebClient', 3000, clientRequestHandler);
serv.add(WebClient);
//serv.add(new SomeOtherAPI());
serv.start();// starts all services in this server
// only stop server by closing node process