This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
socket.js
240 lines (214 loc) · 8.16 KB
/
socket.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
var sys = require("sys");
var ws = require('./vendor/node-websocket-server/lib/ws');
var redis = require("./vendor/redis-node-client/lib/redis-client");
function log(data){
sys.log("\033[0;32m"+data+"\033[0m");
}
var user_count = 0;
var main_store = redis.createClient();
var server = ws.createServer({ debug: true });
server.addListener("listening", function(){
log("Listening for connections.");
});
server.addListener("request", function(req, res){
res.writeHead(200, {"Content-Type": "text/plain"});
res.write("Chat Server");
res.end();
});
// Handle WebSocket Requests
server.addListener("connection", function(conn){
log("opened connection: "+conn.id);
//server.send(conn.id, "Connected as: "+conn.id);
//conn.broadcast("<"+conn.id+"> connected");
var self = conn;
conn.redis_subscriber = redis.createClient();
conn.redis_publisher = redis.createClient();
conn.redis_subscriber.subscribeTo("*",
function (channel, message, subscriptionPattern) {
var output = '{"channel": "' + channel + '", "payload": ' + message + '}';
//sys.puts(output);
conn.write(output);
});
current_user_id = conn.user_id = ++user_count;
//store the current user's id on global store
main_store.rpush('pad-users', conn.user_id, function(err, reply){
main_store.lrange('pad-users', 0, -1, function(err, values){
conn.write('{"channel": "initial", "id":' + current_user_id + ', "users":[' + values + '] }');
//send all the exisiting diff messages
// main_store.lrange('pad-diff', 0, -1, function(err, messages){
// for(var msg_id in messages){
// conn.write('{"channel": "diff", "payload": ' + messages[msg_id] + '}');
// }
// });
//
main_store.lrange('pad-chat', 0, -1, function(err, messages){
for(var msg_id in messages){
conn.write('{"channel": "chat", "payload": ' + messages[msg_id] + '}');
}
});
//publish the message when joining
conn.redis_publisher.publish("join", JSON.stringify({"user": conn.user_id}),
function (err, reply) {
sys.puts(err);
sys.puts("Published message to " +
(reply === 0 ? "no one" : (reply + " subscriber(s).")));
});
});
});
conn.addListener("message", function(raw_message){
log(conn.id + ": "+JSON.stringify(raw_message));
message_obj = JSON.parse(raw_message);
channel = message_obj["type"];
message = message_obj["message"];
timestamp = new Date().getTime();
serialized_message = JSON.stringify({"user": this.user_id, "message": message, "timestamp": timestamp, "channel": channel });
//store snapshot
if(channel == "snapshot"){
sys.puts(serialized_message);
main_store.set('pad-snapshot', serialized_message, function(){});
}
//send all the exisiting diff messages
else if(channel == "playback"){
main_store.lrange('pad-1', 0, -1, function(err, messages){
for(var msg_id in messages){
log(messages[msg_id]);
var parsed_msg = JSON.parse(messages[msg_id]); //this is a dirty hack REMOVE!
conn.write('{"channel":"' + parsed_msg['channel'] + '", "payload": ' + messages[msg_id] + '}');
}
//once all messages sent, send a playback complete message
conn.write('{"channel": "playback_done", "payload": "" }');
});
}
else {
conn.redis_publisher.publish(channel, serialized_message,
function (err, reply) {
sys.puts("Published message to " +
(reply === 0 ? "no one" : (reply + " subscriber(s).")));
//store the messages on main store
main_store.rpush('pad-1', serialized_message, function(err, reply){});
});
}
});
});
server.addListener("close", function(conn){
log(conn.id + ": onClose");
//publish a message before leaving
conn.redis_publisher.publish("leave", JSON.stringify({"user": conn.user_id}),
function (err, reply) {
sys.puts(err);
sys.puts("Published message to " +
(reply === 0 ? "no one" : (reply + " subscriber(s).")));
});
conn.redis_publisher.close();
conn.redis_subscriber.close();
});
server.listen(8080);
// old web-socket listeners
// server.addListener("client", function(conn){
// log(conn._id + ": new connection");
//
//
// conn.addListener("readyStateChange", function(readyState){
// log("stateChanged: "+readyState);
// });
//
// conn.addListener("open", function(){
// log(conn._id + ": onOpen");
// o = this;
//
// o.redis_subscriber = redis.createClient();
// o.redis_publisher = redis.createClient();
//
// o.redis_subscriber.subscribeTo("*",
// function (channel, message, subscriptionPattern) {
// var output = '{"channel": "' + channel + '", "payload": ' + message + '}';
//
// sys.puts(output);
// conn.write(output);
// });
//
// current_user_id = o.user_id = ++user_count;
//
// //store the current user's id on global store
// main_store.rpush('pad-users', o.user_id, function(err, reply){
// main_store.lrange('pad-users', 0, -1, function(err, values){
// conn.write('{"channel": "initial", "id":' + current_user_id + ', "users":[' + values + '] }');
//
// //send all the exisiting diff messages
// // main_store.lrange('pad-diff', 0, -1, function(err, messages){
// // for(var msg_id in messages){
// // conn.write('{"channel": "diff", "payload": ' + messages[msg_id] + '}');
// // }
// // });
// //
//
// // main_store.get('pad-snapshot', function(err, reply){
// // if(reply)
// // conn.write('{"channel": "snapshot", "payload": ' + reply + '}');
// // });
//
// main_store.lrange('pad-chat', 0, -1, function(err, messages){
// for(var msg_id in messages){
// conn.write('{"channel": "chat", "payload": ' + messages[msg_id] + '}');
// }
// });
//
// //publish the message when joining
// o.redis_publisher.publish("join", JSON.stringify({"user": o.user_id}),
// function (err, reply) {
// sys.puts("Published message to " +
// (reply === 0 ? "no one" : (reply + " subscriber(s).")));
// });
// });
// });
// });
//
// conn.addListener("close", function(){
// var c = this;
// log(c._id + ": onClose");
//
// //publish a message before leaving
// this.redis_publisher.publish("leave", JSON.stringify({"user": this.user_id}),
// function (err, reply) {
// sys.puts("Published message to " +
// (reply === 0 ? "no one" : (reply + " subscriber(s).")));
// });
//
// this.redis_publisher.close();
// this.redis_subscriber.close();
// });
//
// conn.addListener("message", function(raw_message){
// log(conn._id + ": "+JSON.stringify(raw_message));
//
// message_obj = JSON.parse(raw_message);
// channel = message_obj["type"];
// message = message_obj["message"];
// timestamp = new Date().getTime();
// serialized_message = JSON.stringify({"user": this.user_id, "message": message, "timestamp": timestamp });
//
// //store snapshot
// if(channel == "snapshot"){
// sys.puts(serialized_message);
// main_store.set('pad-snapshot', serialized_message, function(){});
// }
// //send all the exisiting diff messages
// else if(channel == "playback"){
// main_store.lrange('pad-diff', 0, -1, function(err, messages){
// for(var msg_id in messages){
// log(messages[msg_id]);
// conn.write('{"channel": "diff", "payload": ' + messages[msg_id] + '}');
// }
// });
// }
// else {
// this.redis_publisher.publish(channel, serialized_message,
// function (err, reply) {
// sys.puts("Published message to " +
// (reply === 0 ? "no one" : (reply + " subscriber(s).")));
// //store the messages on main store
// main_store.rpush('pad-' + channel, serialized_message, function(err, reply){});
// });
// }
// });
// });