-
Notifications
You must be signed in to change notification settings - Fork 0
/
server3.js
77 lines (54 loc) · 1.58 KB
/
server3.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
74
75
76
77
var app = require('express')();
var server = require('http').Server(app);
var WebSocketServer = require('websocket').server;
var cors = require('cors')(); //permitir requisicao de outras origens
port=15010;
/* https://enable-cors.org/server_expressjs.html */
//app.use(cors());
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
io = new WebSocketServer({
httpServer: server
});
let CLIENTS = [];
io.on('request', function (request) {
var connection = request.accept(null, request.origin);
console.log('cliente conectado');
console.log(request.key);
CLIENTS.push(connection);
connection.on('message', function(message){
if (message.type === 'utf8'){
try {
console.log(message.utf8Data);
//sendAll(message.utf8Data);
// ui = message.utf8Data;
//connection.send('server: ' + ui , request.key);
CLIENTS.forEach(s => s.send(message.utf8Data));
}catch (e){
console.log("ERRO");
console.log(e.message);
}
}
});
connection.on('close', function(e){
console.log('onclose');
});
connection.on('disconnect', function(e){
console.log('ondisconect');
});
function sendAll (message) {
for (var i=0; i<CLIENTS.length; i++) {
//CLIENTS[i].send(key);
CLIENTS[i].send("Message: " + message);
}
}
});
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html')
})
server.listen(port, function () {
console.log('Server listening at port %d', port);
});