-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
36 lines (30 loc) · 1020 Bytes
/
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
const app = require('express')();
const server = require('http').createServer(app);
const io = require('socket.io')(server, {
cors: {
origin: '*',
methods: ['GET', "POST"]
}
});
var connected_users = {}
var msgs = [
{ username: 'hema', content: 'hey thi test', socket_id: '3hsk12ian' },
{ username: 'john', content: 'hey this is test', socket_id: '3hsk1' },
{ username: 'john', content: 'hey this is test', socket_id: '3hsk12ian' },
{ username: 'john', content: 'hey this is test', socket_id: '3hsk1' },
]
io.on('connection', (socket) => {
console.log('Connected')
socket.emit('getSocketId', socket.id)
socket.emit('getMsgs', msgs)
socket.on('sendCreds', (msg) => {
connected_users[socket.id] = msg
console.log(connected_users)
})
socket.on('sendMsg', (data) => {
msgs.push(data)
console.log('added msg')
io.emit('updateMsgs', msgs)
})
})
server.listen(5000, () => console.log('server is running on 5000'))