/
client.js
48 lines (40 loc) · 1.37 KB
/
client.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
// This script demonstrates a logger for the chat app. First, start the chat
// server in one terminal then run this in another:
//
// $ node examples/node/server.js
// $ node examples/node/client.js
//
// The client connects to the chat server and logs all messages sent by all
// connected users.
var faye = require('../../build/node/faye-node'),
port = process.argv[2] || 8000,
path = process.argv[3] || 'bayeux',
scheme = process.argv[4] === 'ssl' ? 'https' : 'http',
endpoint = scheme + '://localhost:' + port + '/' + path;
console.log('Connecting to ' + endpoint);
var client = new faye.Client(endpoint);
var subscription = client.subscribe('/chat/*', function(message) {
var user = message.user;
var publication = client.publish('/members/' + user, {
user: 'node-logger',
message: ' Got your message, ' + user + '!'
});
publication.callback(function() {
console.log('[PUBLISH SUCCEEDED]');
});
publication.errback(function(error) {
console.log('[PUBLISH FAILED]', error);
});
});
subscription.callback(function() {
console.log('[SUBSCRIBE SUCCEEDED]');
});
subscription.errback(function(error) {
console.log('[SUBSCRIBE FAILED]', error);
});
client.bind('transport:down', function() {
console.log('[CONNECTION DOWN]');
});
client.bind('transport:up', function() {
console.log('[CONNECTION UP]');
});