/
achieve_ws.js
72 lines (65 loc) · 2.56 KB
/
achieve_ws.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
const achieve = require('achieve');
const WebSocket = require('ws');
const url = require('url');
const fs = require('fs');
const rooms = require('achieve_rooms');
// You can set place your app components anywhere, by setting the root path.
// achieve.setAppPath("C:/webapps");
/*
// To use HTTPS or HTTP2, you need to have certificates.
// The options object provides certificate paths and non-default ports
const options = {
key: fs.readFileSync('c:/mycerts/privkey.pem'),
ca: fs.readFileSync('c:/mycerts/chain.pem'),
cert: fs.readFileSync('c:/mycerts/fullchain.pem'),
httpsPort: 8440, // outside range example: "httpsPort: 0" defaults
http2Port: 8441
};
*/
// Create servers for https, http, and http2
// Optional ... if you're just getting started and don't have certificates, just start http with
// achieve.listen() which defaults to port 80 or achieve.listen(port number)
// Note that websockets are not yet supported on HTTP2
const sserver = achieve.slisten(options);
const server = achieve.listen(8989); // backwards compatable ... if arg is Number, http is run on that port
const server2 = achieve.listen2(options);
// Create Websocket Servers
const ws1 = new WebSocket.Server({ noServer: true });
const wss1 = new WebSocket.Server({ noServer: true });
// Create handlers (call-backs) for insecure websockets
ws1.on('connection', function connection(ws) {
console.log("non-secure connection");
ws.on('message', function incoming(data) {
console.log("ns broadcast: " + data);
rooms.broadcast(ws,data);
});
ws.on('close', function close() {
rooms.remove(ws);
});
});
server.on('upgrade', function upgrade(request, socket, head) {
console.log("ns upgrade");
let pathname = url.parse(request.url).pathname;
ws1.handleUpgrade(request, socket, head, function done(ws) {
let room = rooms.joinRoom(ws,request.url);
ws1.emit('connection', ws, request);
});
});
// Create handlers (call-backs) for secure websockets
wss1.on('connection', function connection(ws, request) {
console.log("secure connection");
ws.on('message', function incoming(data) {
// console.log("message: " + data);
rooms.broadcast(ws,data);
});
ws.on('close', function close() {
rooms.remove(ws);
});
});
sserver.on('upgrade', function upgrade(request, socket, head) {
let pathname = url.parse(request.url).pathname;
wss1.handleUpgrade(request, socket, head, function done(ws) {
let room = rooms.joinRoom(ws,request.url);
wss1.emit('connection', ws, request);
});
});