diff --git a/README.md b/README.md index d96c13bc0..7f1769c25 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,8 @@ Option | Default value | Description server | api.streamr.com | Address of the server to connect to. autoConnect | true | If set to `true`, the client connects automatically on the first call to `subscribe()`. Otherwise an explicit call to `connect()` is required. autoDisconnect | true  | If set to `true`, the client automatically disconnects when the last channel is unsubscribed. Otherwise the connection is left open and can be disconnected explicitly by calling `disconnect()`. +transports | null | Override default transport selection / upgrade scheme. For example, value `["websocket"]` will force use of sockets right from the beginning, while value `["polling"]` will allow only long-polling to be used. + ## Resend options diff --git a/streamr-client.js b/streamr-client.js index 7fa2c1ac5..502d3d67b 100644 --- a/streamr-client.js +++ b/streamr-client.js @@ -256,7 +256,9 @@ function StreamrClient(options) { // Automatically connect on first subscribe autoConnect: true, // Automatically disconnect on last unsubscribe - autoDisconnect: true + autoDisconnect: true, + // Allow client socket library to choose appropriate transport + transports: null } this.subsByStream = {} this.subById = {} @@ -384,7 +386,10 @@ StreamrClient.prototype.connect = function(reconnect) { this.connecting = true this.disconnecting = false - this.socket = io(this.options.server, {forceNew: true}) + this.socket = io(this.options.server, { + forceNew: true, + transports: this.options.transports + }) this.socket.on('ui', function(data) { if (typeof data == 'string' || data instanceof String) { diff --git a/test/test.streamr-client.js b/test/test.streamr-client.js index 47bf9940a..7b2c157af 100644 --- a/test/test.streamr-client.js +++ b/test/test.streamr-client.js @@ -100,7 +100,7 @@ describe('StreamrClient', function() { socket = createSocketMock() var ioCalls = 0 - global.io = function() { + global.io = function(uri, opts) { ioCalls++ // Create new sockets for subsequent calls @@ -112,6 +112,9 @@ describe('StreamrClient', function() { socket.emit('connect') }) + socket.uri = uri; + socket.opts = opts; + return socket } @@ -121,6 +124,30 @@ describe('StreamrClient', function() { }) describe("connect", function() { + it('should not pass transport details in io() call', function(done) { + client.connect() + client.socket.on("connect", function() { + assert.strictEqual(client.socket.opts["transports"], null) + done() + }) + }) + + context('when client initialized with transport details', function () { + beforeEach(function () { + client = new StreamrClient({ + transports: ["websocket"] + }) + }) + + it('should pass transport details in io() call', function(done) { + client.connect() + client.socket.on("connect", function() { + assert.deepEqual(client.socket.opts["transports"], ["websocket"]) + done() + }) + }) + }) + it('should emit pending subscribes', function(done) { var subscription = client.subscribe("stream1", function(message) {}) client.connect()