From ebb2ce8fc927035a0457dcefded51eab971564ec Mon Sep 17 00:00:00 2001 From: moozzyk Date: Tue, 28 Mar 2017 09:12:50 -0700 Subject: [PATCH] Converting transport type from string to enum --- .../HubConnection.spec.ts | 5 +++-- .../Connection.ts | 16 ++++++++-------- .../HubConnection.ts | 6 ++++-- .../IConnection.ts | 3 ++- .../Transports.ts | 6 ++++++ .../wwwroot/js/common.js | 7 +++++-- .../wwwroot/js/connectionTests.js | 6 +++--- .../wwwroot/js/hubConnectionTests.js | 14 +++++++------- samples/SocketsSample/wwwroot/hubs.html | 6 +++--- samples/SocketsSample/wwwroot/index.html | 12 ++++++------ samples/SocketsSample/wwwroot/sockets.html | 6 +++--- 11 files changed, 50 insertions(+), 37 deletions(-) diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS.Tests/HubConnection.spec.ts b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS.Tests/HubConnection.spec.ts index 9d9c8fcae8..8bc23fc3e0 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS.Tests/HubConnection.spec.ts +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS.Tests/HubConnection.spec.ts @@ -1,11 +1,12 @@ import { IConnection } from "../Microsoft.AspNetCore.SignalR.Client.TS/IConnection" import { HubConnection } from "../Microsoft.AspNetCore.SignalR.Client.TS/HubConnection" import { DataReceived, ConnectionClosed } from "../Microsoft.AspNetCore.SignalR.Client.TS/Common" +import { TransportType } from "../Microsoft.AspNetCore.SignalR.Client.TS/Transports" describe("HubConnection", () => { it("completes pending invocations when stopped", async (done) => { let connection: IConnection = { - start(transportName: string): Promise { + start(transportType: TransportType): Promise { return Promise.resolve(); }, @@ -39,7 +40,7 @@ describe("HubConnection", () => { it("completes pending invocations when connection is lost", async (done) => { let connection: IConnection = { - start(transportName: string): Promise { + start(transportType: TransportType): Promise { return Promise.resolve(); }, diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/Connection.ts b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/Connection.ts index 3fb7b1ef41..74944fc46d 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/Connection.ts +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/Connection.ts @@ -1,6 +1,6 @@ import { DataReceived, ConnectionClosed } from "./Common" import { IConnection } from "./IConnection" -import { ITransport, WebSocketTransport, ServerSentEventsTransport, LongPollingTransport } from "./Transports" +import { ITransport, TransportType, WebSocketTransport, ServerSentEventsTransport, LongPollingTransport } from "./Transports" import { IHttpClient, HttpClient } from "./HttpClient" import { ISignalROptions } from "./ISignalROptions" @@ -26,14 +26,14 @@ export class Connection implements IConnection { this.connectionState = ConnectionState.Initial; } - async start(transportName: string = "webSockets"): Promise { + async start(transportType: TransportType = TransportType.WebSockets): Promise { if (this.connectionState != ConnectionState.Initial) { throw new Error("Cannot start a connection that is not in the 'Initial' state."); } this.connectionState = ConnectionState.Connecting; - this.transport = this.createTransport(transportName); + this.transport = this.createTransport(transportType); this.transport.onDataReceived = this.onDataReceived; this.transport.onClosed = e => this.stopConnection(e); @@ -44,21 +44,21 @@ export class Connection implements IConnection { this.connectionState = ConnectionState.Connected; } catch(e) { - console.log("Failed to start the connection.") + console.log("Failed to start the connection. " + e) this.connectionState = ConnectionState.Disconnected; this.transport = null; throw e; }; } - private createTransport(transportName: string): ITransport { - if (transportName === "webSockets") { + private createTransport(transportType: TransportType): ITransport { + if (transportType === TransportType.WebSockets) { return new WebSocketTransport(); } - if (transportName === "serverSentEvents") { + if (transportType === TransportType.ServerSentEvents) { return new ServerSentEventsTransport(this.httpClient); } - if (transportName === "longPolling") { + if (transportType === TransportType.LongPolling) { return new LongPollingTransport(this.httpClient); } diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts index 7e9a03f739..7f6c78a95b 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts @@ -1,6 +1,7 @@ import { ConnectionClosed } from "./Common" import { IConnection } from "./IConnection" import { Connection } from "./Connection" +import { TransportType } from "./Transports" interface InvocationDescriptor { readonly Id: string; @@ -15,6 +16,7 @@ interface InvocationResultDescriptor { } export { Connection } from "./Connection" +export { TransportType } from "./Transports" export class HubConnection { private connection: IConnection; @@ -85,8 +87,8 @@ export class HubConnection { } } - start(transportName? :string): Promise { - return this.connection.start(transportName); + start(transportType?: TransportType): Promise { + return this.connection.start(transportType); } stop(): void { diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/IConnection.ts b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/IConnection.ts index 29bb972d49..3ac44fe4f0 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/IConnection.ts +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/IConnection.ts @@ -1,7 +1,8 @@ import { DataReceived, ConnectionClosed } from "./Common" +import { TransportType } from "./Transports" export interface IConnection { - start(transportName: string): Promise; + start(transportType: TransportType): Promise; send(data: any): Promise; stop(): void; diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/Transports.ts b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/Transports.ts index ca23076620..a0809063fe 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/Transports.ts +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/Transports.ts @@ -2,6 +2,12 @@ import { DataReceived, TransportClosed } from "./Common" import { IHttpClient } from "./HttpClient" import * as Formatters from "./Formatters"; +export enum TransportType { + WebSockets, + ServerSentEvents, + LongPolling +} + export interface ITransport { connect(url: string, queryString: string): Promise; send(data: any): Promise; diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/common.js b/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/common.js index 5ef76470f1..221babe7e4 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/common.js +++ b/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/common.js @@ -1,6 +1,9 @@ const ECHOENDPOINT_URL = `http://${document.location.host}/echo`; function eachTransport(action) { - let transportNames = ["webSockets", "serverSentEvents", "longPolling"]; - transportNames.forEach(t => action(t)); + let transportTypes = [ + signalR.TransportType.WebSockets, + signalR.TransportType.ServerSentEvents, + signalR.TransportType.LongPolling ]; + transportTypes.forEach(t => action(t)); } diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/connectionTests.js b/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/connectionTests.js index b3da98a027..88d0f419bf 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/connectionTests.js +++ b/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/connectionTests.js @@ -1,6 +1,6 @@ describe('connection', () => { - eachTransport(transportName => { - it(`over ${transportName} can send and receive messages`, done => { + eachTransport(transportType => { + it(`over ${signalR.TransportType[transportType]} can send and receive messages`, done => { const message = "Hello World!"; let connection = new signalR.Connection(ECHOENDPOINT_URL); @@ -17,7 +17,7 @@ describe('connection', () => { done(); } - connection.start(transportName) + connection.start(transportType) .then(() => { connection.send(message); }) 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 83f27349fc..99d172bac6 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 @@ -1,8 +1,8 @@ const TESTHUBENDPOINT_URL = `http://${document.location.host}/testhub`; describe('hubConnection', () => { - eachTransport(transportName => { - it(`over ${transportName} can invoke server method and receive result`, done => { + eachTransport(transportType => { + it(`over ${signalR.TransportType[transportType]} can invoke server method and receive result`, done => { const message = "Hi"; let hubConnection = new signalR.HubConnection(TESTHUBENDPOINT_URL, 'formatType=json&format=text'); hubConnection.onClosed = error => { @@ -10,7 +10,7 @@ describe('hubConnection', () => { done(); } - hubConnection.start(transportName) + hubConnection.start(transportType) .then(() => { hubConnection.invoke('Echo', message) .then(result => { @@ -29,11 +29,11 @@ describe('hubConnection', () => { }); }); - it(`over ${transportName} rethrows an exception from the server`, done => { + it(`over ${signalR.TransportType[transportType]} rethrows an exception from the server`, done => { const errorMessage = "An error occurred."; let hubConnection = new signalR.HubConnection(TESTHUBENDPOINT_URL, 'formatType=json&format=text'); - hubConnection.start(transportName) + hubConnection.start(transportType) .then(() => { hubConnection.invoke('ThrowException', errorMessage) .then(() => { @@ -54,7 +54,7 @@ describe('hubConnection', () => { }); }); - it(`over ${transportName} can receive server calls`, done => { + it(`over ${signalR.TransportType[transportType]} can receive server calls`, done => { let client = new signalR.HubConnection(TESTHUBENDPOINT_URL, 'formatType=json&format=text'); const message = "Hello SignalR"; @@ -65,7 +65,7 @@ describe('hubConnection', () => { }); }); - client.start(transportName) + client.start(transportType) .then(() => { return Promise.all([client.invoke('InvokeWithString', message), callbackPromise]); }) diff --git a/samples/SocketsSample/wwwroot/hubs.html b/samples/SocketsSample/wwwroot/hubs.html index d4dd1a6e91..027665a34c 100644 --- a/samples/SocketsSample/wwwroot/hubs.html +++ b/samples/SocketsSample/wwwroot/hubs.html @@ -93,9 +93,9 @@ function addLine(line, color) { document.getElementById('messages').appendChild(child); } -let transport = getParameterByName('transport') || 'webSockets'; +let transportType = signalR.TransportType[getParameterByName('transport')] || signalR.TransportType.WebSockets; -document.getElementById('head1').innerHTML = transport; +document.getElementById('head1').innerHTML = signalR.TransportType[transportType]; let connection = new signalR.HubConnection(`http://${document.location.host}/hubs`, 'formatType=json&format=text'); connection.on('Send', msg => { @@ -112,7 +112,7 @@ connection.onClosed = e => { } click('connect', event => { - connection.start(transport) + connection.start(transportType) .then(() => { isConnected = true; addLine('Connected successfully', 'green'); diff --git a/samples/SocketsSample/wwwroot/index.html b/samples/SocketsSample/wwwroot/index.html index 3fa89d97e0..f8404615c5 100644 --- a/samples/SocketsSample/wwwroot/index.html +++ b/samples/SocketsSample/wwwroot/index.html @@ -7,16 +7,16 @@

ASP.NET Sockets

ASP.NET SignalR (Hubs)

diff --git a/samples/SocketsSample/wwwroot/sockets.html b/samples/SocketsSample/wwwroot/sockets.html index e25e8cb9ff..d91cd91c6a 100644 --- a/samples/SocketsSample/wwwroot/sockets.html +++ b/samples/SocketsSample/wwwroot/sockets.html @@ -17,9 +17,9 @@