Enabling creating HubConnection without HttpConnection

This commit is contained in:
Pawel Kadluczka 2017-09-11 13:37:53 -07:00 committed by Pawel Kadluczka
parent 393ab6a4f0
commit bb79a9760c
3 changed files with 17 additions and 10 deletions

View File

@ -3,6 +3,7 @@
import { ConnectionClosed } from "./Common"
import { IConnection } from "./IConnection"
import { HttpConnection} from "./HttpConnection"
import { TransportType, TransferMode } from "./Transports"
import { Subject, Observable } from "./Observable"
import { IHubProtocol, ProtocolType, MessageType, HubMessage, CompletionMessage, ResultMessage, InvocationMessage, NegotiationMessage } from "./IHubProtocol";
@ -28,9 +29,15 @@ export class HubConnection {
private id: number;
private connectionClosedCallback: ConnectionClosed;
constructor(connection: IConnection, options: IHubConnectionOptions = {}) {
this.connection = connection;
constructor(urlOrConnection: string | IConnection, options: IHubConnectionOptions = {}) {
options = options || {};
if (typeof urlOrConnection === "string") {
this.connection = new HttpConnection(urlOrConnection, options);
}
else {
this.connection = urlOrConnection;
}
this.logger = LoggerFactory.createLogger(options.logging);
this.protocol = options.protocol || new JsonHubProtocol();

View File

@ -1,10 +1,10 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
import { IHttpConnectionOptions } from "./IHttpConnectionOptions"
import { IHubProtocol } from "./IHubProtocol"
import { ILogger, LogLevel } from "./ILogger"
export interface IHubConnectionOptions {
export interface IHubConnectionOptions extends IHttpConnectionOptions {
protocol?: IHubProtocol;
logging?: ILogger | LogLevel;
}

View File

@ -17,7 +17,7 @@ describe('hubConnection', function () {
protocol: protocol,
logging: signalR.LogLevel.Trace
};
var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, options), options);
var hubConnection = new signalR.HubConnection(TESTHUBENDPOINT_URL, options);
hubConnection.onClosed = function (error) {
expect(error).toBe(undefined);
done();
@ -44,7 +44,7 @@ describe('hubConnection', function () {
protocol: protocol,
logging: signalR.LogLevel.Trace
};
var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, options), options);
var hubConnection = new signalR.HubConnection(TESTHUBENDPOINT_URL, options);
hubConnection.onClosed = function (error) {
expect(error).toBe(undefined);
@ -79,7 +79,7 @@ describe('hubConnection', function () {
protocol: protocol,
logging: signalR.LogLevel.Trace
};
var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, options), options);
var hubConnection = new signalR.HubConnection(TESTHUBENDPOINT_URL, options);
hubConnection.start().then(function () {
hubConnection.invoke('ThrowException', errorMessage).then(function () {
@ -105,7 +105,7 @@ describe('hubConnection', function () {
protocol: protocol,
logging: signalR.LogLevel.Trace
};
var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, options), options);
var hubConnection = new signalR.HubConnection(TESTHUBENDPOINT_URL, options);
hubConnection.start().then(function () {
hubConnection.stream('ThrowException', errorMessage).subscribe({
@ -132,7 +132,7 @@ describe('hubConnection', function () {
protocol: protocol,
logging: signalR.LogLevel.Trace
};
var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, options), options);
var hubConnection = new signalR.HubConnection(TESTHUBENDPOINT_URL, options);
var message = "你好 SignalR";
@ -170,7 +170,7 @@ describe('hubConnection', function () {
protocol: protocol,
logging: signalR.LogLevel.Trace
};
var hubConnection = new signalR.HubConnection(new signalR.HttpConnection('http://' + document.location.host + '/uncreatable', options), options);
var hubConnection = new signalR.HubConnection('http://' + document.location.host + '/uncreatable', options);
hubConnection.onClosed = function (error) {
expect(error.message).toMatch(errorRegex[signalR.TransportType[transportType]]);