-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (62 loc) · 1.79 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var express = require('express');
var app = express(); // express server
var http = require('http').createServer(app); // http server
var WebSocket = require('ws').Server // websocket server
// var io = require('socket.io')(http);
var path = require('path');
var bodyParser = require("body-parser");
var port = process.env.PORT || 8090;
var _scene = 0;
// static html
// app.use(express.static('public'));
app.use(express.static(path.join(__dirname, 'public')));
// configure body-parser as middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// server setup
// listen for http connections
http.listen(port, () => {
console.log('everyday-windows app listening on port ', port);
});
// websocket setup
wss = new WebSocket({ server: http });
wss.on('connection', function(ws_client){
console.log("user connected");
// error handling
ws_client.on('error', function(err){
console.log("error: " + err);
});
// disconnection
ws_client.on('close', function(){
console.log("user disconnected");
});
// message handling
ws_client.on('message', function(msg){
// check if the values are valid/useful
var intComing = parseInt(msg);
if(intComing != NaN && intComing>=0 && intComing<=5){
_scene = parseInt(msg);
broadcast(_scene);
console.log("change scene broadcast: " + _scene);
}
});
});
function broadcast(msg){
wss.clients.forEach(function each(client) {
client.send(msg);
});
}
// socket.io setup -- deprecated
/*
io.on('connection', function(socket){
console.log("user connected");
socket.on('disconnect', function(){
console.log("user disconnected");
});
socket.on('select', function(mode){
_scene = mode;
io.emit('change scene', _scene);
console.log("received selection: " + _scene);
});
});
*/