-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
45 lines (36 loc) · 1.23 KB
/
index.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
const SocketServer = require('ws').Server;
var express = require('express');
var connectedUsers = [];
//init Express
var app = express();
//init Express Router
var router = express.Router();
var port = process.env.PORT || 80;
//REST route for GET /status
router.get('/status', function(req, res) {
res.json({ status: 'App is running!' });
});
//connect path to router
app.use("/", router);
//add middleware for static content
app.use(express.static('static'))
var server = app.listen(port, function () {
console.log('node.js static, REST server and websockets listening on port: ' + port)
})
//if serving static app from another server/port, send CORS headers in response
//{ headers: {
//"Access-Control-Allow-Origin": "*",
// "Access-Control-Allow-Headers": "http://localhost:3000",
// "Access-Control-Allow-Methods": "PUT, GET, POST, DELETE, OPTIONS"
//} }
const wss = new SocketServer({ server });
//init Websocket ws and handle incoming connect requests
wss.on('connection', function connection(ws) {
console.log("connection ...");
//on connect message
ws.on('message', function incoming(message) {
console.log('received: %s', message);
connectedUsers.push(message);
});
ws.send('something');
});