parent
14d3f2bc9b
commit
70e4ec9a06
|
|
@ -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"
|
||||
|
|
@ -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;
|
||||
|
|
@ -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 {
|
||||
|
|
@ -82,14 +81,12 @@ export class Connection {
|
|||
this.transport.stop();
|
||||
this.transport = null;
|
||||
this.connectionState = ConnectionState.Disconnected;
|
||||
this.connectionClosedCallback(error);
|
||||
|
||||
if (this.onClosed) {
|
||||
this.onClosed(error);
|
||||
}
|
||||
}
|
||||
|
||||
set dataReceived(callback: DataReceived) {
|
||||
this.dataReceivedCallback = callback;
|
||||
}
|
||||
|
||||
set connectionClosed(callback: ConnectionClosed) {
|
||||
this.connectionClosedCallback = callback;
|
||||
}
|
||||
onDataReceived: DataReceived;
|
||||
onClosed: ConnectionClosed;
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
import { ConnectionClosed } from "./Common"
|
||||
import { IConnection } from "./IConnection"
|
||||
import { Connection } from "./Connection"
|
||||
|
||||
interface InvocationDescriptor {
|
||||
|
|
@ -16,16 +17,21 @@ 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>();
|
||||
|
|
@ -33,7 +39,7 @@ export class HubConnection {
|
|||
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) {
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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(() => {
|
||||
|
|
@ -17,7 +21,6 @@ describe('hubConnection', () => {
|
|||
})
|
||||
.then(() => {
|
||||
hubConnection.stop();
|
||||
done();
|
||||
})
|
||||
})
|
||||
.catch(() => {
|
||||
|
|
|
|||
|
|
@ -102,7 +102,7 @@ connection.on('Send', msg => {
|
|||
addLine(msg);
|
||||
});
|
||||
|
||||
connection.connectionClosed = e => {
|
||||
connection.onClosed = e => {
|
||||
if (e) {
|
||||
addLine('Connection closed with error: ' + e, 'red');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@
|
|||
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);
|
||||
|
|
|
|||
Loading…
Reference in New Issue