Skip to content
Permalink
Browse files

Change transport configuration.

  • Loading branch information...
legastero committed Jul 13, 2019
1 parent 51ca007 commit a88b4e9ea333b2b35e941009848de6a2253d291f
Showing with 108 additions and 114 deletions.
  1. +25 −0 CHANGELOG.md
  2. +4 −4 README.md
  3. +4 −4 docs/Configuring.md
  4. +8 −10 examples/events-demo.html
  5. +9 −9 examples/jingle-demo.html
  6. +43 −61 src/Client.ts
  7. +9 −22 src/index.ts
  8. +6 −4 test/connection.js
@@ -15,6 +15,31 @@
- **Removed WildEmitter.** Now using standard EventEmitter. Event handlers using `*` wildcards will need to be changed.
- The `*` and `raw:*` events are still supported.
- Added input/display helpers for Realtime Text.
- **Configuration of transports has changed.** The wsURL/boshURL/transport fields are no longer used. Configuring transports is now done by setting the `transports` field to an object:

```
XMPP.createClient({
transports: {
websocket: 'wss://...',
bosh: 'https://...'
}
})
```

Using `false` instead of a URL will disable that transport type. An object can be used to pass additional configuration, such as BOSH pre-binding rid/sid:

```
XMPP.createClient({
transports: {
websocket: false,
bosh: {
url: 'https://...',
rid: 1234,
sid: '...'
}
}
})
```

## 11.1.0

