diff --git a/samples/SocketsSample/wwwroot/hubs.html b/samples/SocketsSample/wwwroot/hubs.html index a26c706274..44d97bc93b 100644 --- a/samples/SocketsSample/wwwroot/hubs.html +++ b/samples/SocketsSample/wwwroot/hubs.html @@ -48,12 +48,9 @@ } document.addEventListener('DOMContentLoaded', () => { - var transports = getParameterByName('transport'); - if (transports != null) { - transports = transports.split(',') - } + let transport = getParameterByName('transport') || 'webSockets'; - 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'); connection.on('Send', msg => { @@ -70,7 +67,7 @@ } click('connect', event => { - connection.start(transports) + connection.start(transport) .then(() => { isConnected = true; addLine('Connected successfully', 'green'); diff --git a/src/Microsoft.AspNetCore.SignalR.Client.TS/Connection.ts b/src/Microsoft.AspNetCore.SignalR.Client.TS/Connection.ts index fb03d934f4..d10c0e10d7 100644 --- a/src/Microsoft.AspNetCore.SignalR.Client.TS/Connection.ts +++ b/src/Microsoft.AspNetCore.SignalR.Client.TS/Connection.ts @@ -20,76 +20,44 @@ class Connection { this.connectionState = ConnectionState.Disconnected; } - start(transportNames?: string[]): Promise { + start(transportName: string = 'webSockets'): Promise { if (this.connectionState != ConnectionState.Disconnected) { throw new Error("Cannot start a connection that is not in the 'Disconnected' state"); } - let transports = this.filterTransports(transportNames); - if (transports.length == 0) { - throw new Error("No valid transports requested."); - } + this.transport = this.createTransport(transportName); + this.transport.onDataReceived = this.dataReceivedCallback; + this.transport.onError = e => this.stopConnection(); return new HttpClient().get(`${this.url}/getid?${this.queryString}`) .then(connectionId => { this.connectionId = connectionId; this.queryString = `id=${connectionId}&${this.connectionId}`; - return this.tryStartTransport(transports, 0); + return this.transport.connect(this.url, this.queryString); }) - .then(transport => { - this.transport = transport; + .then(() => { this.connectionState = ConnectionState.Connected; }) .catch(e => { console.log("Failed to start the connection.") this.connectionState = ConnectionState.Disconnected; + this.transport = null; throw e; }); } - private filterTransports(transportNames: string[]): ITransport[] { - let availableTransports = ['webSockets', 'serverSentEvents', 'longPolling']; - transportNames = transportNames || availableTransports; - // uniquify - transportNames = transportNames.filter((value, index, values) => { - return values.indexOf(value) == index; - }); + private createTransport(transportName: string): ITransport { + if (transportName === 'webSockets') { + return new WebSocketTransport(); + } + if (transportName === 'serverSentEvents') { + return new ServerSentEventsTransport(); + } + if (transportName === 'longPolling') { + return new LongPollingTransport(); + } - let transports: ITransport[] = []; - 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 { - 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.') - } - }) + throw new Error("No valid transports requested."); } send(data: any): Promise { @@ -109,6 +77,7 @@ class Connection { private stopConnection(error?: any) { this.transport.stop(); + this.transport = null; this.connectionState = ConnectionState.Disconnected; this.connectionClosedCallback(error); } diff --git a/src/Microsoft.AspNetCore.SignalR.Client.TS/RpcConnection.ts b/src/Microsoft.AspNetCore.SignalR.Client.TS/RpcConnection.ts index 8edb0278e6..5e99486b4c 100644 --- a/src/Microsoft.AspNetCore.SignalR.Client.TS/RpcConnection.ts +++ b/src/Microsoft.AspNetCore.SignalR.Client.TS/RpcConnection.ts @@ -50,8 +50,8 @@ class RpcConnection { } } - start(transportNames?:string[]): Promise { - return this.connection.start(transportNames); + start(transportName? :string): Promise { + return this.connection.start(transportName); } stop(): void {