forked from wmora/nodejs-express-socketio-chatroom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
157 lines (122 loc) · 4.46 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Module dependencies:
- Express
- Http (to run Express)
- Body parser (to parse JSON requests)
- Underscore (because it's cool)
- Socket.IO
It is a common practice to name the variables after the module name.
Ex: http is the "http" module, express is the "express" module, etc.
The only exception is Underscore, where we use, conveniently, an
underscore. Oh, and "socket.io" is simply called io. Seriously, the
rest should be named after its module name.
*/
var express = require("express")
, app = express()
, http = require("http").createServer(app)
, bodyParser = require("body-parser")
, io = require("socket.io").listen(http)
, _ = require("underscore");
/*
The list of participants in our chatroom.
The format of each participant will be:
{
id: "sessionId",
name: "participantName"
}
*/
var participants = [];
var socket_list = [];
/* Server config */
//Server's IP address
app.set("ipaddr", "127.0.0.1");
//Server's port number
app.set("port", 8080);
//Specify the views folder
app.set("views", __dirname + "/views");
//View engine is Jade
app.set("view engine", "jade");
//Specify where the static content is
app.use(express.static("public", __dirname + "/public"));
//Tells server to support JSON requests
app.use(bodyParser.json());
/* Server routing */
//Handle route "GET /", as in "http://localhost:8080/"
app.get("/", function(request, response) {
//Render the view called "index"
response.render("index");
});
//POST method to create a chat message
app.post("/message", function(request, response) {
//The request body expects a param named "message"
var message = request.body.message;
//If the message is empty or wasn't sent it's a bad request
if(_.isUndefined(message) || _.isEmpty(message.trim())) {
return response.json(400, {error: "Message is invalid"});
}
//We also expect the sender's name with the message
var name = request.body.name;
//Let our chatroom know there was a new message
io.sockets.emit("incomingMessage", {message: message, name: name});
//Looks good, let the client know
response.json(200, {message: "Message received"});
});
app.post("/message_private", function(request, response) {
var message = request.body.message;
//If the message is empty or wasn't sent it's a bad request
if(_.isUndefined(message) || _.isEmpty(message.trim())) {
return response.json(400, {error: "Message is invalid"});
}
//We also expect the sender's name with the message
var name = request.body.name;
var selected_id = request.body.to_user;
console.log(name);
var to_user = _.find(socket_list, function(sock) {
if (sock.id == selected_id) {
return true;
} else {
return false;
}
});
console.log("userid_:"+to_user.id);
//Let our chatroom know there was a new message
to_user.emit("incomingPrivateMessage", {message: message, name: name});
//Looks good, let the client know
response.json(200, {message: "Message received"});
});
/* Socket.IO events */
io.on("connection", function(socket){
/*
When a new user connects to our server, we expect an event called "newUser"
and then we'll emit an event called "newConnection" with a list of all
participants to all connected clients
*/
socket_list.push(socket);
socket.on("newUser", function(data) {
participants.push({id: data.id, name: data.name});
console.log("new user");
io.sockets.emit("newConnection", {participants: participants});
});
/*
When a user changes his name, we are expecting an event called "nameChange"
and then we'll emit an event called "nameChanged" to all participants with
the id and new name of the user who emitted the original message
*/
socket.on("nameChange", function(data) {
_.findWhere(participants, {id: socket.id}).name = data.name;
io.sockets.emit("nameChanged", {id: data.id, name: data.name});
});
/*
When a client disconnects from the server, the event "disconnect" is automatically
captured by the server. It will then emit an event called "userDisconnected" to
all participants with the id of the client that disconnected
*/
socket.on("disconnect", function() {
participants = _.without(participants, _.findWhere(participants, {id: socket.id}));
io.sockets.emit("userDisconnected", {id: socket.id, sender:"system"});
});
});
//Start the http server at port and IP defined before
http.listen(app.get("port"), app.get("ipaddr"), function() {
console.log("Server up and running. Go to http://" + app.get("ipaddr") + ":" + app.get("port"));
});