@@ -27,10 +27,10 @@ const client = XMPP.createClient({
// If you have a .well-known/host-meta.json file for your
// domain, the connection transport config can be skipped.
transport: 'websocket',
wsURL: 'wss://example.com:5281/xmpp-websocket'
// (or `boshURL` if using 'bosh' as the transport)
transports: {
websocket: 'wss://example.com:5281/xmpp-websocket',
bosh: 'https://example.com:5281/http-bind'
}
});
client.on('session:started', () => {
@@ -180,16 +180,16 @@ It is possible to inspect the configuration later by using `client.config`.
</table>
<p>Manually set the transport type to use instead of auto-discovering.</p>

<h3 id="transports">IQ Timeout</h3>
<h3 id="transports">Transport Configurations</h3>
<table>
<tr><th>Name</th><th>Type</th><th>Default Value</th></tr>
<tr>
<td><code>transports</code></td>
<td><code>string</code> | <code>string[]</code></td>
<td><code>["websocket", "bosh"]</code></td>
<td></td>
<td><code>{ websocket: true, bosh: true }</code></td>
</tr>
</table>
<p>Limit the transport types that will be used.</p>
<p>Limit the transport types that will be used, or specify connection URLs to use without needing to use auto-discovery.</p><p>If a transport is set to `false`, it will be disabled.</p><p>If a transport is set to a string, that will be used as the connection URL.</p><p>If a transport is set to an object, it MUST include a `url` value for the connection URL.</p>

<h3 id="useStreamManagement">Use Stream Management</h3>
<table>
@@ -124,25 +124,23 @@ <h1>Connection Settings</h1>
if (e.preventDefault) e.preventDefault();
var jid = document.getElementById('jid').value;
var url = document.getElementById('url').value;
var wsURL,
boshURL,
transports = ['websocket', 'bosh'];
var transports = {
bosh: true,
websocket: true
};
if (url.indexOf('http') === 0) {
boshURL = url;
transports = ['bosh'];
transports.bosh = url;
transports.websocket = false;
} else if (url.indexOf('ws') === 0) {
wsURL = url;
transports = ['websocket'];
transports.websocket = url;
transports.bosh = false;
}
client = XMPP.createClient({
jid: jid,
password: document.getElementById('password').value,
wsURL: wsURL,
boshURL: boshURL,
transports: transports
});
@@ -93,22 +93,22 @@ <h2>Remote Video</h2>
var server = jid.slice(jid.indexOf('@') + 1);
var url = document.getElementById('url').value;
var wsURL, boshURL, transport;
var transports = {
bosh: true,
websocket: true
};
if (url.indexOf('http') === 0) {
boshURL = url;
transport = 'bosh';
transports.bosh = url;
transports.websocket = false;
} else if (url.indexOf('ws') === 0) {
wsURL = url;
transport = 'websocket';
transports.websocket = url;
transports.bosh = false;
}
client = XMPP.createClient({
jid: jid,
password: document.getElementById('password').value,
wsURL: wsURL,
boshURL: boshURL,
transport: transport
transports: transports
});
client.jingle.config.debug = true;
@@ -1,6 +1,6 @@
import { EventEmitter } from 'events';

import { Agent, AgentConfig, AgentEvents, Transport } from './';
import { Agent, AgentConfig, AgentEvents, Transport, TransportConfig } from './';
import * as JID from './JID';
import * as JXT from './jxt';
import * as SASL from './lib/sasl';
@@ -180,69 +180,54 @@ export default class Client extends EventEmitter {
return this._getConfiguredCredentials();
}

public async connect(
opts?: AgentConfig,
transInfo?: { name?: string; url?: string }
): Promise<void> {
this._initConfig(opts);
if (!transInfo && this.config.transports && this.config.transports.length === 1) {
transInfo = {};
transInfo.name = this.config.transports[0];
}
if (transInfo && transInfo.name) {
const trans = (this.transport = new this.transports[transInfo.name](
public async connect(): Promise<void> {
const transportPref = ['websocket', 'bosh'];
let endpoints: { [key: string]: string[] } | undefined;
for (const name of transportPref) {
let conf = this.config.transports![name];
if (!conf) {
continue;
}
if (typeof conf === 'string') {
conf = { url: conf };
} else if (conf === true) {
if (!endpoints) {
try {
endpoints = await ((this as unknown) as Agent).discoverBindings(
this.config.server!
);
} catch (err) {
console.error(err);
continue;
}
}
endpoints[name] = (endpoints[name] || []).filter(
url => url.startsWith('wss:') || url.startsWith('https:')
);
if (!endpoints[name] || !endpoints[name].length) {
continue;
}
conf = { url: endpoints[name][0] };
}

this.transport = new this.transports[name](
(this as unknown) as Agent,
this.sm,
this.stanzas
));
return trans.connect({
);
this.transport.connect({
acceptLanguages: this.config.acceptLanguages || ['en'],
jid: this.config.jid!,
lang: this.config.lang || 'en',
server: this.config.server!,
url:
(transInfo.name === 'websocket' ? this.config.wsURL : this.config.boshURL) || ''
url: conf.url!,
...conf
});
return;
}

try {
const endpoints = await ((this as unknown) as Agent).discoverBindings(
this.config.server!
);
for (const transport of this.config.transports || []) {
for (let i = 0, len = (endpoints[transport] || []).length; i < len; i++) {
const uri = endpoints[transport][i];
if (uri.indexOf('wss://') === 0 || uri.indexOf('https://') === 0) {
if (transport === 'websocket') {
this.config.wsURL = uri;
} else {
this.config.boshURL = uri;
}
return this.connect(undefined, {
name: transport,
url: uri
});
} else {
console.warn(
'Discovered unencrypted %s endpoint (%s). Ignoring',
transport,
uri
);
}
}
}

console.error('No endpoints found for the requested transports.');
return this.disconnect();
} catch (err) {
console.error(
'Could not find https://' +
this.config.server +
'/.well-known/host-meta file to discover connection endpoints for the requested transports.',
err
);
return this.disconnect();
}
console.error('No endpoints found for the requested transports.');
return this.disconnect();
}

public disconnect() {
@@ -370,7 +355,10 @@ export default class Client extends EventEmitter {
const currConfig = this.config || {};
this.config = {
jid: '',
transports: ['websocket', 'bosh'],
transports: {
bosh: true,
websocket: true
},
useStreamManagement: true,
...currConfig,
...opts
@@ -384,11 +372,5 @@ export default class Client extends EventEmitter {
this.config.credentials.password = this.config.password;
delete this.config.password;
}
if (this.config.transport) {
this.config.transports = [this.config.transport];
}
if (this.config.transports && !Array.isArray(this.config.transports)) {
this.config.transports = [this.config.transports];
}
}
}
@@ -142,34 +142,21 @@ export interface AgentConfig {
timeout?: number;

/**
* IQ Timeout
* Transport Configurations
*
* Limit the transport types that will be used.
* Limit the transport types that will be used, or specify connection
* URLs to use without needing to use auto-discovery.
*
* @default ["websocket", "bosh"]
*/
transports?: string | string[];

/**
* Connection Transport
* If a transport is set to `false`, it will be disabled.
*
* Manually set the transport type to use instead of auto-discovering.
*/
transport?: string;

/**
* WebSocket URL
* If a transport is set to a string, that will be used as the connection URL.
*
* Manually set the WebSocket connection URL to use instead of auto-discovering.
*/
wsURL?: string;

/**
* BOSH URL
* If a transport is set to an object, it MUST include a `url` value for
* the connection URL.
*
* Manually set the BOSH connection URL to use instead of auto-discovering.
* @default { websocket: true, bosh: true }
*/
boshURL?: string;
transports?: { [key: string]: boolean | string | Partial<TransportConfig> };

/**
* Account Password
@@ -6,8 +6,9 @@ test('Connect using WebSocket', function(t) {

const client = stanza.createClient({
jid: 'anon@anon.lance.im',
transport: 'websocket',
wsURL: 'wss://anon.lance.im/xmpp-websocket'
transports: {
websocket: 'wss://anon.lance.im/xmpp-websocket'
}
});

client.on('session:started', function() {
@@ -22,9 +23,10 @@ test('Connect using BOSH', function(t) {
t.plan(1);

const client = stanza.createClient({
boshURL: 'https://anon.lance.im/http-bind',
jid: 'anon@anon.lance.im',
transport: 'bosh'
transports: {
bosh: 'https://anon.lance.im/http-bind'
}
});

client.on('session:started', function() {

0 comments on commit a88b4e9

Please sign in to comment.
You can’t perform that action at this time.