-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·33 lines (26 loc) · 1.04 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
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
app.use(express.static(__dirname));
app.get('/', function(req, res) {
//res.render(__dirname + '/views/index.ejs');
res.sendFile(__dirname + '/index.html');
});
io.sockets.on('connection', function(socket) {
socket.on('username', function(username) {
console.log("User " + username + " connected.");
socket.username = username;
io.emit('is_online', '🔵 <i>' + socket.username + ' join the chat..</i>');
});
socket.on('disconnect', function(username) {
console.log("User " + socket.username + " disconnected.");
io.emit('is_online', '🔴 <i>' + socket.username + ' left the chat..</i>');
})
socket.on('chat_message', function(message) {
io.emit('chat_message', '<strong>' + socket.username + '</strong>: ' + message);
});
});
http.listen(3000, function() {
console.log('listening on *:3000');
});