-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (43 loc) · 1.51 KB
/
index.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
48
49
50
51
52
53
54
55
const express = require('express');
const http = require('http');
const {Server} = require('socket.io');
const app = express();
const server = http.createServer(app);
app.use(express.static("./public"));
const io = new Server(server);
let users = {};
io.on('connection',(socket)=>{
//assign default nickname
socket.nickname = `User${Math.floor(Math.random()*1000)}`;
users[socket.id] = socket.nickname;
//emit to all other users that someone has joined
io.emit('user joined',{user: socket.nickname, users: Object.values(users)});
socket.on('set nickname',(nickname)=>{
users[socket.id] = nickname;
socket.nickname = nickname;
io.emit('nickname changed',{id: socket.id, nickname});
})
socket.on('chat message',(msg)=>{
io.emit('chat message',msg);
})
//detect when a user is disconnected
socket.on('disconnect',()=> {
console.log(`User ${socket.id} disconnected`);
delete users[socket.id];
io.emit('user left',{user: socket.nickname, users: Object.values(users)});
})
//Functionality : Status of if a person is typing or not
socket.on('typing',()=> {
socket.broadcast.emit('typing',{user: socket.nickname});
})
//stop typing notification
socket.on('stop typing', () => {
socket.broadcast.emit('stop typing', { user: socket.nickname });
});
})
app.get("/", (req, res) => {
res.sendFile("./public/index.html");
})
server.listen(9000,()=>{
console.log("Server is running on port 9000")
})