-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
43 lines (34 loc) · 1.15 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
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require('socket.io');
const io = new Server(server);
const port = process.env.PORT || 3000;
app.use(express.static(__dirname + '/assets'));
app.get('/',(req,res) => {
res.sendFile(__dirname + '/index.html');
});
var users = {};
io.on('connection', (socket) => {
socket.on('join', function(username) {
users[socket.id] = username;
socket.emit('update',`Hi 👋 ${username}, welcome to this simple chat app.`);
io.emit('update', `${username} has joined the chat.`);
io.emit('update-users',users);
});
socket.on('chat message',(msg) => {
io.emit('chat message', msg, users[socket.id]);
});
socket.on('typing',() => {
io.emit('typing',`${users[socket.id]} is typing a message ...`);
})
socket.on('disconnect',function(){
io.emit('update',`${users[socket.id]} has left the chat.`);
delete users[socket.id];
io.emit('update-users', users);
});
});
server.listen(port, ()=> {
console.log('listening on *:'+port);
});