import WebSocket from "ws"; import { iDispatchResponseError } from "../../../../contracts/error/iDispatchResponseError"; import { iDispatchResponseMessage } from "../../../../contracts/messages/iDispatchResponseMessage"; import { ServerEnvironment } from "../../../configuration/serverEnvironment"; import { Kernel } from "../../kernel"; export class AcumenServerMessenger { public connections = []; public constructor(private kernel: Kernel, private serverEnvironment: ServerEnvironment) { } public alertSocketConnection(socket: WebSocket) { const length = this.connections.push(socket); const lines = [`[Socket Connect] to Acumen Testing Server: ${length}`]; this.kernel.modules.messages.info(this.serverEnvironment, lines); } public alertSocketDisconnection(socket: WebSocket) { const index = this.connections.indexOf(socket) this.connections.splice(index, 1) const lines = [`[Socket Disconnect] to Acumen Testing Server: ${index + 1}`]; this.kernel.modules.messages.info(this.serverEnvironment, lines); } public alertChange(socket: WebSocket) { const message = {} as iDispatchResponseMessage; message.url = "/message"; message.body = { type: "update", data: null }; message.headers = {}; socket.send(JSON.stringify(message)); } public sendError(socket: WebSocket, message: string) { const response = {} as iDispatchResponseError; response.url = "/error" response.body = new Error(message).stack.split("\n"); response.headers = { reason: "conditional" } socket.send(JSON.stringify(response)); } public sendNotification(socket: WebSocket, notification: string) { const message = {} as iDispatchResponseMessage; message.url = "/message"; message.body = { type: "notification", data: notification }; message.headers = {}; socket.send(JSON.stringify(message)); } public sendData(socket: WebSocket, data: any) { socket.send(JSON.stringify(data)); } public displayServerBooted(address: string, port: number) { const lines = [`[Acumen UI] running http://${address}:${port}`] this.kernel.modules.messages.info(this.serverEnvironment, lines); } }