@loopback/socketio
Version:
LoopBack's WebSocket server based on socket.io
42 lines (38 loc) • 1.26 kB
text/typescript
// Copyright IBM Corp. and LoopBack contributors 2020. All Rights Reserved.
// Node module: @loopback/socketio
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {Context, ControllerClass, CoreBindings, inject} from '@loopback/core';
import {SocketIoBindings} from './keys';
import {
SocketIoInvokeMethod,
SocketIoRejectMethod,
SocketIoSendMethod,
SocketIoSequence,
} from './types';
export class DefaultSocketIoSequence implements SocketIoSequence {
constructor(
.context() protected context: Context,
(CoreBindings.CONTROLLER_CURRENT)
protected controller: ControllerClass,
(SocketIoBindings.INVOKE_METHOD)
protected invoke: SocketIoInvokeMethod,
(SocketIoBindings.SEND_METHOD)
protected send: SocketIoSendMethod,
(SocketIoBindings.REJECT_METHOD)
protected reject: SocketIoRejectMethod,
) {}
async handle(methodName: string, args: unknown[], done: Function) {
try {
const result = await this.invoke(
this.context,
this.controller,
methodName,
args,
);
await this.send(done, result);
} catch (err) {
await this.reject(done, err);
}
}
}