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';
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();

View File

@ -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(<InvocationMessage>message);
this.invokeClientMethod(<InvocationMessage>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<void> {
return this.connection.start();
async start(): Promise<void> {
await this.connection.start();
await this.connection.send(
TextMessageFormat.write(
JSON.stringify(<NegotiationMessage>{ protocol: this.protocol.name()})));
}
stop(): void {

View File

@ -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;
}

View File

@ -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));
}
}