-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (36 loc) · 1.18 KB
/
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
43
44
45
46
47
const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);
// var clients = [];
app.set('view engine','ejs');
// use res.render to load up an ejs view file
// index page
app.get('*', (req, res) => {
res.render('pages/index');
const url = req.url;
io.on('connection', (socket) => {
// Sends message when someone joins the page
io.emit('system message', `A User Join the Chat`);
var name = "Temp User " + socket.id;
socket.join(url);
console.log(url);
console.log(socket.id);
// When a username is picked
socket.on(url + ' nickname pick', (nickname) => {
io.emit(url + ' system message', `${name} changed their nickname to ${nickname}`);
name = nickname;
return name;
});
// When a normal chat message is sent
socket.on(url + ' chat message', (msg) => {
io.emit(url + ' chat message', `${name}: ${msg}`);
});
// Used for system messages that will face the client
socket.on(url + ' system message', (msg) => {
io.emit(url + ' system message', `${msg}`);
});
});
});
http.listen(3000, () => {
console.log('listening on *:3000');
});