-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
55 lines (45 loc) · 1.45 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
let express = require('express');
let app = express();
let http = require('http');
let https = require('https');
let server = http.createServer(app);
let io = require('socket.io').listen(server);
let users = [];
let connections = [];
server.listen(process.env.PORT || 3000);
console.log('Server running...');
app.get('/', (req, res) => {
res.sendFile(`${__dirname}/index.html`)
});
io.sockets.on('connection', client => {
connections.push(client);
console.log(`Connected: ${connections.length} sockets connected`);
const options = {
host: 'randomuser.me',
path: '/api',
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
};
https.get(options, function(res) {
//console.log("Got response: " + res.statusCode);
res.on("data", function(chunk) {
let tmp = chunk + '';
console.log('---------------------');
console.log(tmp);
//let jsonData = JSON.parse(tmp);
//console.log(jsonData);
});
}).on('error', function(e) {
//console.log("Got error: " + e.message);
});
client.on('disconnect', client => {
connections.splice(connections.indexOf(client), 1);
console.log(`Disconnected: ${connections.length} sockets still connected`);
});
client.on('send message', data => {
io.sockets.emit('new message', {msg: data});
console.log(data);
});
});