diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts index 749b5dfadc..fa1a56ebf8 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts @@ -3,36 +3,10 @@ import { IConnection } from "./IConnection" import { Connection } from "./Connection" import { TransportType } from "./Transports" import { Subject, Observable } from "./Observable" -import * as Formatters from "./Formatters"; - -const enum MessageType { - Invocation = 1, - Result, - Completion -} - -interface HubMessage { - readonly type: MessageType; - readonly invocationId: string; -} - -interface InvocationMessage extends HubMessage { - readonly target: string; - readonly arguments: Array; - readonly nonblocking?: boolean; -} - -interface ResultMessage extends HubMessage { - readonly item?: any; -} - -interface CompletionMessage extends HubMessage { - readonly error?: string; - readonly result?: any; -} - export { Connection } from "./Connection" export { TransportType } from "./Transports" +import { IHubProtocol, MessageType, HubMessage, CompletionMessage, ResultMessage, InvocationMessage } from "./IHubProtocol"; +import { JsonHubProtocol } from "./JsonHubProtocol"; export class HubConnection { private connection: IConnection; @@ -40,6 +14,7 @@ export class HubConnection { private methods: Map void>; private id: number; private connectionClosedCallback: ConnectionClosed; + private protocol: IHubProtocol; static create(url: string, queryString?: string): HubConnection { return new this(new Connection(url, queryString)) @@ -59,22 +34,16 @@ export class HubConnection { this.callbacks = new Map void>(); this.methods = new Map void>(); this.id = 0; + this.protocol = new JsonHubProtocol(); } private onDataReceived(data: any) { - // TODO: separate JSON parsing - // Can happen if a poll request was cancelled - if (!data) { - return; - } - // Parse the messages - let messages = Formatters.TextMessageFormat.parse(data); + let messages = this.protocol.parseMessages(data); for (var i = 0; i < messages.length; ++i) { - console.log(`Received message: ${messages[i]}`); + var message = messages[i]; - var message = JSON.parse(messages[i]); switch (message.type) { case MessageType.Invocation: this.InvokeClientMethod(message); @@ -159,8 +128,7 @@ export class HubConnection { } }); - // TODO: separate conversion to enable different data formats - let message = this.framePayload(invocationDescriptor); + let message = this.protocol.writeMessage(invocationDescriptor); this.connection.send(message) .catch(e => { @@ -190,8 +158,7 @@ export class HubConnection { } }); - // TODO: separate conversion to enable different data formats - let message = this.framePayload(invocationDescriptor); + let message = this.protocol.writeMessage(invocationDescriptor); this.connection.send(message) .catch(e => { @@ -211,11 +178,6 @@ export class HubConnection { this.connectionClosedCallback = callback; } - private framePayload(invocationDescriptor: InvocationMessage): string { - let data = JSON.stringify(invocationDescriptor); - return Formatters.TextMessageFormat.write(data); - } - private createInvocation(methodName: string, args: any[]): InvocationMessage { let id = this.id; this.id++; diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/IHubProtocol.ts b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/IHubProtocol.ts new file mode 100644 index 0000000000..d60ab86286 --- /dev/null +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/IHubProtocol.ts @@ -0,0 +1,30 @@ +export const enum MessageType { + Invocation = 1, + Result, + Completion +} + +export interface HubMessage { + readonly type: MessageType; + readonly invocationId: string; +} + +export interface InvocationMessage extends HubMessage { + readonly target: string; + readonly arguments: Array; + readonly nonblocking?: boolean; +} + +export interface ResultMessage extends HubMessage { + readonly item?: any; +} + +export interface CompletionMessage extends HubMessage { + readonly error?: string; + readonly result?: any; +} + +export interface IHubProtocol { + parseMessages(input: string): HubMessage[]; + writeMessage(message: HubMessage): string; +} \ No newline at end of file diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/JsonHubProtocol.ts b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/JsonHubProtocol.ts new file mode 100644 index 0000000000..a9af39c208 --- /dev/null +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/JsonHubProtocol.ts @@ -0,0 +1,23 @@ +import { TextMessageFormat } from "./Formatters"; +import { IHubProtocol, HubMessage } from "./IHubProtocol"; + +export class JsonHubProtocol implements IHubProtocol { + parseMessages(input: string): HubMessage[] { + if (!input) { + return []; + } + + // Parse the messages + let messages = TextMessageFormat.parse(input); + let hubMessages = []; + for (var i = 0; i < messages.length; ++i) { + hubMessages.push(JSON.parse(messages[i])); + } + + return hubMessages; + } + + writeMessage(message: HubMessage): string { + return TextMessageFormat.write(JSON.stringify(message)); + } +} \ No newline at end of file diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/Microsoft.AspNetCore.SignalR.Client.TS.csproj b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/Microsoft.AspNetCore.SignalR.Client.TS.csproj index 0b30b88f2d..bf32b28e1a 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/Microsoft.AspNetCore.SignalR.Client.TS.csproj +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/Microsoft.AspNetCore.SignalR.Client.TS.csproj @@ -13,7 +13,6 @@ -