Adding negotiation to TS client
This commit is contained in:
parent
f8d91b54d4
commit
59e032dbcb
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue