上一篇:Theia APIs——事件
通過JSON-PRC進行通信
概述
註冊服務
import { ContainerModule } from 'inversify'; import { ConnectionHandler, JsonRpcConnectionHandler } from "../../messaging/common"; import { ILoggerServer, ILoggerClient } from '../../application/common/logger-protocol'; export const loggerServerModule = new ContainerModule(bind => { bind(ConnectionHandler).toDynamicValue(ctx => new JsonRpcConnectionHandler<ILoggerClient>("/services/logger", client => { const loggerServer = ctx.container.get<ILoggerServer>(ILoggerServer); loggerServer.setClient(client); return loggerServer; }) ).inSingletonScope() });
我們來詳細看一下:
import { ConnectionHandler, JsonRpcConnectionHandler } from "../../messaging/common";
import { MessageConnection } from "vscode-jsonrpc"; export const ConnectionHandler = Symbol('ConnectionHandler'); export interface ConnectionHandler { readonly path: string; onConnection(connection: MessageConnection): void; }
import { ILoggerServer, ILoggerClient } from '../../application/common/logger-protocol';
文件logger-protocol.ts包含了服務器和客戶端需要實現的接口。
bind<ConnectionHandler>(ConnectionHandler).toDynamicValue(ctx => {
constructor( @inject(ContributionProvider) @named(ConnectionHandler) protected readonly handlers: ContributionProvider<ConnectionHandler>) { } onStart(server: http.Server): void { for (const handler of this.handlers.getContributions()) { const path = handler.path; try { createServerWebSocketConnection({ server, path }, connection => handler.onConnection(connection)); } catch (error) { console.error(error) } } }
new JsonRpcConnectionHandler<ILoggerClient>("/services/logger", client => {
我們來看看這個類的實現做了哪些事情:
export class JsonRpcConnectionHandler<T extends object> implements ConnectionHandler { constructor( readonly path: string, readonly targetFactory: (proxy: JsonRpcProxy<T>) => any ) { } onConnection(connection: MessageConnection): void { const factory = new JsonRpcProxyFactory<T>(this.path); const proxy = factory.createProxy(); factory.target = this.targetFactory(proxy); factory.listen(connection); } }
onConnection(connection: MessageConnection): void { const factory = new JsonRpcProxyFactory<T>(this.path); const proxy = factory.createProxy(); factory.target = this.targetFactory(proxy); factory.listen(connection);
我們一行一行來看:
const factory = new JsonRpcProxyFactory<T>(this.path);
上面這一行在路徑"/services/logger"上創建了一個JsonRpcProxy。
const proxy = factory.createProxy();
然後,我們從工廠創建了一個代理對象,它將使用ILoggerClient接口來調用JSON-RPC連接的另一端。
factory.target = this.targetFactory(proxy);
上面這一行將調用我們在參數中傳遞的函數,所以:
client => { const loggerServer = ctx.container.get<ILoggerServer>(ILoggerServer); loggerServer.setClient(client); return loggerServer; }
factory.listen(connection);
'/services/*': { target: 'ws://localhost:3000', ws: true },
連接到服務
import { ContainerModule, Container } from 'inversify'; import { WebSocketConnectionProvider } from '../../messaging/browser/connection'; import { ILogger, LoggerFactory, LoggerOptions, Logger } from '../common/logger'; import { ILoggerServer } from '../common/logger-protocol'; import { LoggerWatcher } from '../common/logger-watcher'; export const loggerFrontendModule = new ContainerModule(bind => { bind(ILogger).to(Logger).inSingletonScope(); bind(LoggerWatcher).toSelf().inSingletonScope(); bind(ILoggerServer).toDynamicValue(ctx => { const loggerWatcher = ctx.container.get(LoggerWatcher); const connection = ctx.container.get(WebSocketConnectionProvider); return connection.createProxy<ILoggerServer>("/services/logger", loggerWatcher.getLoggerClient()); }).inSingletonScope(); });
其中最重要的幾行:
bind(ILoggerServer).toDynamicValue(ctx => { const loggerWatcher = ctx.container.get(LoggerWatcher); const connection = ctx.container.get(WebSocketConnectionProvider); return connection.createProxy<ILoggerServer>("/services/logger", loggerWatcher.getLoggerClient()); }).inSingletonScope();
我們一行一行來看:
const loggerWatcher = ctx.container.get(LoggerWatcher);
const connection = ctx.container.get(WebSocketConnectionProvider);
上面這一行獲得了一個websocket連接,它將被用來創建一個代理。
return connection.createProxy<ILoggerServer>("/services/logger", loggerWatcher.getLoggerClient());
我們將一個本地對象作爲第二個參數傳入,用來處理來自遠程對象的JSON-RPC消息。有時,本地對象依賴於代理,在代理實例化之前無法實例化。這種情況下,代理接口應該實現JsonRpcServer,而本地對象應該作爲客戶端來提供。
export type JsonRpcServer<Client> = Disposable & { setClient(client: Client | undefined): void; }; export interface ILoggerServer extends JsonRpcServery<ILoggerClient> { // ... } const serverProxy = connection.createProxy<ILoggerServer>("/services/logger"); const client = loggerWatcher.getLoggerClient(); serverProxy.setClient(client);
createProxy<T extends object>(path: string, target?: object, options?: WebSocketOptions): T { const factory = new JsonRpcProxyFactory<T>(path, target); this.listen(factory, options); return factory.createProxy(); }
-
在路徑"logger"上創建JsonRpc代理。
-
公開loggerWatcher.getLoggerClient()對象。
-
返回ILoggerServer類型的代理。
在示例的前端和後端加載模塊
import { loggerServerModule } from 'theia-core/lib/application/node/logger-server-module';
然後將其載入到主容器。
container.load(loggerServerModule);
import { loggerFrontendModule } from 'theia-core/lib/application/browser/logger-frontend-module';
container.load(frontendLanguagesModule);