Converting transport type from string to enum

This commit is contained in:
moozzyk 2017-03-28 09:12:50 -07:00
parent 7944be712f
commit ebb2ce8fc9
11 changed files with 50 additions and 37 deletions

View File

@ -1,11 +1,12 @@
import { IConnection } from "../Microsoft.AspNetCore.SignalR.Client.TS/IConnection" import { IConnection } from "../Microsoft.AspNetCore.SignalR.Client.TS/IConnection"
import { HubConnection } from "../Microsoft.AspNetCore.SignalR.Client.TS/HubConnection" import { HubConnection } from "../Microsoft.AspNetCore.SignalR.Client.TS/HubConnection"
import { DataReceived, ConnectionClosed } from "../Microsoft.AspNetCore.SignalR.Client.TS/Common" import { DataReceived, ConnectionClosed } from "../Microsoft.AspNetCore.SignalR.Client.TS/Common"
import { TransportType } from "../Microsoft.AspNetCore.SignalR.Client.TS/Transports"
describe("HubConnection", () => { describe("HubConnection", () => {
it("completes pending invocations when stopped", async (done) => { it("completes pending invocations when stopped", async (done) => {
let connection: IConnection = { let connection: IConnection = {
start(transportName: string): Promise<void> { start(transportType: TransportType): Promise<void> {
return Promise.resolve(); return Promise.resolve();
}, },
@ -39,7 +40,7 @@ describe("HubConnection", () => {
it("completes pending invocations when connection is lost", async (done) => { it("completes pending invocations when connection is lost", async (done) => {
let connection: IConnection = { let connection: IConnection = {
start(transportName: string): Promise<void> { start(transportType: TransportType): Promise<void> {
return Promise.resolve(); return Promise.resolve();
}, },

View File

@ -1,6 +1,6 @@
import { DataReceived, ConnectionClosed } from "./Common" import { DataReceived, ConnectionClosed } from "./Common"
import { IConnection } from "./IConnection" 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 { IHttpClient, HttpClient } from "./HttpClient"
import { ISignalROptions } from "./ISignalROptions" import { ISignalROptions } from "./ISignalROptions"
@ -26,14 +26,14 @@ export class Connection implements IConnection {
this.connectionState = ConnectionState.Initial; this.connectionState = ConnectionState.Initial;
} }
async start(transportName: string = "webSockets"): Promise<void> { async start(transportType: TransportType = TransportType.WebSockets): Promise<void> {
if (this.connectionState != ConnectionState.Initial) { if (this.connectionState != ConnectionState.Initial) {
throw new Error("Cannot start a connection that is not in the 'Initial' state."); throw new Error("Cannot start a connection that is not in the 'Initial' state.");
} }
this.connectionState = ConnectionState.Connecting; this.connectionState = ConnectionState.Connecting;
this.transport = this.createTransport(transportName); this.transport = this.createTransport(transportType);
this.transport.onDataReceived = this.onDataReceived; this.transport.onDataReceived = this.onDataReceived;
this.transport.onClosed = e => this.stopConnection(e); this.transport.onClosed = e => this.stopConnection(e);
@ -44,21 +44,21 @@ export class Connection implements IConnection {
this.connectionState = ConnectionState.Connected; this.connectionState = ConnectionState.Connected;
} }
catch(e) { catch(e) {
console.log("Failed to start the connection.") console.log("Failed to start the connection. " + e)
this.connectionState = ConnectionState.Disconnected; this.connectionState = ConnectionState.Disconnected;
this.transport = null; this.transport = null;
throw e; throw e;
}; };
} }
private createTransport(transportName: string): ITransport { private createTransport(transportType: TransportType): ITransport {
if (transportName === "webSockets") { if (transportType === TransportType.WebSockets) {
return new WebSocketTransport(); return new WebSocketTransport();
} }
if (transportName === "serverSentEvents") { if (transportType === TransportType.ServerSentEvents) {
return new ServerSentEventsTransport(this.httpClient); return new ServerSentEventsTransport(this.httpClient);
} }
if (transportName === "longPolling") { if (transportType === TransportType.LongPolling) {
return new LongPollingTransport(this.httpClient); return new LongPollingTransport(this.httpClient);
} }

View File

@ -1,6 +1,7 @@
import { ConnectionClosed } from "./Common" import { ConnectionClosed } from "./Common"
import { IConnection } from "./IConnection" import { IConnection } from "./IConnection"
import { Connection } from "./Connection" import { Connection } from "./Connection"
import { TransportType } from "./Transports"
interface InvocationDescriptor { interface InvocationDescriptor {
readonly Id: string; readonly Id: string;
@ -15,6 +16,7 @@ interface InvocationResultDescriptor {
} }
export { Connection } from "./Connection" export { Connection } from "./Connection"
export { TransportType } from "./Transports"
export class HubConnection { export class HubConnection {
private connection: IConnection; private connection: IConnection;
@ -85,8 +87,8 @@ export class HubConnection {
} }
} }
start(transportName? :string): Promise<void> { start(transportType?: TransportType): Promise<void> {
return this.connection.start(transportName); return this.connection.start(transportType);
} }
stop(): void { stop(): void {

View File

@ -1,7 +1,8 @@
import { DataReceived, ConnectionClosed } from "./Common" import { DataReceived, ConnectionClosed } from "./Common"
import { TransportType } from "./Transports"
export interface IConnection { export interface IConnection {
start(transportName: string): Promise<void>; start(transportType: TransportType): Promise<void>;
send(data: any): Promise<void>; send(data: any): Promise<void>;
stop(): void; stop(): void;

View File

@ -2,6 +2,12 @@ import { DataReceived, TransportClosed } from "./Common"
import { IHttpClient } from "./HttpClient" import { IHttpClient } from "./HttpClient"
import * as Formatters from "./Formatters"; import * as Formatters from "./Formatters";
export enum TransportType {
WebSockets,
ServerSentEvents,
LongPolling
}
export interface ITransport { export interface ITransport {
connect(url: string, queryString: string): Promise<void>; connect(url: string, queryString: string): Promise<void>;
send(data: any): Promise<void>; send(data: any): Promise<void>;

View File

@ -1,6 +1,9 @@
const ECHOENDPOINT_URL = `http://${document.location.host}/echo`; const ECHOENDPOINT_URL = `http://${document.location.host}/echo`;
function eachTransport(action) { function eachTransport(action) {
let transportNames = ["webSockets", "serverSentEvents", "longPolling"]; let transportTypes = [
transportNames.forEach(t => action(t)); signalR.TransportType.WebSockets,
signalR.TransportType.ServerSentEvents,
signalR.TransportType.LongPolling ];
transportTypes.forEach(t => action(t));
} }

View File

@ -1,6 +1,6 @@
describe('connection', () => { describe('connection', () => {
eachTransport(transportName => { eachTransport(transportType => {
it(`over ${transportName} can send and receive messages`, done => { it(`over ${signalR.TransportType[transportType]} can send and receive messages`, done => {
const message = "Hello World!"; const message = "Hello World!";
let connection = new signalR.Connection(ECHOENDPOINT_URL); let connection = new signalR.Connection(ECHOENDPOINT_URL);
@ -17,7 +17,7 @@ describe('connection', () => {
done(); done();
} }
connection.start(transportName) connection.start(transportType)
.then(() => { .then(() => {
connection.send(message); connection.send(message);
}) })

View File

@ -1,8 +1,8 @@
const TESTHUBENDPOINT_URL = `http://${document.location.host}/testhub`; const TESTHUBENDPOINT_URL = `http://${document.location.host}/testhub`;
describe('hubConnection', () => { describe('hubConnection', () => {
eachTransport(transportName => { eachTransport(transportType => {
it(`over ${transportName} can invoke server method and receive result`, done => { it(`over ${signalR.TransportType[transportType]} can invoke server method and receive result`, done => {
const message = "Hi"; const message = "Hi";
let hubConnection = new signalR.HubConnection(TESTHUBENDPOINT_URL, 'formatType=json&format=text'); let hubConnection = new signalR.HubConnection(TESTHUBENDPOINT_URL, 'formatType=json&format=text');
hubConnection.onClosed = error => { hubConnection.onClosed = error => {
@ -10,7 +10,7 @@ describe('hubConnection', () => {
done(); done();
} }
hubConnection.start(transportName) hubConnection.start(transportType)
.then(() => { .then(() => {
hubConnection.invoke('Echo', message) hubConnection.invoke('Echo', message)
.then(result => { .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."; const errorMessage = "An error occurred.";
let hubConnection = new signalR.HubConnection(TESTHUBENDPOINT_URL, 'formatType=json&format=text'); let hubConnection = new signalR.HubConnection(TESTHUBENDPOINT_URL, 'formatType=json&format=text');
hubConnection.start(transportName) hubConnection.start(transportType)
.then(() => { .then(() => {
hubConnection.invoke('ThrowException', errorMessage) hubConnection.invoke('ThrowException', errorMessage)
.then(() => { .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'); let client = new signalR.HubConnection(TESTHUBENDPOINT_URL, 'formatType=json&format=text');
const message = "Hello SignalR"; const message = "Hello SignalR";
@ -65,7 +65,7 @@ describe('hubConnection', () => {
}); });
}); });
client.start(transportName) client.start(transportType)
.then(() => { .then(() => {
return Promise.all([client.invoke('InvokeWithString', message), callbackPromise]); return Promise.all([client.invoke('InvokeWithString', message), callbackPromise]);
}) })

View File

@ -93,9 +93,9 @@ function addLine(line, color) {
document.getElementById('messages').appendChild(child); 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'); let connection = new signalR.HubConnection(`http://${document.location.host}/hubs`, 'formatType=json&format=text');
connection.on('Send', msg => { connection.on('Send', msg => {
@ -112,7 +112,7 @@ connection.onClosed = e => {
} }
click('connect', event => { click('connect', event => {
connection.start(transport) connection.start(transportType)
.then(() => { .then(() => {
isConnected = true; isConnected = true;
addLine('Connected successfully', 'green'); addLine('Connected successfully', 'green');

View File

@ -7,16 +7,16 @@
<body> <body>
<h1>ASP.NET Sockets</h1> <h1>ASP.NET Sockets</h1>
<ul> <ul>
<li><a href="sockets.html?transport=longPolling">Long polling</a></li> <li><a href="sockets.html?transport=LongPolling">Long polling</a></li>
<li><a href="sockets.html?transport=serverSentEvents">Server Sent Events</a></li> <li><a href="sockets.html?transport=ServerSentEvents">Server Sent Events</a></li>
<li><a href="sockets.html?transport=webSockets">Web Sockets</a></li> <li><a href="sockets.html?transport=WebSockets">Web Sockets</a></li>
<li><a href="ws.html">Web Sockets (using only Browser APIs)</a></li> <li><a href="ws.html">Web Sockets (using only Browser APIs)</a></li>
</ul> </ul>
<h1>ASP.NET SignalR (Hubs)</h1> <h1>ASP.NET SignalR (Hubs)</h1>
<ul> <ul>
<li><a href="hubs.html?transport=longPolling">Long polling</a></li> <li><a href="hubs.html?transport=LongPolling">Long polling</a></li>
<li><a href="hubs.html?transport=serverSentEvents">Server Sent Events</a></li> <li><a href="hubs.html?transport=ServerSentEvents">Server Sent Events</a></li>
<li><a href="hubs.html?transport=webSockets">Web Sockets</a></li> <li><a href="hubs.html?transport=WebSockets">Web Sockets</a></li>
</ul> </ul>
</body> </body>
</html> </html>

View File

@ -17,9 +17,9 @@
<script src="utils.js"></script> <script src="utils.js"></script>
<script> <script>
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
let transport = getParameterByName('transport') || 'webSockets'; let transportType = signalR.TransportType[getParameterByName('transport')] || signalR.TransportType.WebSockets;
document.getElementById('transportName').innerHTML = transport; document.getElementById('transportName').innerHTML = signalR.TransportType[transportType];
let url = `http://${document.location.host}/chat` let url = `http://${document.location.host}/chat`
let connection = new signalR.Connection(url); let connection = new signalR.Connection(url);
@ -36,7 +36,7 @@
event.preventDefault(); event.preventDefault();
}); });
connection.start(transport).then(() => { connection.start(transportType).then(() => {
console.log("Opened"); console.log("Opened");
}, () => { }, () => {
console.log("Error opening connection"); console.log("Error opening connection");