ing
This commit is contained in:
parent
baf6c129a1
commit
e8ae690011
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
|
@ -0,0 +1,7 @@
|
|||
import Request from './Request';
|
||||
|
||||
export class Notification extends Request<any> {
|
||||
public constructor(method: string, params: any) {
|
||||
super(method, params);
|
||||
}
|
||||
}
|
|
@ -44,3 +44,5 @@ export class ProtocolError {
|
|||
return JSON.parse(json);
|
||||
}
|
||||
}
|
||||
|
||||
export default ProtocolError;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
import { ProtocolHeader } from './ProtocolHeader';
|
||||
|
||||
export class ProtocolRequest<ID> extends ProtocolHeader<ID> {
|
||||
public constructor(protocol: string, id: ID) {
|
||||
super(protocol, id);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -1,2 +0,0 @@
|
|||
export abstract class ProtocolSub {
|
||||
}
|
|
@ -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;
|
32
src/ts/@overflow/commons/websocket/protocol/Response.ts
Normal file
32
src/ts/@overflow/commons/websocket/protocol/Response.ts
Normal 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;
|
|
@ -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';
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
export * from './RPCRequest';
|
||||
export * from './RPCResponse';
|
||||
|
||||
export const PROTOCOL_NAME = 'jsonrpc';
|
||||
|
Loading…
Reference in New Issue
Block a user