Remove ITransport dependency on IConnection (#1934)

This commit is contained in:
David Fowler 2018-04-10 12:29:16 -07:00 committed by GitHub
parent 8a3516284e
commit 5aafb9b8be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 15 deletions

View File

@ -85,7 +85,7 @@ export class HttpConnection implements IConnection {
this.transport = this.constructTransport(TransportType.WebSockets); this.transport = this.constructTransport(TransportType.WebSockets);
// We should just call connect directly in this case. // We should just call connect directly in this case.
// No fallback or negotiate in this case. // No fallback or negotiate in this case.
await this.transport.connect(this.url, transferFormat, this); await this.transport.connect(this.url, transferFormat);
} else { } else {
const token = await this.options.accessTokenFactory(); const token = await this.options.accessTokenFactory();
let headers; let headers;
@ -103,6 +103,10 @@ export class HttpConnection implements IConnection {
await this.createTransport(this.options.transport, negotiateResponse, transferFormat, headers); await this.createTransport(this.options.transport, negotiateResponse, transferFormat, headers);
} }
if (typeof this.transport === typeof LongPollingTransport) {
this.features.inherentKeepAlive = true;
}
this.transport.onreceive = this.onreceive; this.transport.onreceive = this.onreceive;
this.transport.onclose = (e) => this.stopConnection(true, e); this.transport.onclose = (e) => this.stopConnection(true, e);
@ -121,7 +125,7 @@ export class HttpConnection implements IConnection {
const negotiateUrl = this.resolveNegotiateUrl(this.baseUrl); const negotiateUrl = this.resolveNegotiateUrl(this.baseUrl);
this.logger.log(LogLevel.Trace, `Sending negotiation request: ${negotiateUrl}`); this.logger.log(LogLevel.Trace, `Sending negotiation request: ${negotiateUrl}`);
try { try {
const response = await this.httpClient.post(negotiateUrl, { const response = await this.httpClient.post(negotiateUrl, {
content: "", content: "",
headers, headers,
}); });
@ -142,7 +146,7 @@ export class HttpConnection implements IConnection {
if (this.isITransport(requestedTransport)) { if (this.isITransport(requestedTransport)) {
this.logger.log(LogLevel.Trace, "Connection was provided an instance of ITransport, using that directly."); this.logger.log(LogLevel.Trace, "Connection was provided an instance of ITransport, using that directly.");
this.transport = requestedTransport; this.transport = requestedTransport;
await this.transport.connect(this.url, requestedTransferFormat, this); await this.transport.connect(this.url, requestedTransferFormat);
// only change the state if we were connecting to not overwrite // only change the state if we were connecting to not overwrite
// the state if the connection is already marked as Disconnected // the state if the connection is already marked as Disconnected
@ -161,7 +165,7 @@ export class HttpConnection implements IConnection {
this.updateConnectionId(negotiateResponse); this.updateConnectionId(negotiateResponse);
} }
try { try {
await this.transport.connect(this.url, requestedTransferFormat, this); await this.transport.connect(this.url, requestedTransferFormat);
this.changeState(ConnectionState.Connecting, ConnectionState.Connected); this.changeState(ConnectionState.Connecting, ConnectionState.Connected);
return; return;
} catch (ex) { } catch (ex) {

View File

@ -5,7 +5,6 @@ import { AbortController } from "./AbortController";
import { DataReceived, TransportClosed } from "./Common"; import { DataReceived, TransportClosed } from "./Common";
import { HttpError, TimeoutError } from "./Errors"; import { HttpError, TimeoutError } from "./Errors";
import { HttpClient, HttpRequest } from "./HttpClient"; import { HttpClient, HttpRequest } from "./HttpClient";
import { IConnection } from "./IConnection";
import { ILogger, LogLevel } from "./ILogger"; import { ILogger, LogLevel } from "./ILogger";
import { Arg } from "./Utils"; import { Arg } from "./Utils";
@ -21,7 +20,7 @@ export enum TransferFormat {
} }
export interface ITransport { export interface ITransport {
connect(url: string, transferFormat: TransferFormat, connection: IConnection): Promise<void>; connect(url: string, transferFormat: TransferFormat): Promise<void>;
send(data: any): Promise<void>; send(data: any): Promise<void>;
stop(): Promise<void>; stop(): Promise<void>;
onreceive: DataReceived; onreceive: DataReceived;
@ -40,11 +39,10 @@ export class WebSocketTransport implements ITransport {
this.logMessageContent = logMessageContent; this.logMessageContent = logMessageContent;
} }
public async connect(url: string, transferFormat: TransferFormat, connection: IConnection): Promise<void> { public async connect(url: string, transferFormat: TransferFormat): Promise<void> {
Arg.isRequired(url, "url"); Arg.isRequired(url, "url");
Arg.isRequired(transferFormat, "transferFormat"); Arg.isRequired(transferFormat, "transferFormat");
Arg.isIn(transferFormat, TransferFormat, "transferFormat"); Arg.isIn(transferFormat, TransferFormat, "transferFormat");
Arg.isRequired(connection, "connection");
if (typeof (WebSocket) === "undefined") { if (typeof (WebSocket) === "undefined") {
throw new Error("'WebSocket' is not supported in your environment."); throw new Error("'WebSocket' is not supported in your environment.");
@ -131,11 +129,10 @@ export class ServerSentEventsTransport implements ITransport {
this.logMessageContent = logMessageContent; this.logMessageContent = logMessageContent;
} }
public async connect(url: string, transferFormat: TransferFormat, connection: IConnection): Promise<void> { public async connect(url: string, transferFormat: TransferFormat): Promise<void> {
Arg.isRequired(url, "url"); Arg.isRequired(url, "url");
Arg.isRequired(transferFormat, "transferFormat"); Arg.isRequired(transferFormat, "transferFormat");
Arg.isIn(transferFormat, TransferFormat, "transferFormat"); Arg.isIn(transferFormat, TransferFormat, "transferFormat");
Arg.isRequired(connection, "connection");
if (typeof (EventSource) === "undefined") { if (typeof (EventSource) === "undefined") {
throw new Error("'EventSource' is not supported in your environment."); throw new Error("'EventSource' is not supported in your environment.");
@ -226,19 +223,15 @@ export class LongPollingTransport implements ITransport {
this.logMessageContent = logMessageContent; this.logMessageContent = logMessageContent;
} }
public connect(url: string, transferFormat: TransferFormat, connection: IConnection): Promise<void> { public connect(url: string, transferFormat: TransferFormat): Promise<void> {
Arg.isRequired(url, "url"); Arg.isRequired(url, "url");
Arg.isRequired(transferFormat, "transferFormat"); Arg.isRequired(transferFormat, "transferFormat");
Arg.isIn(transferFormat, TransferFormat, "transferFormat"); Arg.isIn(transferFormat, TransferFormat, "transferFormat");
Arg.isRequired(connection, "connection");
this.url = url; this.url = url;
this.logger.log(LogLevel.Trace, "(LongPolling transport) Connecting"); this.logger.log(LogLevel.Trace, "(LongPolling transport) Connecting");
// Set a flag indicating we have inherent keep-alive in this transport.
connection.features.inherentKeepAlive = true;
if (transferFormat === TransferFormat.Binary && (typeof new XMLHttpRequest().responseType !== "string")) { if (transferFormat === TransferFormat.Binary && (typeof new XMLHttpRequest().responseType !== "string")) {
// This will work if we fix: https://github.com/aspnet/SignalR/issues/742 // This will work if we fix: https://github.com/aspnet/SignalR/issues/742
throw new Error("Binary protocols over XmlHttpRequest not implementing advanced features are not supported."); throw new Error("Binary protocols over XmlHttpRequest not implementing advanced features are not supported.");