-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
34 lines (21 loc) · 987 Bytes
/
Copy pathserver.js
File metadata and controls
34 lines (21 loc) · 987 Bytes
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
console.log("script loaded");
let express = require("express");
let app = express();
let port = 3000;
let server = app.listen(port); //listening to a specific port
app.use(express.static("public")); //use this folder (public)
console.log('running server on http://localhost:' + port);
let serverSocket = require('socket.io');
let io = serverSocket(server); //connection point between server service and clients
//socket io cheatsheet https://socket.io/docs/v3/emit-cheatsheet/ version 4
io.on("connection", newConnection);
//we expect one parameter, pass a new information during the connection
//when the pc is connect to our service, print out a line of code
function newConnection(newSocket) {
console.log(newSocket.id);
newSocket.on("mouse", mouseReceived);
function mouseReceived(dataReceived) {
console.log(dataReceived);
newSocket.broadcast.emit("mouseBroadcast", dataReceived); //send to all the other connections. Emit a message
}
}