diff --git a/src/ts/@overflow/app/index.tsx b/src/ts/@overflow/app/index.tsx index 4243db4..8644cc9 100644 --- a/src/ts/@overflow/app/index.tsx +++ b/src/ts/@overflow/app/index.tsx @@ -70,8 +70,6 @@ class OFApplication { private config: Config; private container: HTMLElement; - // @Inject() - // private context: AppContext; private rpcClient: WebSocketRPC; private store: Store; private sagaMiddleware: SagaMiddleware; @@ -108,14 +106,14 @@ class OFApplication { private initRpcClient(): Promise { const rpcClient = new Promise((resolve, reject) => { let client = new WebSocketRPC(this.config.rpc.url); - // client.initialize() - // .then(() => { - // resolve(client); - // }) - // .catch((err: any) => { - // reject(err); - // }); - resolve(client); + client.initialize() + .then(() => { + resolve(client); + }) + .catch((err: any) => { + reject(err); + }); + // resolve(client); }); return rpcClient; diff --git a/src/ts/@overflow/commons/websocket/WebSocketRPC.ts b/src/ts/@overflow/commons/websocket/WebSocketRPC.ts index 5ac6c52..7600792 100644 --- a/src/ts/@overflow/commons/websocket/WebSocketRPC.ts +++ b/src/ts/@overflow/commons/websocket/WebSocketRPC.ts @@ -1,15 +1,9 @@ import { - ProtocolNotification, - ProtocolRequest, - ProtocolResponse, + Notification, + Request, + Response, } from './protocol'; -import { - PROTOCOL_NAME as RPCProtocol, - RPCRequest, - RPCResponse, -} from './protocol/rpc'; - export type OnDisconnectFunc = () => void; export type OnResponseFunc = (response: any) => void; @@ -60,9 +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 ProtocolRequest(RPCProtocol, requestID); - let req = new RPCRequest(method, args); - request.setBody(req); + let request = new Request(method, args); + request.ID = requestID; this.conn.send(JSON.stringify(request)); this.requestQueue.set(requestID, {resolve: resolve, reject: reject}); }); @@ -110,11 +103,10 @@ export default class WebSocketRPC { }; this.conn.onmessage = (ev: MessageEvent): any => { - let response = new ProtocolResponse(JSON.parse(ev.data)); - let res = new RPCResponse(response.getBody()); - const requestID = response.getID(); - const error = response.getError(); - const result = res.getResult(); + let response = new Response(JSON.parse(ev.data)); + const requestID = response.ID; + const error = response.Error; + const result = JSON.parse(response.Result); if (this.requestQueue.has(requestID)) { let promise = this.requestQueue.get(requestID); diff --git a/src/ts/@overflow/commons/websocket/protocol/AbstractProtocol.ts b/src/ts/@overflow/commons/websocket/protocol/AbstractProtocol.ts new file mode 100644 index 0000000..3037984 --- /dev/null +++ b/src/ts/@overflow/commons/websocket/protocol/AbstractProtocol.ts @@ -0,0 +1,32 @@ +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/Notification.ts b/src/ts/@overflow/commons/websocket/protocol/Notification.ts new file mode 100644 index 0000000..f8f7fed --- /dev/null +++ b/src/ts/@overflow/commons/websocket/protocol/Notification.ts @@ -0,0 +1,7 @@ +import Request from './Request'; + +export class Notification extends Request { + public constructor(method: string, params: any) { + super(method, params); + } +} diff --git a/src/ts/@overflow/commons/websocket/protocol/ProtocolError.ts b/src/ts/@overflow/commons/websocket/protocol/ProtocolError.ts index 69568c1..978d709 100644 --- a/src/ts/@overflow/commons/websocket/protocol/ProtocolError.ts +++ b/src/ts/@overflow/commons/websocket/protocol/ProtocolError.ts @@ -44,3 +44,5 @@ export class ProtocolError { return JSON.parse(json); } } + +export default ProtocolError; diff --git a/src/ts/@overflow/commons/websocket/protocol/ProtocolHeader.ts b/src/ts/@overflow/commons/websocket/protocol/ProtocolHeader.ts deleted file mode 100644 index 1a4f7d8..0000000 --- a/src/ts/@overflow/commons/websocket/protocol/ProtocolHeader.ts +++ /dev/null @@ -1,39 +0,0 @@ -export class ProtocolHeader { - protected readonly protocol: string; - protected readonly id: ID; - protected body: any; - - public constructor(protocol: string, id: ID, body?: any) { - this.protocol = protocol; - this.id = id; - this.body = body; - } - - /** - * getProtocol - */ - public getProtocol(): string { - return this.protocol; - } - - /** - * getID - */ - public getID(): ID { - return this.id; - } - - /** - * setID - */ - public setBody(body: any): void { - this.body = body; - } - /** - * getID - */ - public getBody(): any { - return this.body; - } - -} diff --git a/src/ts/@overflow/commons/websocket/protocol/ProtocolNotification.ts b/src/ts/@overflow/commons/websocket/protocol/ProtocolNotification.ts deleted file mode 100644 index 57e5787..0000000 --- a/src/ts/@overflow/commons/websocket/protocol/ProtocolNotification.ts +++ /dev/null @@ -1,29 +0,0 @@ -export class ProtocolNotification { - private readonly protocol: string; - private body: string; - - public constructor(protocol: string) { - this.protocol = protocol; - } - - /** - * getProtocol - */ - public getProtocol(): string { - return this.protocol; - } - - /** - * setBody - */ - public setBody(body: string): void { - this.body = body; - } - /** - * getBody - */ - public getBody(): string { - return this.body; - } - -} diff --git a/src/ts/@overflow/commons/websocket/protocol/ProtocolRequest.ts b/src/ts/@overflow/commons/websocket/protocol/ProtocolRequest.ts deleted file mode 100644 index 1339d21..0000000 --- a/src/ts/@overflow/commons/websocket/protocol/ProtocolRequest.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { ProtocolHeader } from './ProtocolHeader'; - -export class ProtocolRequest extends ProtocolHeader { - public constructor(protocol: string, id: ID) { - super(protocol, id); - } -} diff --git a/src/ts/@overflow/commons/websocket/protocol/ProtocolResponse.ts b/src/ts/@overflow/commons/websocket/protocol/ProtocolResponse.ts deleted file mode 100644 index 1a6253f..0000000 --- a/src/ts/@overflow/commons/websocket/protocol/ProtocolResponse.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { ProtocolHeader } from './ProtocolHeader'; -import { ProtocolError } from './ProtocolError'; - -export class ProtocolResponse extends ProtocolHeader { - protected error: ProtocolError; - - public constructor(response: ProtocolResponse) { - super(response.protocol, response.id, response.body); - this.error = response.error; - } - - /** - * setError - */ - public setError(error: ProtocolError): void { - this.error = error; - } - /** - * getError - */ - public getError(): ProtocolError { - return this.error; - } -} diff --git a/src/ts/@overflow/commons/websocket/protocol/ProtocolSub.ts b/src/ts/@overflow/commons/websocket/protocol/ProtocolSub.ts deleted file mode 100644 index 7d6d925..0000000 --- a/src/ts/@overflow/commons/websocket/protocol/ProtocolSub.ts +++ /dev/null @@ -1,2 +0,0 @@ -export abstract class ProtocolSub { -} diff --git a/src/ts/@overflow/commons/websocket/protocol/rpc/RPCRequest.ts b/src/ts/@overflow/commons/websocket/protocol/Request.ts similarity index 73% rename from src/ts/@overflow/commons/websocket/protocol/rpc/RPCRequest.ts rename to src/ts/@overflow/commons/websocket/protocol/Request.ts index 08e01c6..f0b5613 100644 --- a/src/ts/@overflow/commons/websocket/protocol/rpc/RPCRequest.ts +++ b/src/ts/@overflow/commons/websocket/protocol/Request.ts @@ -1,6 +1,6 @@ -import { ProtocolSub } from '../ProtocolSub'; +import AbstractProtocol from './AbstractProtocol'; -export class RPCRequest extends ProtocolSub { +export class Request extends AbstractProtocol { private readonly method: string; private readonly params: any; @@ -23,4 +23,7 @@ export class RPCRequest extends ProtocolSub { public getParams(): any { return this.params; } + } + +export default Request; diff --git a/src/ts/@overflow/commons/websocket/protocol/Response.ts b/src/ts/@overflow/commons/websocket/protocol/Response.ts new file mode 100644 index 0000000..9a2c233 --- /dev/null +++ b/src/ts/@overflow/commons/websocket/protocol/Response.ts @@ -0,0 +1,32 @@ +import AbstractProtocol from './AbstractProtocol'; +import ProtocolError from './ProtocolError'; + +export class Response extends AbstractProtocol { + 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}]`); + } + this.result = response.result; + this.error = response.error; + } + + /** + * getError + */ + public get Result(): any { + return this.result; + } + + /** + * getError + */ + public get Error(): ProtocolError { + return this.error; + } +} + +export default Response; diff --git a/src/ts/@overflow/commons/websocket/protocol/index.ts b/src/ts/@overflow/commons/websocket/protocol/index.ts index afdd54d..cd1af29 100644 --- a/src/ts/@overflow/commons/websocket/protocol/index.ts +++ b/src/ts/@overflow/commons/websocket/protocol/index.ts @@ -1,6 +1,5 @@ +export * from './AbstractProtocol'; export * from './ProtocolError'; -export * from './ProtocolHeader'; -export * from './ProtocolNotification'; -export * from './ProtocolRequest'; -export * from './ProtocolResponse'; -export * from './ProtocolSub'; +export * from './Notification'; +export * from './Request'; +export * from './Response'; diff --git a/src/ts/@overflow/commons/websocket/protocol/rpc/RPCResponse.ts b/src/ts/@overflow/commons/websocket/protocol/rpc/RPCResponse.ts deleted file mode 100644 index 4b1179b..0000000 --- a/src/ts/@overflow/commons/websocket/protocol/rpc/RPCResponse.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { ProtocolSub } from '../ProtocolSub'; - -export class RPCResponse extends ProtocolSub { - private readonly result: any; - - public constructor(result: any) { - super(); - this.result = result; - } - - /** - * getResult - */ - public getResult(): any { - return this.result; - } -} diff --git a/src/ts/@overflow/commons/websocket/protocol/rpc/index.ts b/src/ts/@overflow/commons/websocket/protocol/rpc/index.ts deleted file mode 100644 index f51d676..0000000 --- a/src/ts/@overflow/commons/websocket/protocol/rpc/index.ts +++ /dev/null @@ -1,5 +0,0 @@ -export * from './RPCRequest'; -export * from './RPCResponse'; - -export const PROTOCOL_NAME = 'jsonrpc'; -