forked from matiastucci/socket-chat-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·77 lines (63 loc) · 2.06 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 8080;
app.all('/*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", req.headers.origin || "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");
res.header("Access-Control-Allow-Methods", "GET");
next();
});
app.get('/', function(req, res){
res.send('welcome to my chat server');
});
// usernames which are currently connected to the chat
var usernames = {};
io.on('connection', function (socket) {
var addedUser = false;
socket.on('new message', function (data) {
socket.broadcast.emit('new message', {
username: socket.username,
content: data
});
});
socket.on('add user', function (username) {
// we store the username in the socket session for this client
socket.username = username;
// add the client's username to the global list
usernames[username] = username;
addedUser = true;
// echo globally (all clients) that a person has connected
socket.broadcast.emit('user joined', {
username: socket.username
});
});
// when the client emits 'typing', we broadcast it to others
socket.on('typing', function () {
socket.broadcast.emit('typing', {
username: socket.username
});
});
// when the client emits 'stop typing', we broadcast it to others
socket.on('stop typing', function () {
socket.broadcast.emit('stop typing', {
username: socket.username
});
});
socket.on('disconnect', function () {
// remove the username from global usernames list
if (addedUser) {
delete usernames[socket.username];
// echo globally that this client has left
socket.broadcast.emit('user left', {
username: socket.username
});
}
});
});
app.get('/usernames', function(req, res){
res.send(usernames);
});
http.listen(port, function(){
console.log('listening on *:'+port);
});