62 lines
1.6 KiB
TypeScript
62 lines
1.6 KiB
TypeScript
|
|
||
|
import * as net from "net";
|
||
|
import { Server, AddValueRequest, AddValueResponse } from "./out";
|
||
|
import * as readline from 'node:readline';
|
||
|
|
||
|
|
||
|
const server = new Server.ServiceProvider();
|
||
|
|
||
|
class TestService extends Server.TestService<undefined> {
|
||
|
async AddValuesSingleParam(
|
||
|
request: AddValueRequest,
|
||
|
ctx: undefined
|
||
|
): Promise<AddValueResponse> {
|
||
|
return {
|
||
|
value: request.value1 + request!.value2,
|
||
|
};
|
||
|
}
|
||
|
async AddValuesMultipleParams(
|
||
|
value1: number,
|
||
|
value2: number,
|
||
|
ctx: undefined
|
||
|
): Promise<number> {
|
||
|
return value1 + value2;
|
||
|
}
|
||
|
|
||
|
async ReturningVoid(param1) {
|
||
|
console.log("Calling Returning Void");
|
||
|
}
|
||
|
|
||
|
OnEvent(param1: string, ctx: undefined): void {
|
||
|
console.log("Received notification", param1);
|
||
|
}
|
||
|
|
||
|
async FunctionWithArrayAsParamAndReturn(vals1, vals2) {
|
||
|
return [...vals1, ...vals2];
|
||
|
}
|
||
|
|
||
|
async ThrowingError(ctx: undefined): Promise<void> {
|
||
|
throw new Error("Remote error!");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
server.addService(new TestService());
|
||
|
|
||
|
net.createServer((socket) => {
|
||
|
socket.on("error", console.error);
|
||
|
|
||
|
console.log("Connection from:", socket.remoteAddress);
|
||
|
const sess = server.getSession(msg => {
|
||
|
const s = JSON.stringify(msg);
|
||
|
console.log("Sending:", s);
|
||
|
socket.write(s + "\n");
|
||
|
});
|
||
|
const rl = readline.createInterface(socket);
|
||
|
rl.on("line", line => {
|
||
|
console.log("Receiving:", line);
|
||
|
sess.onMessage(JSON.parse(line));
|
||
|
})
|
||
|
rl.on("error", console.error);
|
||
|
}).listen(8859).on("listening", () => {
|
||
|
console.log("Is listening on :8859");
|
||
|
}).on("error", console.error)
|