-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
48 lines (37 loc) · 1.12 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
45
46
47
//Node server which will handle socket io connections
const express = require('express');
const app=express();
const http=require('http').createServer(app);
const port=process.env.PORT || 8000;
http.listen(port,()=>
{
console.log(`Listening on port ${port}`);
})
app.use(express.static(__dirname + '/js'));
app.get('/',(req,res)=>
{
res.sendFile(__dirname + '/index.html')
});
const io = require('socket.io')(http);
const users={};
io.on('connection',socket =>
{
//If any new user joins, let other users connected to the server know!
socket.on('new-user-joinedd',name =>
{
// console.log("New user joined",name);
users[socket.id]=name;
socket.broadcast.emit('user-joinedd',name);
});
//if someone sends a message, broadcast it to other people
socket.on('sendd',message=>
{
socket.broadcast.emit('receivee',{message:message,name:users[socket.id]});
});
//if someone leaves the server, broadcast it to other peopl
socket.on('disconnect',message =>
{
socket.broadcast.emit('leavee',users[socket.id]);
delete users[socket.id];
});
})