forked from rramachand21-zz/nodejsappservicelinux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hostingstart.js
32 lines (25 loc) · 815 Bytes
/
hostingstart.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
const express = require('express');
const http = require('http');
const url = require('url');
const WebSocket = require('ws');
const app = express();
app.use(express.static('public'));
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
});
const server = http.createServer(app);
const wss = new WebSocket.Server({ server, perMessageDeflate: false });
wss.broadcast = function broadcast(data) {
wss.clients.forEach(function each(client) {
client.send(data);
});
};
wss.on('connection', function(ws) {
ws.on('message', function(msg) {
data = JSON.parse(msg);
if (data.message) wss.broadcast('<strong>' + data.name + '</strong>: ' + data.message);
});
});
server.listen(process.env.PORT, function() {
console.log('listening on ');
});