-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
43 lines (26 loc) · 963 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
37
38
39
40
41
42
43
const express = require('express')
const socketio = require('socket.io')
const http = require('http')
app = express()
const server = http.createServer(app)
const io = socketio(server)
let idtousername = {}
io.on('connection', (socket) => {
socket.on('login', (data) => {
let socketid = socket.id;
idtousername[socketid] = data.username;
socket.join(data.username) //create room
})
socket.on('send-msg', (data) => {
if (data.message.startsWith('@')) {
let user = data.message.split(':')[0].substring(1);
let msg = data.message.split(':')[1].trim();
io.in(user).emit('new-msg', { message: msg }) //room name
} else
io.emit('new-msg', { message: data.message, username: idtousername[socket.id] })
})
})
app.use('/', express.static(__dirname + '/public'));
server.listen(4000, () => {
console.log("server is running http://localhost:4000")
})