Add service client support

This commit is contained in:
2022-07-21 18:33:23 +02:00
parent 327d7dfac6
commit 1774306b06
6 changed files with 338 additions and 46 deletions

View File

@ -1,12 +1,42 @@
import 'dart:async';
import 'dart:io';
import "./out/lib/example.dart";
import "dart:convert";
int main() {
var t = TestAtom(val_boolean: false, val_number: 1, val_string: "hi");
print(jsonEncode(t));
void main() async {
var str = StreamController<Map<String, dynamic>>();
var t2 = TestEnum.VAL2;
print(jsonEncode(t2));
var provider = ServiceProvider(str.stream);
var sock = await Socket.connect("127.0.0.1", 8859);
return 0;
utf8.decoder.bind(sock).transform(new LineSplitter()).listen((line) {
str.add(jsonDecode(line));
});
provider.output.stream.listen((event) {
sock.writeln(jsonEncode(event));
});
var s = new TestServiceClient(provider);
var r = await s.AddValuesMultipleParams(10, 15);
print(r);
var r2 =
await s.AddValuesSingleParam(AddValueRequest(value1: 10, value2: 15));
print(r2?.value);
var catched = false;
await s.ThrowingError().catchError((err) {
catched = true;
print("Expected error was catched: " + err.toString());
});
if (!catched) {
throw Error();
}
await sock.close();
// exit(0);
}

View File

@ -0,0 +1,62 @@
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)

View File

@ -22,7 +22,7 @@ class TestService extends Server.TestService<undefined> {
ctx: undefined
): Promise<AddValueResponse> {
return {
value: request.value1 + request.value2,
value: request.value1 + request!.value2,
};
}
async AddValuesMultipleParams(