-
Notifications
You must be signed in to change notification settings - Fork 5
/
browser.js
60 lines (49 loc) · 1.49 KB
/
browser.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
// Copyright IBM Corp. 2015. All Rights Reserved.
// Node module: strong-pubsub-example
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
var Client = require('strong-pubsub');
var Adapter = require('strong-pubsub-mqtt');
var duplex = require('duplex');
Primus.Stream = require('stream');
module.exports = function(PORT) {
var client = new Client({port: PORT, host: 'localhost'}, Adapter, {
createConnection: function(port, host) {
var connection = duplex();
var primus = Primus.connect('http://' + host + ':' + port, {
transformer: 'engine.io',
parser: 'binary'
});
connection.on('_data', function(chunk) {
// someone called `connection.write(buf)`
primus.write(chunk);
});
primus.on('data', function(chunk) {
// chunk is an arrayBuffer
connection._data(toBuffer(chunk));
});
primus.on('open', function() {
connection.emit('connect');
});
connection.on('_end', function() {
primus.end();
this._end();
});
return connection;
}
});
client.subscribe('foo', function() {
console.log('subscribed');
});
client.on('message', function(topic, msg) {
console.log(topic, msg.toString());
});
}
function toBuffer(ab) {
var buffer = new Buffer(ab.byteLength);
var view = new Uint8Array(ab);
for(var i = 0; i < buffer.length; ++i) {
buffer[i] = view[i];
}
return buffer;
}