-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpubsub-server.js
More file actions
137 lines (119 loc) · 3.65 KB
/
pubsub-server.js
File metadata and controls
137 lines (119 loc) · 3.65 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
134
135
136
137
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var config = require(process.argv[2] || './config');
var IdMap = require('./lib/IdMap');
var Channel = require('./lib/Channel');
var Client = require('./lib/Client');
var ChannelFactory = require('./lib/ChannelFactory');
/**
* Listen for new connections, and create a client when connected.
*/
io.on('connection', function(socket)
{
var client = new Client( socket );
config.debug( 'Client Connected', socket.id );
/**
* Handle when the client tries to subscribe to a channel.
*/
socket.on('subscribe', function(msg)
{
var token = msg.token;
var channelId = msg.id;
config.validateToken( token ).then(
function onValidToken()
{
ChannelFactory.get( channelId, true ).then(
function onChannel(channel)
{
client.join( channel, token );
config.debug( 'Client', socket.id, 'subscribed to channel', channelId, 'with token', token );
}
);
},
function onInvalidToken()
{
config.debug( 'Client', socket.id, 'sent invalid token', token );
}
);
});
/**
* Handle when the client tries to unsubscribe from a channel.
*/
socket.on('unsubscribe', function(msg)
{
var channelId = msg.id;
ChannelFactory.get( channelId, false ).then(
function onChannel(channel)
{
if (channel.has( client ) && client.subscribed( channel ))
{
client.leave( channel );
if (channel.size === 0)
{
ChannelFactory.remove( channel );
}
config.debug( 'Client', socket.id, 'unsubscribed from channel', channelId );
}
else
{
config.debug( 'Client', socket.id, 'does not participate in the channel', channelId, 'so they cannot unsubscribe' );
}
},
function onInvalidChannel()
{
config.debug( 'Client', socket.id, 'cannot unsubscribe from the channel', channelId, ', it does not exist' );
}
);
});
/**
* Handle when the client tries to publish to a channel.
*/
socket.on('publish', function(msg)
{
var data = msg.data;
var channelId = msg.id;
ChannelFactory.get( channelId, false ).then(
function onChannel(channel)
{
// if you need to be subscribed to publish...
if (!config.requireSubscription || client.subscribed( channel ))
{
config.validatePublish( data, client, channel ).then(
function onValidPublish()
{
// if it's a valid publish
channel.publish( client,
{
id: channelId,
data: data
});
config.debug( 'Client', socket.id, 'published to channel', channelId, ':', data );
},
function onInvalidPublish()
{
config.debug( 'Client', socket.id, 'sent an invalid publish to channel', channelId, ':', data );
}
);
}
else
{
config.debug( 'Client', socket.id, 'tried to publish to channel', channelId, 'but is not subscribed' );
}
},
function onInvalidChannel()
{
config.debug( 'Client', socket.id, 'tried to publish to channel', channelId, 'which does not exist' );
}
);
});
/**
* Handle when a client disconnects from the server.
*/
socket.on('disconnect', function()
{
client.destroy();
config.debug( 'Client Disconnected', socket.id );
});
});
http.listen( config.port );