Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

Commit

Permalink
Introducing the IConnection interface
Browse files Browse the repository at this point in the history
Unifying events
  • Loading branch information
moozzyk authored and moozzyk committed Mar 23, 2017
1 parent 14d3f2b commit 70e4ec9
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 24 deletions.
19 changes: 8 additions & 11 deletions client-ts/Microsoft.AspNetCore.SignalR.Client.TS/Connection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DataReceived, ConnectionClosed } from "./Common"
import { IConnection } from "./IConnection"
import { ITransport, WebSocketTransport, ServerSentEventsTransport, LongPollingTransport } from "./Transports"
import { IHttpClient, HttpClient } from "./HttpClient"
import { ISignalROptions } from "./ISignalROptions"
Expand All @@ -9,15 +10,13 @@ enum ConnectionState {
Connected
}

export class Connection {
export class Connection implements IConnection {
private connectionState: ConnectionState;
private url: string;
private queryString: string;
private connectionId: string;
private httpClient: IHttpClient;
private transport: ITransport;
private dataReceivedCallback: DataReceived = (data: any) => { };
private connectionClosedCallback: ConnectionClosed = (error?: any) => { };

constructor(url: string, queryString: string = "", options: ISignalROptions = {}) {
this.url = url;
Expand All @@ -32,7 +31,7 @@ export class Connection {
}

this.transport = this.createTransport(transportName);
this.transport.onDataReceived = this.dataReceivedCallback;
this.transport.onDataReceived = this.onDataReceived;
this.transport.onClosed = e => this.stopConnection(e);

try {
Expand Down Expand Up @@ -82,14 +81,12 @@ export class Connection {
this.transport.stop();
this.transport = null;
this.connectionState = ConnectionState.Disconnected;
this.connectionClosedCallback(error);
}

set dataReceived(callback: DataReceived) {
this.dataReceivedCallback = callback;
if (this.onClosed) {
this.onClosed(error);
}
}

set connectionClosed(callback: ConnectionClosed) {
this.connectionClosedCallback = callback;
}
onDataReceived: DataReceived;
onClosed: ConnectionClosed;
}
22 changes: 14 additions & 8 deletions client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ConnectionClosed } from "./Common"
import { IConnection } from "./IConnection"
import { Connection } from "./Connection"

interface InvocationDescriptor {
Expand All @@ -16,24 +17,29 @@ interface InvocationResultDescriptor {
export { Connection } from "./Connection"

export class HubConnection {
private connection: Connection;
private connection: IConnection;
private callbacks: Map<string, (invocationDescriptor: InvocationResultDescriptor) => void>;
private methods: Map<string, (...args: any[]) => void>;
private id: number;

constructor(url: string, queryString?: string) {
this.connection = new Connection(url, queryString);
static create(url: string, queryString?: string): HubConnection {
return new this(new Connection(url, queryString))
}

this.connection.dataReceived = data => {
this.dataReceived(data);
constructor(connection: IConnection);
constructor(url: string, queryString?: string);
constructor(connectionOrUrl: IConnection | string, queryString?: string) {
this.connection = typeof connectionOrUrl === "string" ? new Connection(connectionOrUrl, queryString) : connectionOrUrl;
this.connection.onDataReceived = data => {
this.onDataReceived(data);
};

this.callbacks = new Map<string, (invocationDescriptor: InvocationResultDescriptor) => void>();
this.methods = new Map<string, (...args: any[]) => void>();
this.id = 0;
}

private dataReceived(data: any) {
private onDataReceived(data: any) {
// TODO: separate JSON parsing
// Can happen if a poll request was cancelled
if (!data) {
Expand Down Expand Up @@ -101,7 +107,7 @@ export class HubConnection {
this.methods[methodName] = method;
}

set connectionClosed(callback: ConnectionClosed) {
this.connection.connectionClosed = callback;
set onClosed(callback: ConnectionClosed) {
this.connection.onClosed = callback;
}
}
10 changes: 10 additions & 0 deletions client-ts/Microsoft.AspNetCore.SignalR.Client.TS/IConnection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { DataReceived, ConnectionClosed } from "./Common"

export interface IConnection {
start(transportName: string): Promise<void>;
send(data: any): Promise<void>;
stop(): void;

onDataReceived: DataReceived;
onClosed: ConnectionClosed;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ describe('connection', () => {
let connection = new signalR.Connection(ECHOENDPOINT_URL);

let received = "";
connection.dataReceived = data => {
connection.onDataReceived = data => {
received += data;
if (data == message) {
connection.stop();
}
}

connection.connectionClosed = error => {
connection.onClosed = error => {
expect(error).toBeUndefined();
done();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ describe('hubConnection', () => {
it(`over ${transportName} 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 => {
expect(error).toBe(undefined);
done();
}

hubConnection.start(transportName)
.then(() => {
Expand All @@ -17,7 +21,6 @@ describe('hubConnection', () => {
})
.then(() => {
hubConnection.stop();
done();
})
})
.catch(() => {
Expand Down
2 changes: 1 addition & 1 deletion samples/SocketsSample/wwwroot/hubs.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ <h4>Private Message</h4>
addLine(msg);
});

connection.connectionClosed = e => {
connection.onClosed = e => {
if (e) {
addLine('Connection closed with error: ' + e, 'red');
}
Expand Down
2 changes: 1 addition & 1 deletion samples/SocketsSample/wwwroot/sockets.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ <h1 id="transportName">Unknown Transport</h1>
let url = `http://${document.location.host}/chat`
let connection = new signalR.Connection(url);

connection.dataReceived = data => {
connection.onDataReceived = data => {
let child = document.createElement('li');
child.innerText = data;
document.getElementById('messages').appendChild(child);
Expand Down

0 comments on commit 70e4ec9

Please sign in to comment.