From bb79a9760c7052622bbc5aaeabb9fe7a600b9b84 Mon Sep 17 00:00:00 2001 From: Pawel Kadluczka Date: Mon, 11 Sep 2017 13:37:53 -0700 Subject: [PATCH] Enabling creating HubConnection without HttpConnection --- .../HubConnection.ts | 11 +++++++++-- .../IHubConnectionOptions.ts | 4 ++-- .../wwwroot/js/hubConnectionTests.js | 12 ++++++------ 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts index 8c36667fc3..fa6f8f93fa 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts @@ -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(); diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/IHubConnectionOptions.ts b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/IHubConnectionOptions.ts index 7e41c9649e..9aa80a1cfe 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/IHubConnectionOptions.ts +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/IHubConnectionOptions.ts @@ -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; } diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/hubConnectionTests.js b/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/hubConnectionTests.js index f58435025a..c53ba3d710 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/hubConnectionTests.js +++ b/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/hubConnectionTests.js @@ -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]]);