-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
50 lines (44 loc) · 1.21 KB
/
server.js
File metadata and controls
50 lines (44 loc) · 1.21 KB
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
let express = require('express');
let app = express();
let host = 4000
let server = app.listen(host)
//Storing the latest image in img_cache
const img_cache = [];
//storing the id of the person who added the image
var connect_id=undefined;
app.use(express.static('public'));
console.log("Socket server is running on localhost:" + host)
let socket = require('socket.io')
let io = socket(server);
io.sockets.on('connection', newConnection)
function newConnection(socket){
console.log("conn: ",socket.id);
socket.on('mouse', mouseMsg);
socket.on('load_done',()=>{
if(img_cache.length>0){
socket.emit('main',img_cache[0]);
console.log("sent");
}
});
socket.on('disconnect',()=>{
console.log(socket.id,"got disconnected");
if(socket.id===connect_id){
socket.broadcast.emit('revamp');
img_cache.pop();
console.log(socket.id,"disconnected");
}
});
function mouseMsg(data) {
socket.broadcast.emit('mouse', data);
console.log(data);
}
socket.on('main',mainimg);
function mainimg(data){
console.log("In server");
img_cache.pop();
img_cache.push(data);
connect_id = socket.id;
console.log(connect_id,"the person who changed the image");
socket.broadcast.emit('main',img_cache[0]);
}
}