-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
41 lines (31 loc) · 961 Bytes
/
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
/// Load http module
let http = require("http");
let PORT = process.env.PORT;
console.log(PORT);
// Load express module
let express = require("express");
let app = express();
// Create server
// Hook it up to listen to the correct PORT
let server = http.createServer(app).listen(PORT);
// Point my app at the public folder to serve up the index.html file
app.use(express.static("public"));
// Load the socket.io functionality
// Hook it up to the web server
let io = require("socket.io")(server);
// Listen for connections
io.on(
"connection",
// Callback function on connection
// Comes back with a socket object
function(socket) {
console.log("HELLO", socket.id);
// This connected socket listens for incoming messages called 'data'
socket.on('data', function(data){
// Log the data that came in
console.log(data);
// Send it back out to everyone
io.emit('data', data);
});
}
);