forked from scottgonzalez/node-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
132 lines (106 loc) · 3.01 KB
/
Copy pathserver.js
File metadata and controls
132 lines (106 loc) · 3.01 KB
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
var router = require("./router"),
url = require("url"),
qs = require("querystring"),
Channel = require("./channel").Channel;
function Server() {
this.httpServer = router.createServer();
this.channels = [];
}
extend(Server.prototype, {
listen: function(port, host) {
this.httpServer.listen(port, host);
},
passThru: function(path, handler) {
this.httpServer.get(path, handler);
},
addChannel: function(options) {
var httpServer = this.httpServer,
channel = new Channel(options);
if (!channel) {
return false;
}
this.channels.push(channel);
handlers.forEach(function(handler) {
httpServer.get(channel.basePath + handler.path,
handler.handler.partial(channel));
});
return channel;
}
});
exports.createServer = function() {
return new Server();
};
var handlers = [
{ path: "/who", handler: function(channel, request, response) {
var nicks = [];
for (var id in channel.sessions) {
nicks.push(channel.sessions[id].nick);
}
response.simpleJSON(200, { nicks: nicks });
} },
{ path: "/join", handler: function(channel, request, response) {
var nick = qs.parse(url.parse(request.url).query).nick;
if (!nick) {
response.simpleJSON(400, { error: "bad nick." });
return;
}
var session = channel.createSession(nick);
if (!session) {
response.simpleJSON(400, { error: "nick in use." });
return;
}
response.simpleJSON(200, { id: session.id, nick: nick, since: session.since });
} },
{ path: "/part", handler: function(channel, request, response) {
var id = qs.parse(url.parse(request.url).query).id;
var eventId = channel.destroySession(id);
response.simpleJSON(200, { id: eventId });
} },
{ path: "/recv", handler: function(channel, request, response) {
var query = qs.parse(url.parse(request.url).query),
since = parseInt(query.since, 10),
session = channel.sessions[query.id];
if (!session) {
response.simpleJSON(400, { error: "No such session id." });
return;
}
if (isNaN(since)) {
response.simpleJSON(400, { error: "Must supply since parameter." });
return;
}
session.poke();
channel.query(since, function(messages) {
session.poke();
response.simpleJSON(200, { messages: messages });
});
} },
{ path: "/send", handler: function(channel, request, response) {
var query = qs.parse(url.parse(request.url).query),
text = query.text,
session = channel.sessions[query.id];
if (!session) {
response.simpleJSON(400, { error: "No such session id." });
return;
}
if (!text || !text.length) {
response.simpleJSON(400, { error: "Must supply text parameter." });
return;
}
session.poke();
var id = channel.appendMessage(session.nick, "msg", text);
response.simpleJSON(200, { id: id });
} }
];
var slice = [].slice;
Function.prototype.partial = function() {
var fn = this,
args = slice.call(arguments);
return function() {
return fn.apply(this, args.concat(slice.call(arguments)));
};
};
function extend(obj, props) {
for (var prop in props) {
obj[prop] = props[prop];
}
}