forked from Jasonette/agent.websockets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
35 lines (35 loc) · 1.05 KB
/
app.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
const WebSocket = require("ws")
const WebSocketServer = WebSocket.Server
const http = require("http")
const express = require("express")
const app = express()
const port = process.env.PORT || 5000
const jason = require('./app.json')
app.get("/jason", function(req, res) {
// fill in the agent url with the current server url
jason.$jason.head.agents.ws.url = req.protocol + '://' + req.get('host');
res.json(jason)
})
app.use(express.static(__dirname + "/"))
const server = http.createServer(app)
server.listen(port)
console.log("http server listening on %d", port)
const wss = new WebSocketServer({server: server})
const noop = () => {}
console.log("websocket server created")
wss.on('error', function () {});
wss.on('connection', function (ws) {
ws.on('error', function () {});
ws.on('message', function(m) {
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(m);
}
});
});
setInterval(() => {
wss.clients.forEach((client) => {
client.ping(noop)
});
}, 1000);
})