-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
44 lines (38 loc) · 1.23 KB
/
server.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
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(3000);
app.use(express.static(__dirname + '/public'));
// app.get('/', function (req, res) {
// res.sendFile('/index.html');
// });
io.on('connection', function (socket) {
socket.on('user joined', function(msg){
socket.username = msg.username;
socket.broadcast.emit('chat message',msg.msg);
console.log('message: ' + msg.msg);
});
socket.on('chat message', function(msg){
io.emit('chat message',msg.msg);
console.log('message: ' + msg.msg);
});
socket.on('disconnect', function () {
console.log(socket.username+' disconnected');
socket.broadcast.emit('user left', {
username: socket.username
});
});
socket.on('typing',function () {
console.log(socket.username+' typing');
socket.broadcast.emit('typing', {
username: socket.username
});
});
socket.on('stop typing',function () {
console.log(socket.username+' stopped typing');
socket.broadcast.emit('stop typing', {
username: socket.username
});
});
});