-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
57 lines (38 loc) · 1.14 KB
/
app.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
const express = require('express');
const socket = require('socket.io');
// Setup Express App
const app = express();
// Static file Middelware
app.use(express.static('public'));
// Listen to Port
const port = process.env.PORT || 3000;
const server = app.listen(port, (req,res)=>{
console.log('Listening to port ', port);
});
// Setup Socket
const io = socket(server);
// console.log(io);
let users = {};
io.on('connection', (socket)=>{
console.log(users);
console.log('Socket connection made ',socket.id);
// Handle Join event
socket.on('join', (handle)=>{
users[socket.id]=handle;
socket.broadcast.emit('join', handle);
});
// Handle typing event
socket.on('typing', (data)=>{
socket.broadcast.emit('typing', data);
});
// Handle chat event
socket.on('sendMsg', (data)=>{
// io.sockets.emit('recieveMsg', data);
socket.broadcast.emit('recieveMsg', data);
});
// Handle leave event
// socket.on('disconnect', (message)=>{
// socket.broadcast.emit('disconnect', users[socket.id]);
// delete users[socket.id];
// });
});