-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpress_server.js
45 lines (35 loc) · 1.32 KB
/
express_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
var http = require('http');
var express = require('express');
var shortid = require('shortid');
var app = express();
var server = http.createServer(app);
// Pass a http.Server instance to the listen method
const io = require('socket.io')(http);
// The server should start listening
server.listen(3008);
// Register the index route of your app that returns the HTML file
app.get('/v2/users/709189', function(req, res) {
console.log("Request made")
res.send('Hello World!')
});
// Expose the node_modules folder as static resources (to access socket.io.js in the browser)
app.use('/static', express.static('node_modules'));
// Handle connection
server.on('connection', function(socket) {
socket.id = shortid.generate();
//socket.setTimeout(500)
socket.setKeepAlive(true);
console.log("A new connection was made by a client." + ` SOCKET ${ socket.id }`);
socket.on('end', function() {
console.log(`SOCKET ${ socket.id } END: other end of the socket sends a FIN packet`);
});
socket.on('timeout', function() {
console.log(`SOCKET ${ socket.id } TIMEOUT`);
});
socket.on('error', function(error) {
console.log(`SOCKET ${ socket.id } ERROR: ` + JSON.stringify(error));
});
socket.on('close', function(had_error) {
console.log(`SOCKET ${ socket.id } CLOSED. IT WAS ERROR: ` + had_error);
});
});