This commit is contained in:
crusader 2017-08-09 17:52:57 +09:00
parent baf6c129a1
commit e8ae690011
15 changed files with 99 additions and 157 deletions

View File

@ -70,8 +70,6 @@ class OFApplication {
private config: Config;
private container: HTMLElement;
// @Inject()
// private context: AppContext;
private rpcClient: WebSocketRPC;
private store: Store<any>;
private sagaMiddleware: SagaMiddleware<any>;
@ -108,14 +106,14 @@ class OFApplication {
private initRpcClient(): Promise<WebSocketRPC> {
const rpcClient = new Promise<WebSocketRPC>((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;

View File

@ -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<any> {
return new Promise<any>((resolve, reject) => {
const requestID = this.getRequestID();
let request = new ProtocolRequest<ID_TYPE>(RPCProtocol, requestID);
let req = new RPCRequest(method, args);
request.setBody(req);
let request = new Request<ID_TYPE>(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<ID_TYPE>(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<ID_TYPE>(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);

View File

@ -0,0 +1,32 @@
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,7 @@
import Request from './Request';
export class Notification extends Request<any> {
public constructor(method: string, params: any) {
super(method, params);
}
}

View File

@ -44,3 +44,5 @@ export class ProtocolError {
return JSON.parse(json);
}
}
export default ProtocolError;

View File

@ -1,39 +0,0 @@
export class ProtocolHeader<ID> {
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;
}
}

View File

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

View File

@ -1,7 +0,0 @@
import { ProtocolHeader } from './ProtocolHeader';
export class ProtocolRequest<ID> extends ProtocolHeader<ID> {
public constructor(protocol: string, id: ID) {
super(protocol, id);
}
}

View File

@ -1,24 +0,0 @@
import { ProtocolHeader } from './ProtocolHeader';
import { ProtocolError } from './ProtocolError';
export class ProtocolResponse<ID> extends ProtocolHeader<ID> {
protected error: ProtocolError;
public constructor(response: ProtocolResponse<ID>) {
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;
}
}

View File

@ -1,2 +0,0 @@
export abstract class ProtocolSub {
}

View File

@ -1,6 +1,6 @@
import { ProtocolSub } from '../ProtocolSub';
import AbstractProtocol from './AbstractProtocol';
export class RPCRequest extends ProtocolSub {
export class Request<ID> extends AbstractProtocol<ID> {
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;

View File

@ -0,0 +1,32 @@
import AbstractProtocol from './AbstractProtocol';
import ProtocolError from './ProtocolError';
export class Response<ID> extends AbstractProtocol<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}]`);
}
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;

View File

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

View File

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

View File

@ -1,5 +0,0 @@
export * from './RPCRequest';
export * from './RPCResponse';
export const PROTOCOL_NAME = 'jsonrpc';