Adding negotiation to TS client

This commit is contained in:
Pawel Kadluczka 2017-06-27 15:52:12 -07:00
parent f8d91b54d4
commit 59e032dbcb
4 changed files with 32 additions and 6 deletions

View File

@ -8,6 +8,19 @@ import { TextMessageFormat } from "../Microsoft.AspNetCore.SignalR.Client.TS/For
import { asyncit as it, captureException } from './JasmineUtils'; import { asyncit as it, captureException } from './JasmineUtils';
describe("HubConnection", () => { 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", () => { describe("invoke", () => {
it("sends an invocation", async () => { it("sends an invocation", async () => {
let connection = new TestConnection(); let connection = new TestConnection();

View File

@ -4,8 +4,9 @@ import { TransportType } from "./Transports"
import { Subject, Observable } from "./Observable" import { Subject, Observable } from "./Observable"
export { TransportType } from "./Transports" export { TransportType } from "./Transports"
export { HttpConnection } from "./HttpConnection" 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 { JsonHubProtocol } from "./JsonHubProtocol";
import { TextMessageFormat } from "./Formatters"
export class HubConnection { export class HubConnection {
private connection: IConnection; private connection: IConnection;
@ -39,7 +40,7 @@ export class HubConnection {
switch (message.type) { switch (message.type) {
case MessageType.Invocation: case MessageType.Invocation:
this.InvokeClientMethod(<InvocationMessage>message); this.invokeClientMethod(<InvocationMessage>message);
break; break;
case MessageType.Result: case MessageType.Result:
case MessageType.Completion: 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); let method = this.methods.get(invocationMessage.target);
if (method) { if (method) {
method.apply(this, invocationMessage.arguments); method.apply(this, invocationMessage.arguments);
@ -89,8 +90,11 @@ export class HubConnection {
} }
} }
start(): Promise<void> { async start(): Promise<void> {
return this.connection.start(); await this.connection.start();
await this.connection.send(
TextMessageFormat.write(
JSON.stringify(<NegotiationMessage>{ protocol: this.protocol.name()})));
} }
stop(): void { stop(): void {

View File

@ -24,7 +24,12 @@ export interface CompletionMessage extends HubMessage {
readonly result?: any; readonly result?: any;
} }
export interface NegotiationMessage {
readonly protocol: string;
}
export interface IHubProtocol { export interface IHubProtocol {
name(): string;
parseMessages(input: string): HubMessage[]; parseMessages(input: string): HubMessage[];
writeMessage(message: HubMessage): string; writeMessage(message: HubMessage): string;
} }

View File

@ -2,6 +2,10 @@
import { IHubProtocol, HubMessage } from "./IHubProtocol"; import { IHubProtocol, HubMessage } from "./IHubProtocol";
export class JsonHubProtocol implements IHubProtocol { export class JsonHubProtocol implements IHubProtocol {
name(): string {
return "json"
}
parseMessages(input: string): HubMessage[] { parseMessages(input: string): HubMessage[] {
if (!input) { if (!input) {
return []; return [];
@ -18,6 +22,6 @@ export class JsonHubProtocol implements IHubProtocol {
} }
writeMessage(message: HubMessage): string { writeMessage(message: HubMessage): string {
return TextMessageFormat.write(JSON.stringify(message)); return TextMessageFormat.write(JSON.stringify(message));
} }
} }