-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathwebsocket-server.js
38 lines (34 loc) · 1.18 KB
/
websocket-server.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
// Simple websocket server demo
const response = require("ringo/jsgi/response");
const arrays = require("ringo/utils/arrays");
const httpServer = require("ringo/httpserver");
const connections = [];
const app = (req) => {
return response.static(module.resolve("html/websocket.html"), "text/html");
};
const onConnect = (conn) => {
connections.push(conn);
console.info("Opening connection, " + connections.length + " open");
conn.addListener("text", message => {
connections.forEach(conn => conn.send(message));
console.info("Sending message");
});
conn.addListener("close", () => {
arrays.remove(connections, conn);
console.info("Closing connection, " + connections.length + " remaining");
})
};
if (require.main == module) {
httpServer.build()
// enable sessions with a custom node name
// serve application
.serveApplication("/", app)
// add websocket - this must be called after serveApplication
// as it operates on the current context of the builder
.addWebSocket("/websocket", onConnect)
.http({
"port": 8080
})
// start up the server
.start();
}