-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
64 lines (52 loc) · 1.54 KB
/
server.js
File metadata and controls
64 lines (52 loc) · 1.54 KB
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
56
57
58
59
60
61
62
63
64
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const socketio = require('socket.io');
const io = socketio(server);
let users = {
'aakash': 'akki'
}
app.use('/', express.static(__dirname + '/public'))
server.listen(4664, () => {
console.log('server started on http://localhost:4664');
})
let socketmap={};
function helper( s , u)
{
s.join(u);
s.emit('logged_in');
socketmap[s.id]=u;
console.log(socketmap)
}
io.on('connection', (socket) => {
console.log('connect id:' + socket.id);
// socket.on('msg_send',(data)=>{
// io.emit('msg_rcvd',data) // send to everyone
// socket.broadcast.emit('msg_rcvd',data) send as broadcast, show only on others
// socket.emit() just to that particular socket
socket.on('login', (data) => {
if (users[data.username]) {
if (users[data.username] == data.password) {
helper(socket,data.username)
}
else {
socket.emit('login_failed');
}
}
else {
users[data.username] = data.password;
helper(socket,data.username)
}
// console.log(users);
})
socket.on('msg_send', (data) => {
data.from=socketmap[socket.id]
if (data.to) {
io.to(data.to).emit('msg_rcvd', data);
}
else {
socket.broadcast.emit('msg_rcvd', data);
}
})
})