-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreportService.js
55 lines (45 loc) · 1.22 KB
/
reportService.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
const express = require('express');
const http = require('http');
const WebSocket = require('ws');
const cors = require('cors')
const bodyParser = require('body-parser');
const app = express();
const port = 4202;
//initialize a simple http server
const server = http.createServer(app);
let body = "";
const wss = new WebSocket.Server({ server });
app.use(bodyParser.text({ type: 'text/plain' }));
app.use(bodyParser.text({ type: 'text/turtle' }));
app.use(function (err, req, res, next) {
console.error(err.stack);
});
app.get('/', cors(), (req, res) => {
res.send('Hello World!');
});
app.post('/test', (req, res) => {
body = req.body;
console.log(body);
res.send('');
});
app.post('/report', (req, res) => {
body = req.body;
console.log(body);
wss.clients.forEach(client => {
client.send(body);
});
res.send('');
});
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
ws.send(message);
});
ws.on("error", (err) => {
console.log("Caught flash policy server socket error: ");
console.log(err.stack);
});
});
server.listen(port, () => {
console.log(`Server started on port ${server.address().port} :)`);
});