forked from ohvitorino/dumper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
49 lines (36 loc) · 1.19 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
import express from 'express';
import http from 'http';
import WebSocket from 'ws';
import bodyParser from 'body-parser';
import * as fs from "fs";
const PORT = process.env.PORT || 8000
const app = express();
app.use(bodyParser.text({type:"*/*"}))
//initialize a simple http server
const server = http.createServer(app);
//initialize the WebSocket server instance
const wss = new WebSocket.Server({ server });
wss.on('connection', (ws: WebSocket) => {
//connection is up, let's add a simple simple event
ws.on('message', (message: string) => {
//log the received message and send it back to the client
console.log('received: %s', message);
});
//send immediatly a feedback to the incoming connection
ws.send('Connected');
});
//start our server
server.listen(PORT, () => {
console.log(`Server started on port ${PORT} 💥`);
});
app.get('/', (req, res) => {
fs.readFile(__dirname + '/templates/index.html', 'utf8', (err, text) => {
res.send(text.replace('{{PORT}}', PORT.toString()));
});
})
app.post('/', (req: express.Request, res: express.Response) => {
wss.clients.forEach((ws: any) => {
ws.send(req.body)
});
res.send('ok');
})