Removing transport fallback in the client
This commit is contained in:
parent
97bf8c3c09
commit
325c909dff
|
|
@ -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');
|
||||
|
|
|
|||
|
|
@ -20,76 +20,44 @@ class Connection {
|
|||
this.connectionState = ConnectionState.Disconnected;
|
||||
}
|
||||
|
||||
start(transportNames?: string[]): Promise<void> {
|
||||
start(transportName: string = 'webSockets'): Promise<void> {
|
||||
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<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.')
|
||||
}
|
||||
})
|
||||
throw new Error("No valid transports requested.");
|
||||
}
|
||||
|
||||
send(data: any): Promise<void> {
|
||||
|
|
@ -109,6 +77,7 @@ class Connection {
|
|||
|
||||
private stopConnection(error?: any) {
|
||||
this.transport.stop();
|
||||
this.transport = null;
|
||||
this.connectionState = ConnectionState.Disconnected;
|
||||
this.connectionClosedCallback(error);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,8 +50,8 @@ class RpcConnection {
|
|||
}
|
||||
}
|
||||
|
||||
start(transportNames?:string[]): Promise<void> {
|
||||
return this.connection.start(transportNames);
|
||||
start(transportName? :string): Promise<void> {
|
||||
return this.connection.start(transportName);
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
|
|
|
|||
Loading…
Reference in New Issue