diff --git a/src/ts/@overflow/commons/websocket/WebSocketRPC.ts b/src/ts/@overflow/commons/websocket/WebSocketRPC.ts index 941598d..4cdacb8 100644 --- a/src/ts/@overflow/commons/websocket/WebSocketRPC.ts +++ b/src/ts/@overflow/commons/websocket/WebSocketRPC.ts @@ -54,8 +54,8 @@ export default class WebSocketRPC { public Call(method: string, args: any): Promise { return new Promise((resolve, reject) => { const requestID = this.getRequestID(); - let request = new Request(method, args); - request.ID = requestID; + let request = new Request(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(JSON.parse(ev.data)); + const json = JSON.parse(ev.data); + try { + let response: Response = 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): 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++) { diff --git a/src/ts/@overflow/commons/websocket/protocol/AbstractProtocol.ts b/src/ts/@overflow/commons/websocket/protocol/AbstractProtocol.ts deleted file mode 100644 index 3037984..0000000 --- a/src/ts/@overflow/commons/websocket/protocol/AbstractProtocol.ts +++ /dev/null @@ -1,32 +0,0 @@ -export abstract class AbstractProtocol { - 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; diff --git a/src/ts/@overflow/commons/websocket/protocol/Header.ts b/src/ts/@overflow/commons/websocket/protocol/Header.ts new file mode 100644 index 0000000..291b90c --- /dev/null +++ b/src/ts/@overflow/commons/websocket/protocol/Header.ts @@ -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; diff --git a/src/ts/@overflow/commons/websocket/protocol/Notification.ts b/src/ts/@overflow/commons/websocket/protocol/Notification.ts index f8f7fed..f9c1a7b 100644 --- a/src/ts/@overflow/commons/websocket/protocol/Notification.ts +++ b/src/ts/@overflow/commons/websocket/protocol/Notification.ts @@ -1,7 +1,39 @@ -import Request from './Request'; +import Header from './Header'; -export class Notification extends Request { - 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; diff --git a/src/ts/@overflow/commons/websocket/protocol/Request.ts b/src/ts/@overflow/commons/websocket/protocol/Request.ts index f0b5613..23def5d 100644 --- a/src/ts/@overflow/commons/websocket/protocol/Request.ts +++ b/src/ts/@overflow/commons/websocket/protocol/Request.ts @@ -1,27 +1,18 @@ -import AbstractProtocol from './AbstractProtocol'; +import Notification from './Notification'; -export class Request extends AbstractProtocol { - private readonly method: string; - private readonly params: any; +export class Request 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; } } diff --git a/src/ts/@overflow/commons/websocket/protocol/Response.ts b/src/ts/@overflow/commons/websocket/protocol/Response.ts index 9a2c233..67f9d2e 100644 --- a/src/ts/@overflow/commons/websocket/protocol/Response.ts +++ b/src/ts/@overflow/commons/websocket/protocol/Response.ts @@ -1,32 +1,51 @@ -import AbstractProtocol from './AbstractProtocol'; +import Header from './Header'; import ProtocolError from './ProtocolError'; -export class Response extends AbstractProtocol { +export class Response extends Header { + protected id: ID; private result: any; protected error: ProtocolError; - public constructor(response: Response) { - 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(response: Response): Response { + 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; diff --git a/src/ts/@overflow/commons/websocket/protocol/index.ts b/src/ts/@overflow/commons/websocket/protocol/index.ts index cd1af29..0a91beb 100644 --- a/src/ts/@overflow/commons/websocket/protocol/index.ts +++ b/src/ts/@overflow/commons/websocket/protocol/index.ts @@ -1,4 +1,4 @@ -export * from './AbstractProtocol'; +export * from './Header'; export * from './ProtocolError'; export * from './Notification'; export * from './Request';