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 { 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<void> {
start(transportType: TransportType): Promise<void> {
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<void> {
start(transportType: TransportType): Promise<void> {
return Promise.resolve();
},

View File

@ -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<void> {
async start(transportType: TransportType = TransportType.WebSockets): Promise<void> {
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);
}

View File

@ -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<void> {
return this.connection.start(transportName);
start(transportType?: TransportType): Promise<void> {
return this.connection.start(transportType);
}
stop(): void {

View File

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

View File

@ -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<void>;
send(data: any): Promise<void>;

View File

@ -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));
}

View File

@ -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);
})

View File

@ -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]);
})

View File

@ -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');

View File

@ -7,16 +7,16 @@
<body>
<h1>ASP.NET Sockets</h1>
<ul>
<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=webSockets">Web Sockets</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=WebSockets">Web Sockets</a></li>
<li><a href="ws.html">Web Sockets (using only Browser APIs)</a></li>
</ul>
<h1>ASP.NET SignalR (Hubs)</h1>
<ul>
<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=webSockets">Web Sockets</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=WebSockets">Web Sockets</a></li>
</ul>
</body>
</html>

View File

@ -17,9 +17,9 @@
<script src="utils.js"></script>
<script>
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 connection = new signalR.Connection(url);
@ -36,7 +36,7 @@
event.preventDefault();
});
connection.start(transport).then(() => {
connection.start(transportType).then(() => {
console.log("Opened");
}, () => {
console.log("Error opening connection");