-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (29 loc) · 860 Bytes
/
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
let express = require('express');
let socket = require('socket.io');
let url = require('url');
//App setup
let app = express();
let port = process.env.port || 5000;
let Server = app.listen(port, (req, res) => {
console.log("Server running on http://localhost:5000");
});
// Static file
app.use(express.static('public'));
// socket setup
let io = socket(Server);
io.on('connection',function (socket) {
console.log("made socket connection", socket.id);
socket.on('chat',function(data) {
io.sockets.emit('chat',data);
});
socket.on('typing',(data) => {
socket.broadcast.emit('typing',data);
});
});
//Routes setup
app.get('/', (req, res) => {
const path = url.parse(req.url).pathname.slice(1).replace(/%20/g, ' ')
res.writeHead(200, {'Content-Type': 'application/json'})
res.write(index);
res.end();
});