forked from ckpwong/GFayeSwift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
faye_server.js
82 lines (62 loc) · 2.19 KB
/
faye_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
var http = require('http'),
faye = require('faye');
faye.Logging.logLevel = 'debug';
var bayeux = new faye.NodeAdapter({
mount: '/faye',
timeout: 45
});
// Handle non-Bayeux requests
var server = http.createServer(function(request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write('Non-Bayeux request');
response.end();
});
serverLog = {
incoming: function(message, callback) {
console.log("SOMETTHING HAPPENDED!");
if (message.channel === '/meta/subscribe') {
logWithTimeStamp("CLIENT SUBSCRIBED Client ID: " + message.clientId);
}
if (message.channel.match(/\/users\/*/)) {
logWithTimeStamp("USER MESSAGE ON CHANNEL: " + message.channel);
}
return callback(message);
}
};
logWithTimeStamp = function(logMessage) {
var timestampedMessage;
timestampedMessage = "" + (Date()) + " | " + logMessage;
return console.log(timestampedMessage);
};
bayeux.bind('handshake', function(client_id) {
console.log("[handshake] - client: '"+ client_id +"'");
});
bayeux.bind('subscribe', function(client_id, channel) {
console.log("[subscribe] - client: '"+ client_id +"', channel: '"+ channel +"'");
});
bayeux.bind('unsubscribe', function(client_id, channel) {
console.log("[unsubscribe] - client: '"+ client_id +"', channel: '"+ channel +"'");
});
bayeux.bind('publish', function(client_id, channel, data) {
console.log("[publish] - client: '"+ client_id +"', channel: '"+ channel +"'");
console.log("[publish] - data:");
console.log(data);
});
bayeux.bind('connect', function(client_id) {
console.log("[connect] - client: '"+ client_id +"'");
});
bayeux.bind('disconnect', function(client_id) {
console.log("[disconnect] - client: '"+ client_id +"'");
});
bayeux.addExtension(serverLog);
bayeux.attach(server);
bayeux.getClient().subscribe('/cool', function(message){
console.log("OMG NEW MESSAGE CAME ***********");
console.log(JSON.stringify(message));
});
bayeux.getClient().subscribe('/awesome', function(message){
console.log("OMG NEW AWESOME MESSAGE CAME ***********");
console.log(JSON.stringify(message));
});
server.listen(5222);
console.log("Started Faye Server");