-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
34 lines (26 loc) · 867 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
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 4141;
var path = require('path');
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
app.use(express.static(path.join(__dirname + '/public')));
io.on('connection', function(socket) {
socket.on('user_join', function(data) {
this.username = data;
socket.broadcast.emit('user_join', data);
});
socket.on('chat_message', function(data) {
data.username = this.username;
socket.broadcast.emit('chat_message', data);
});
socket.on('disconnect', function(data) {
socket.broadcast.emit('user_leave', this.username);
});
});
http.listen(port, function() {
console.log('Listening on *:' + port);
});