-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·59 lines (50 loc) · 1.2 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* UI5 WebSocket minimalistic chat example by Holger Schäfer
* using node.js and NPM package ws
*/
var express,
path,
WebSocketServer,
wss,
httpPort = process.env.PORT || 80,
wsPort = 8080,
app;
express = require("express");
path = require("path");
app = express();
// all environments
express.static.mime.default_type = "text/xml";
app.use(express.compress());
app.use(express.static(path.join(__dirname, "public")));
// development only
if (app.get("env") === "development") {
app.use(express.errorHandler());
}
// start web socket server
WebSocketServer = require("ws").Server;
wss = new WebSocketServer({
port: wsPort
});
wss.broadcast = function(data) {
"use strict";
for (var i in this.clients)
this.clients[i].send(data);
console.log("broadcasted: %s", data);
};
wss.on("connection", function(ws) {
"use strict";
ws.on("message", function(message) {
console.log("received: %s", message);
wss.broadcast(message);
});
ws.send(JSON.stringify({
user: "NODEJS",
text: "Hallo from server"
}));
});
// start http server
app.listen(httpPort, function() {
"use strict";
console.log("HTTP Server: http://localhost:" + httpPort);
console.log("WS Server: ws://localhost:" + wsPort);
});