-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpushserver.js
More file actions
133 lines (126 loc) · 3.95 KB
/
pushserver.js
File metadata and controls
133 lines (126 loc) · 3.95 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
133
var redis = require('redis'),
EventEmitter = require('events').EventEmitter,
_ = require('underscore'),
sockjs = require('sockjs'),
util = require('util');
var Channel = function (name, redisSub) {
var thisChannel = this;
this.name = name;
console.log("Creating new channel: " + this.name);
var clients = [];
this.updateRedisSubscription = function(removingClient) {
if (clients.length === 1 && !removingClient) {
console.log("Subscribing to redis channel: " + thisChannel.name);
redisSub.subscribe(thisChannel.name);
} else if (clients.length === 0 && removingClient) {
console.log("Channel " + thisChannel.name + " is empty - closing...");
redisSub.unsubscribe(thisChannel.name);
thisChannel.emit('close');
}
};
this.updateRedisSubscription();
this.removeClient = function (client) {
try {
client.close();
} catch (e) {
// ignorzo!
}
var index = clients.indexOf(client);
clients.splice(index, 1);
thisChannel.updateRedisSubscription(true);
};
this.addClient = function (client) {
if (_.contains(clients, client)) {
return;
}
client.on('close', function () {
console.log("Channel " + thisChannel.name + " dropped a client");
thisChannel.removeClient(client);
this.emit('remove-client');
});
client.channel = this;
clients.push(client);
this.updateRedisSubscription();
};
this.send = function (message) {
console.log("Redis message received. Pushing to " + clients.length + " clients!");
_.each(clients, function (client) {
client.write(JSON.stringify({
type: "message",
data: message }));
});
};
this.close = function () {
_.each(clients, this.removeClient);
};
this.toString = function () {
return "Channel: " + this.name + " (" + clients.length + " clients)";
};
};
util.inherits(Channel, EventEmitter);
var PushServer = function (options) {
this.channels = {};
this.redisSub = redis.createClient()
var that = this;
sockServer = sockjs.createServer();
sockServer.installHandlers(options.server, {prefix:'/push'});
this.subscribeToRedis = function (channelName, client) {
var then = this;
var channel = this.channels[channelName];
if (!channel) {
channel = new Channel(channelName, that.redisSub);
channel.on('close', function () {
delete then.channels[channelName];
});
this.channels[channelName] = channel;
}
channel.addClient(client);
return channel;
};
this.removeClient = function (channelName, client) {
var then = this;
var channel = this.channels[channelName];
if (channel) {
channel.removeClient(client);
}
return channel;
};
sockServer.on('connection', function (client) {
console.log("Connected to Websocket: " + client);
client.on('data', function (message) {
var data = JSON.parse(message);
if (data.action === "subscribe") {
var channel = data.channel;
if (!channel) {
console.log("Subscribe action with no channel - closing socket!");
client.close();
} else {
console.log("Subscribing to channel: " + channel)
that.subscribeToRedis(channel, client);
}
}
else if (data.action === "ping") {
client.write(JSON.stringify({
type: "pong",
data: data.id
}));
}
else if (data.action === "unsubscribe") {
var channel = data.channel;
if (!channel) {
console.log("UnSubscribe action with no channel do nothing");
} else {
console.log("Subscribing to channel: " + channel)
that.removeClient(channel, client);
}
}
});
});
this.redisSub.on('message', function (channel, data) {
console.log("Message Received, Send to Client");
console.log("Message: " + data);
that.channels[channel].send(data);
});
};
util.inherits(PushServer, EventEmitter);
module.exports = PushServer;