-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
44 lines (33 loc) · 1.22 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
// load express
let express = require("express");
// create an app
let app = express();
// define the port where client files will be provided
let port = process.env.PORT || 3000;
// start to listen to that port
let server = app.listen(port);
// print the link in the terminal
console.log("running server on http://localhost:" + port);
// provide static access to the files
// in the "public" folder
app.use(express.static("public"));
// load socket library
let serverSocket = require("socket.io");
// create a socket connection
let io = serverSocket(server);
// define which function should be called
// when a new connection is opened from client
io.on("connection", newConnection);
// callback function: the paramenter (in this case socket)
// will contain all the information on the new connection
function newConnection(newSocket) {
// log the connection in terminal
console.log("new connection:", newSocket.id);
// tell to all the others that a new user connected
newSocket.on("mouse", incomingMouseMessage);
// callback function run when the "mouse" message is received
function incomingMouseMessage(dataReceived) {
// send it to all the clients
newSocket.broadcast.emit("mouseBroadcast", dataReceived);
}
}