Skip to content
This repository was archived by the owner on Dec 21, 2021. It is now read-only.

Commit a10b9ad

Browse files
committed
Merge pull request #3 from Unifina/transport-options
Allow overriding of transport options
2 parents be61047 + 2751c9a commit a10b9ad

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ Option | Default value | Description
4646
server | api.streamr.com | Address of the server to connect to.
4747
autoConnect | true | If set to `true`, the client connects automatically on the first call to `subscribe()`. Otherwise an explicit call to `connect()` is required.
4848
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()`.
49+
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.
50+
4951

5052
## Resend options
5153

streamr-client.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,9 @@ function StreamrClient(options) {
256256
// Automatically connect on first subscribe
257257
autoConnect: true,
258258
// Automatically disconnect on last unsubscribe
259-
autoDisconnect: true
259+
autoDisconnect: true,
260+
// Allow client socket library to choose appropriate transport
261+
transports: null
260262
}
261263
this.subsByStream = {}
262264
this.subById = {}
@@ -384,7 +386,10 @@ StreamrClient.prototype.connect = function(reconnect) {
384386
this.connecting = true
385387
this.disconnecting = false
386388

387-
this.socket = io(this.options.server, {forceNew: true})
389+
this.socket = io(this.options.server, {
390+
forceNew: true,
391+
transports: this.options.transports
392+
})
388393

389394
this.socket.on('ui', function(data) {
390395
if (typeof data == 'string' || data instanceof String) {

test/test.streamr-client.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ describe('StreamrClient', function() {
100100
socket = createSocketMock()
101101

102102
var ioCalls = 0
103-
global.io = function() {
103+
global.io = function(uri, opts) {
104104
ioCalls++
105105

106106
// Create new sockets for subsequent calls
@@ -112,6 +112,9 @@ describe('StreamrClient', function() {
112112
socket.emit('connect')
113113
})
114114

115+
socket.uri = uri;
116+
socket.opts = opts;
117+
115118
return socket
116119
}
117120

@@ -121,6 +124,30 @@ describe('StreamrClient', function() {
121124
})
122125

123126
describe("connect", function() {
127+
it('should not pass transport details in io() call', function(done) {
128+
client.connect()
129+
client.socket.on("connect", function() {
130+
assert.strictEqual(client.socket.opts["transports"], null)
131+
done()
132+
})
133+
})
134+
135+
context('when client initialized with transport details', function () {
136+
beforeEach(function () {
137+
client = new StreamrClient({
138+
transports: ["websocket"]
139+
})
140+
})
141+
142+
it('should pass transport details in io() call', function(done) {
143+
client.connect()
144+
client.socket.on("connect", function() {
145+
assert.deepEqual(client.socket.opts["transports"], ["websocket"])
146+
done()
147+
})
148+
})
149+
})
150+
124151
it('should emit pending subscribes', function(done) {
125152
var subscription = client.subscribe("stream1", function(message) {})
126153
client.connect()

0 commit comments

Comments
 (0)