-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver.js
More file actions
90 lines (64 loc) · 2.22 KB
/
server.js
File metadata and controls
90 lines (64 loc) · 2.22 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
// 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 path = require('path');
var duplex = require('duplex');
var helpers = require('strong-pubsub-test');
var Client = require('strong-pubsub');
var Proxy = require('strong-pubsub-bridge');
var Adapter = require('strong-pubsub-mqtt');
var Connection = require('strong-pubsub-connection-mqtt');
var http = require('http');
var net = require('net');
var browserify = require('browserify');
var express = require('express');
var Primus = require('primus');
var HTTP_PORT = process.env.HTTP_PORT || 3000;
var TCP_PORT = process.env.TCP_PORT || 4000;
var MOSQUITTO_PORT = process.env.MOSQUITTO_PORT || 6000;
var mosquitto = helpers.setupMosquitto(MOSQUITTO_PORT);
helpers.waitForConnection(MOSQUITTO_PORT, function() {
console.log('mosquitto started on %s', MOSQUITTO_PORT);
var app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.get('/bundle.js', function(req, res) {
var b = browserify({
basedir: __dirname,
debug: true
});
b.require('stream');
b.require('url');
b.require('util');
b.require('buffer');
b.require(path.join(__dirname, 'browser.js'), {expose: 'pubsub-client'});
b.bundle().pipe(res);
});
var httpServer = http.createServer(app);
var tcpServer = net.createServer();
var primus = new Primus(httpServer, {
transformer: 'engine.io',
parser: 'binary'
});
primus.on('connection', function(spark) {
var proxy = new Proxy(
new Connection(spark),
new Client({port: MOSQUITTO_PORT}, Adapter)
);
proxy.connect();
});
tcpServer.on('connection', function(socket) {
var proxy = new Proxy(
// upgrade the tcp socket into a strong-pubsub-connection
new Connection(socket),
new Client({port: MOSQUITTO_PORT}, Adapter)
);
proxy.connect();
});
httpServer.listen(HTTP_PORT, function(err) {
console.log(err || 'HTTP server listening at port ' + HTTP_PORT);
});
tcpServer.listen(TCP_PORT, function(err) {
console.log(err || 'TCP server listening at port ' + TCP_PORT);
});
});