-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (32 loc) · 1.04 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 path = require('path')
const port = process.env.PORT || 3000;
app.use('/', express.static(path.join(__dirname, 'public')));
const socketio = require('socket.io')
const io = socketio(server);
const users = {};
io.on('connection', (socket)=>{
console.log('connection established', socket.id)
socket.on('send-msg', (data)=>{ // listen to some event
// console.log(data)
// socket.emit('received-msg', {
// msg:data.msg,
// id : socket.id
// })
io.emit('received-msg', {
msg:data.msg,
id : socket.id,
username: users[socket.id]
})
})
socket.on('login', (data)=>{
console.log(data)
users[socket.id] = data.username // mapping the socket id with userName
})
})
server.listen(port, ()=>{
console.log('server connected at port', port);
})