From 59e032dbcb3b41c3ee9b04dccbf0f70e55328e37 Mon Sep 17 00:00:00 2001 From: Pawel Kadluczka Date: Tue, 27 Jun 2017 15:52:12 -0700 Subject: [PATCH] Adding negotiation to TS client --- .../HubConnection.spec.ts | 13 +++++++++++++ .../HubConnection.ts | 14 +++++++++----- .../IHubProtocol.ts | 5 +++++ .../JsonHubProtocol.ts | 6 +++++- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS.Tests/HubConnection.spec.ts b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS.Tests/HubConnection.spec.ts index bb4c1983e5..9cf4e54c6e 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS.Tests/HubConnection.spec.ts +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS.Tests/HubConnection.spec.ts @@ -8,6 +8,19 @@ import { TextMessageFormat } from "../Microsoft.AspNetCore.SignalR.Client.TS/For import { asyncit as it, captureException } from './JasmineUtils'; describe("HubConnection", () => { + describe("start", () => { + it("sends negotiation message", async () => { + let connection = new TestConnection(); + let hubConnection = new HubConnection(connection); + await hubConnection.start(); + expect(connection.sentData.length).toBe(1) + expect(JSON.parse(connection.sentData[0])).toEqual({ + protocol: "json" + }); + await hubConnection.stop(); + }); + }); + describe("invoke", () => { it("sends an invocation", async () => { let connection = new TestConnection(); diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts index de1bcf0eb9..19178aba34 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts @@ -4,8 +4,9 @@ import { TransportType } from "./Transports" import { Subject, Observable } from "./Observable" export { TransportType } from "./Transports" export { HttpConnection } from "./HttpConnection" -import { IHubProtocol, MessageType, HubMessage, CompletionMessage, ResultMessage, InvocationMessage } from "./IHubProtocol"; +import { IHubProtocol, MessageType, HubMessage, CompletionMessage, ResultMessage, InvocationMessage, NegotiationMessage } from "./IHubProtocol"; import { JsonHubProtocol } from "./JsonHubProtocol"; +import { TextMessageFormat } from "./Formatters" export class HubConnection { private connection: IConnection; @@ -39,7 +40,7 @@ export class HubConnection { switch (message.type) { case MessageType.Invocation: - this.InvokeClientMethod(message); + this.invokeClientMethod(message); break; case MessageType.Result: case MessageType.Completion: @@ -59,7 +60,7 @@ export class HubConnection { } } - private InvokeClientMethod(invocationMessage: InvocationMessage) { + private invokeClientMethod(invocationMessage: InvocationMessage) { let method = this.methods.get(invocationMessage.target); if (method) { method.apply(this, invocationMessage.arguments); @@ -89,8 +90,11 @@ export class HubConnection { } } - start(): Promise { - return this.connection.start(); + async start(): Promise { + await this.connection.start(); + await this.connection.send( + TextMessageFormat.write( + JSON.stringify({ protocol: this.protocol.name()}))); } stop(): void { diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/IHubProtocol.ts b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/IHubProtocol.ts index d60ab86286..dfe03ffe11 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/IHubProtocol.ts +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/IHubProtocol.ts @@ -24,7 +24,12 @@ export interface CompletionMessage extends HubMessage { readonly result?: any; } +export interface NegotiationMessage { + readonly protocol: string; +} + export interface IHubProtocol { + name(): string; 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 index a9af39c208..e48c9fc2f2 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/JsonHubProtocol.ts +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/JsonHubProtocol.ts @@ -2,6 +2,10 @@ import { IHubProtocol, HubMessage } from "./IHubProtocol"; export class JsonHubProtocol implements IHubProtocol { + name(): string { + return "json" + } + parseMessages(input: string): HubMessage[] { if (!input) { return []; @@ -18,6 +22,6 @@ export class JsonHubProtocol implements IHubProtocol { } writeMessage(message: HubMessage): string { - return TextMessageFormat.write(JSON.stringify(message)); + return TextMessageFormat.write(JSON.stringify(message)); } } \ No newline at end of file