-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
51 lines (39 loc) · 1.46 KB
/
main.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
/*
* CursusDB Observer to WS Example with Express
* ******************************************************************
* Originally authored by Alex Gaetano Padula
*/
import Observer from 'cursusdb-observer-node' // Observer for Node.js required
import express from 'express' // Express
import { WebSocketServer } from 'ws'; // Ws for websocket server
import path from 'path' // for paths
const wss = new WebSocketServer({ port: 8081 }) // Create websocket server on port 8081
const app = express(); // Setup express under app constant
const port = process.env.PORT || 8080 // default to port 8080 for express
let wsConnections = [] // Current websocket connections
let ob = new Observer("yoursharedkey")
if (ob.sharedKey !== undefined) {
ob.Start() // Start listening
ob.events.on('event', (data) => { // On observer events relay to websocket
wsConnections.forEach((ws) => {
ws.send(data);
})
})
}
// get '/' return index.html
app.get('/', function(req, res) {
res.sendFile(path.join(path.resolve(), '/index.html'))
});
// On websocket server connection
wss.on('connection', function connection(ws) {
ws.on('error', console.error);
wsConnections = wsConnections.concat(ws)
});
wss.on('close', function close() {
wss.clients.forEach(function each(ws) {
ws.terminate();
});
wsConnections = []
});
app.listen(port) // Start listening on 8080
console.log('HTTP Server started at http://localhost:' + port)