-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
133 lines (96 loc) · 3.25 KB
/
Copy pathapp.js
File metadata and controls
133 lines (96 loc) · 3.25 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
'use strict';
// const express = require('express');
// const { Server, WebSocket } = require('ws');
// const PORT = process.env.PORT || 3000;
// const INDEX = '/index.html';
// const server = express()
// .use((req, res) => res.sendFile(INDEX, { root: __dirname }))
// .listen(PORT, () => console.log(`Listening on ${PORT}`));
// const wss = new Server({ server });
// wss.on('connection', (ws, req) => {
// // ws.id = wss.getUniqueID();
// const ip = req.socket.remoteAddress;
// console.log('Client connected with ip: ', ip);
// let clientsQuantity = 0;
// wss.clients.forEach(client => {
// if (client.OPEN === 1) {
// clientsQuantity++;
// }
// // console.log('Client.ID: ' + client.id)
// });
// console.log('Cantidad de clientes: ', clientsQuantity);
// ws.on('close', () => {
// clientsQuantity = 0;
// console.log('Client disconnected:', ip)
// });
// ws.on('message', function message(data, isBinary) {
// console.log('received: %s', data);
// wss.clients.forEach(function each(client) {
// if (client.readyState === WebSocket.OPEN) {
// client.send(data, { binary: isBinary });
// }
// });
// });
// });
const { WebSocketServer } = require('ws');
function heartbeat() {
this.isAlive = true;
}
const wss = new WebSocketServer({ port: 3000 });
wss.on('connection', function connection(ws, req) {
const ip = req.socket.remoteAddress;
console.log('Client connected with ip: ', ip);
ws.isAlive = true;
ws.on('pong', heartbeat);
ws.on('close', function close() {
// clearInterval(interval);
console.log('Client disconnected:', ip);
});
ws.on('message', function message(data) {
// const msg = data.toString();
// const msg = JSON.parse(data.toString());
console.log('received: %s', data);
wss.clients.forEach(client => {
client.send(data.toString());
})
});
});
const interval = setInterval(function ping() {
wss.clients.forEach(function each(ws) {
if (ws.isAlive === false) {
console.log('se desconecto: ');
return ws.terminate();
}
ws.isAlive = false;
ws.ping();
});
}, 2000);
// ws.on('message', function message(data) {
// // const msg = data.toString();
// // const msg = JSON.parse(data.toString());
// // console.log('received: %s', data);
// // wss.clients.forEach(client => {
// // client.send(data.toString());
// // })
// wss.clients.forEach(function each(client) {
// if (client.readyState === WebSocket.OPEN) {
// client.send(data, { binary: isBinary });
// }
// });
// });
// let on_off = true;
// setInterval(() => {
// wss.clients.forEach((client) => {
// // client.send(new Date().toTimeString());
// on_off = !on_off;
// const data = {
// on_off: on_off,
// power: true,
// temp: true,
// timer: true,
// mas: true,
// menos: true
// };
// client.send(JSON.stringify(data));
// });
// }, 1000);