-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·45 lines (41 loc) · 1.26 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
const express = require('express');
const app = express();
const http = require('http').createServer(app);
const path = require('path');
const io = require('socket.io')(http);
const port = 4000;
function uuid() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
var r = (Math.random() * 16) | 0,
v = c == "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
app.use(express.static(path.join(__dirname, 'src')));
// console.log(path.join(__dirname, 'public','index.html'))
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'))
});
io.on('connection', (socket) => {
let id = socket.client.conn.id;
console.log(`(User: ${id}) connected`);
socket.on('chat-message', (msg) => {
socket.broadcast.emit('chat-message', {
body: msg,
id: uuid(),
type: 'recipient'
})
});
socket.on('is-typing', () => {
socket.broadcast.emit('is-typing');
});
socket.on('not-typing', () => {
socket.broadcast.emit('not-typing');
});
socket.on('disconnect', () => {
console.log(`(User: ${id}) disconnected`);
});
});
http.listen(port, () => {
console.log('Server running on port ' + port)
})