Protocol has been changed.

This commit is contained in:
crusader 2017-08-14 11:09:05 +09:00
parent e9d9610841
commit 62617e5990
7 changed files with 124 additions and 75 deletions

View File

@ -54,8 +54,8 @@ export default class WebSocketRPC {
public Call(method: string, args: any): Promise<any> {
return new Promise<any>((resolve, reject) => {
const requestID = this.getRequestID();
let request = new Request<ID_TYPE>(method, args);
request.ID = requestID;
let request = new Request<ID_TYPE>(requestID, method, args);
this.conn.send(JSON.stringify(request));
this.requestQueue.set(requestID, {resolve: resolve, reject: reject});
});
@ -103,7 +103,30 @@ export default class WebSocketRPC {
};
this.conn.onmessage = (ev: MessageEvent): any => {
let response = new Response<ID_TYPE>(JSON.parse(ev.data));
const json = JSON.parse(ev.data);
try {
let response: Response<ID_TYPE> = Response.convert(json);
this.onResponseHandler(response);
} catch (e) {
// Check message is Notification
try {
let notification = Notification.convert(json);
this.onNotificationHandler(notification);
} catch (e) {
console.log(e);
}
}
this.fireOnMessage(ev);
};
this.conn.onerror = (ev: Event): any => {
console.log(ev);
return null;
};
}
private onResponseHandler(response: Response<ID_TYPE>): void {
const requestID = response.ID;
const error = response.Error;
const result = JSON.parse(response.Result);
@ -123,15 +146,11 @@ export default class WebSocketRPC {
// perhaps sever side send message
}
this.fireOnMessage(ev);
};
this.conn.onerror = (ev: Event): any => {
console.log(ev);
return null;
};
}
private onNotificationHandler(notification: Notification): void {
//
}
private fireOnDisconnect(): void {
for (let i = 0; i < this.onDisconnectListeners.length; i++) {

View File

@ -1,32 +0,0 @@
export abstract class AbstractProtocol<ID> {
protected readonly protocol: string = '1.0';
protected id: ID;
public constructor(id?: ID) {
this.id = id;
}
/**
* getProtocol
*/
public get Protocol(): string {
return this.protocol;
}
/**
* getID
*/
public get ID(): ID {
return this.id;
}
/**
* setID
*/
public set ID(id: ID) {
this.id = id;
}
}
export default AbstractProtocol;

View File

@ -0,0 +1,20 @@
export abstract class Header {
protected protocol: string;
/**
* get Protocol
*/
public get Protocol(): string {
return this.protocol;
}
/**
* set Protocol
*/
public set Protocol(protocol: string) {
this.protocol = protocol;
}
}
export default Header;

View File

@ -1,7 +1,39 @@
import Request from './Request';
import Header from './Header';
export class Notification extends Request<any> {
public constructor(method: string, params: any) {
super(method, params);
export class Notification extends Header {
private readonly method: string;
private readonly params: any;
public constructor(method: string, params?: any) {
super();
this.method = method;
this.params = params;
}
/**
* get Method
*/
public get Method(): string {
return this.method;
}
/**
* get Params
*/
public get Params(): any {
return this.params;
}
public static convert(notification: Notification): Notification {
const method = notification.method;
if (undefined === method) {
throw new Error(`Notification must include method`);
}
const params = notification.params;
return new Notification(method, params);
}
}
export default Notification;

View File

@ -1,27 +1,18 @@
import AbstractProtocol from './AbstractProtocol';
import Notification from './Notification';
export class Request<ID> extends AbstractProtocol<ID> {
private readonly method: string;
private readonly params: any;
export class Request<ID> extends Notification {
protected id: ID;
public constructor(method: string, params: any) {
super();
this.method = method;
this.params = params;
public constructor(id: ID, method: string, params?: any) {
super(method, params);
this.id = id;
}
/**
* getMethod
* getID
*/
public getMethod(): string {
return this.method;
}
/**
* getParams
*/
public getParams(): any {
return this.params;
public get ID(): ID {
return this.id;
}
}

View File

@ -1,32 +1,51 @@
import AbstractProtocol from './AbstractProtocol';
import Header from './Header';
import ProtocolError from './ProtocolError';
export class Response<ID> extends AbstractProtocol<ID> {
export class Response<ID> extends Header {
protected id: ID;
private result: any;
protected error: ProtocolError;
public constructor(response: Response<ID>) {
super(response.id);
if (response.protocol !== this.Protocol) {
throw new Error(`Protocol is not match[${response.protocol}]`);
public constructor(id: ID) {
super();
if (undefined === id) {
throw new Error(`Response must include ID`);
}
this.result = response.result;
this.error = response.error;
this.id = id;
}
/**
* getError
* get ID
*/
public get ID(): ID {
return this.result;
}
/**
* get Result
*/
public get Result(): any {
return this.result;
}
/**
* getError
* get Error
*/
public get Error(): ProtocolError {
return this.error;
}
public static convert<T>(response: Response<T>): Response<T> {
const id = response.id;
if (undefined === id) {
throw new Error(`Response must include ID`);
}
let res = new Response(id);
res.result = response.result;
res.error = response.error;
return res;
}
}
export default Response;

View File

@ -1,4 +1,4 @@
export * from './AbstractProtocol';
export * from './Header';
export * from './ProtocolError';
export * from './Notification';
export * from './Request';