Skip to content

Commit 4033ca8

Browse files
committed
Allow the usage of options.url to connect to server
1 parent 5ea8b5c commit 4033ca8

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

README.markdown

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ var ddpclient = new DDPClient({
5555
// from projects like meteorhacks:cluster
5656
// (for load balancing and service discovery)
5757
// do not use `path` option when you are using useSockJs
58-
useSockJs: true
58+
useSockJs: true,
59+
// Use a full url instead of a set of `host`, `port` and `ssl`
60+
// do not set `useSockJs` option if `url` is used
61+
url: 'wss://example.com/websocket'
5962
});
6063

6164
/*

examples/example.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ var ddpclient = new DDPClient({
1616
// from projects like meteorhacks:cluster
1717
// (load balancing and service discovery)
1818
// do not use `path` option when you are using useSockJs
19-
useSockJs: true
19+
useSockJs: true,
20+
// Use a full url instead of a set of `host`, `port` and `ssl`
21+
// do not set `useSockJs` option if `url` is used
22+
url: 'wss://example.com/websocket'
2023
});
2124

2225
/*

lib/ddp-client.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var DDPClient = function(opts) {
2929
self.autoReconnect = ("autoReconnect" in opts) ? opts.autoReconnect : true;
3030
self.autoReconnectTimer = ("autoReconnectTimer" in opts) ? opts.autoReconnectTimer : 500;
3131
self.maintainCollections = ("maintainCollections" in opts) ? opts.maintainCollections : true;
32-
32+
self.url = opts.url;
3333
// support multiple ddp versions
3434
self.ddpVersion = ("ddpVersion" in opts) ? opts.ddpVersion : "1";
3535
self.supportedDdpVersions = ["1", "pre2", "pre1"];
@@ -359,11 +359,15 @@ DDPClient.prototype._makeSockJSConnection = function() {
359359

360360
DDPClient.prototype._buildWsUrl = function(path) {
361361
var self = this;
362+
var url;
362363
path = path || self.path || "websocket";
363364
var protocol = self.ssl ? "wss://" : "ws://";
364-
var url = protocol + self.host + ":" + self.port;
365-
url += (path.indexOf("/") === 0)? path : "/" + path;
366-
365+
if (self.url && !self.useSockJs) {
366+
url = self.url;
367+
} else {
368+
url = protocol + self.host + ":" + self.port;
369+
url += (path.indexOf("/") === 0)? path : "/" + path;
370+
}
367371
return url;
368372
};
369373

test/ddp-client.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,16 @@ describe("Connect to remote server", function() {
4747
new DDPClient({'host': 'myserver.com', 'port': 443}).connect();
4848
assert.deepEqual(wsConstructor.args, [['wss://myserver.com:443/websocket']]);
4949
});
50-
50+
it('should connect to the provided url', function() {
51+
new DDPClient({'url': 'wss://myserver.com/websocket'}).connect();
52+
assert.deepEqual(wsConstructor.args, [['wss://myserver.com/websocket']]);
53+
});
54+
it('should fallback to sockjs if url and useSockJs:true are provided', function() {
55+
var ddpclient = new DDPClient({'url': 'wss://myserver.com/websocket', 'useSockJs': true});
56+
ddpclient._makeSockJSConnection = sinon.stub();
57+
ddpclient.connect();
58+
assert.ok(ddpclient._makeSockJSConnection.called);
59+
});
5160
it('should clear event listeners on close', function(done) {
5261
var ddpclient = new DDPClient();
5362
var callback = sinon.stub();
@@ -408,4 +417,4 @@ function WithRequestGet(getFn, fn) {
408417
fn();
409418

410419
request.get = originalGet;
411-
}
420+
}

0 commit comments

Comments
 (0)