Removing transport fallback in the client

This commit is contained in:
moozzyk 2016-11-11 11:58:56 -08:00
parent 97bf8c3c09
commit 325c909dff
3 changed files with 24 additions and 58 deletions

View File

@ -48,12 +48,9 @@
} }
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
var transports = getParameterByName('transport'); let transport = getParameterByName('transport') || 'webSockets';
if (transports != null) {
transports = transports.split(',')
}
document.getElementById('head1').innerHTML = transports ? transports.join(', ') : "auto (WebSockets)"; document.getElementById('head1').innerHTML = transport;
let connection = new RpcConnection(`http://${document.location.host}/hubs`, 'formatType=json&format=text'); let connection = new RpcConnection(`http://${document.location.host}/hubs`, 'formatType=json&format=text');
connection.on('Send', msg => { connection.on('Send', msg => {
@ -70,7 +67,7 @@
} }
click('connect', event => { click('connect', event => {
connection.start(transports) connection.start(transport)
.then(() => { .then(() => {
isConnected = true; isConnected = true;
addLine('Connected successfully', 'green'); addLine('Connected successfully', 'green');

View File

@ -20,76 +20,44 @@ class Connection {
this.connectionState = ConnectionState.Disconnected; this.connectionState = ConnectionState.Disconnected;
} }
start(transportNames?: string[]): Promise<void> { start(transportName: string = 'webSockets'): Promise<void> {
if (this.connectionState != ConnectionState.Disconnected) { if (this.connectionState != ConnectionState.Disconnected) {
throw new Error("Cannot start a connection that is not in the 'Disconnected' state"); throw new Error("Cannot start a connection that is not in the 'Disconnected' state");
} }
let transports = this.filterTransports(transportNames); this.transport = this.createTransport(transportName);
if (transports.length == 0) { this.transport.onDataReceived = this.dataReceivedCallback;
throw new Error("No valid transports requested."); this.transport.onError = e => this.stopConnection();
}
return new HttpClient().get(`${this.url}/getid?${this.queryString}`) return new HttpClient().get(`${this.url}/getid?${this.queryString}`)
.then(connectionId => { .then(connectionId => {
this.connectionId = connectionId; this.connectionId = connectionId;
this.queryString = `id=${connectionId}&${this.connectionId}`; this.queryString = `id=${connectionId}&${this.connectionId}`;
return this.tryStartTransport(transports, 0); return this.transport.connect(this.url, this.queryString);
}) })
.then(transport => { .then(() => {
this.transport = transport;
this.connectionState = ConnectionState.Connected; this.connectionState = ConnectionState.Connected;
}) })
.catch(e => { .catch(e => {
console.log("Failed to start the connection.") console.log("Failed to start the connection.")
this.connectionState = ConnectionState.Disconnected; this.connectionState = ConnectionState.Disconnected;
this.transport = null;
throw e; throw e;
}); });
} }
private filterTransports(transportNames: string[]): ITransport[] { private createTransport(transportName: string): ITransport {
let availableTransports = ['webSockets', 'serverSentEvents', 'longPolling']; if (transportName === 'webSockets') {
transportNames = transportNames || availableTransports; return new WebSocketTransport();
// uniquify }
transportNames = transportNames.filter((value, index, values) => { if (transportName === 'serverSentEvents') {
return values.indexOf(value) == index; return new ServerSentEventsTransport();
}); }
if (transportName === 'longPolling') {
return new LongPollingTransport();
}
let transports: ITransport[] = []; throw new Error("No valid transports requested.");
transportNames.forEach(transportName => {
if (transportName === 'webSockets') {
transports.push(new WebSocketTransport());
}
if (transportName === 'serverSentEvents') {
transports.push(new ServerSentEventsTransport());
}
if (transportName === 'longPolling') {
transports.push(new LongPollingTransport());
}
});
return transports;
}
private tryStartTransport(transports: ITransport[], index: number): Promise<ITransport> {
let thisConnection = this;
transports[index].onDataReceived = data => thisConnection.dataReceivedCallback(data);
transports[index].onError = e => thisConnection.stopConnection(e);
return transports[index].connect(this.url, this.queryString)
.then(() => {
return transports[index];
})
.catch(e => {
index++;
if (index < transports.length) {
return this.tryStartTransport(transports, index);
}
else
{
throw new Error('No transport could be started.')
}
})
} }
send(data: any): Promise<void> { send(data: any): Promise<void> {
@ -109,6 +77,7 @@ class Connection {
private stopConnection(error?: any) { private stopConnection(error?: any) {
this.transport.stop(); this.transport.stop();
this.transport = null;
this.connectionState = ConnectionState.Disconnected; this.connectionState = ConnectionState.Disconnected;
this.connectionClosedCallback(error); this.connectionClosedCallback(error);
} }

View File

@ -50,8 +50,8 @@ class RpcConnection {
} }
} }
start(transportNames?:string[]): Promise<void> { start(transportName? :string): Promise<void> {
return this.connection.start(transportNames); return this.connection.start(transportName);
} }
stop(): void { stop(): void {