-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProgram.cs
More file actions
83 lines (71 loc) · 2.75 KB
/
Program.cs
File metadata and controls
83 lines (71 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.Threading.Tasks;
using Lib.GAB.Server;
using Lib.GAB.Tools;
using GabpApi = Lib.GAB.Gabp;
// Minimal host-side GABP server:
// - works standalone or under GABS
// - exposes two tools and one event channel
// - keeps the lifecycle flat so it can be copied into an existing game/app host
var tools = new HelloWorldTools();
// One factory works for local smoke tests and for real launches under GABS
using var server = GabpApi.CreateGabsAwareServerWithInstance("HelloWorldHost", "1.0.0", tools);
// Keep one event channel in the sample so the event side of GABP is visible too
server.Events.RegisterChannel("host/lifecycle", "Lifecycle events from the hello world bridge host");
await server.StartAsync();
await EmitLifecycleAsync(server, "started");
ShowStartup(server);
// In a real host, replace this with your game's or app's normal lifetime hook
Console.ReadLine();
await EmitLifecycleAsync(server, "stopping");
await server.StopAsync();
static void ShowStartup(GabpServer server)
{
var runtimeMode = GabpApi.IsRunningUnderGabs()
? "Running under GABS. Configuration came from the GABS environment."
: "Running standalone. Use the printed port and token to connect a bridge directly.";
Console.WriteLine($"""
GABP server started on port {server.Port}
Token: {server.Token}
{runtimeMode}
Available tools: hello/ping, hello/echo
Available event channel: host/lifecycle
Press Enter to stop.
""");
}
static Task EmitLifecycleAsync(GabpServer server, string state)
{
return server.Events.EmitEventAsync("host/lifecycle", new
{
state,
timestampUtc = DateTime.UtcNow.ToString("O")
});
}
public sealed class HelloWorldTools
{
[Tool(
"hello/ping",
Description = "Confirm that the host is alive.",
ResultDescription = "A human-readable status message and a UTC timestamp from the running host.")]
[ToolResponse("message", Type = "string", Description = "Human-readable status from the host")]
[ToolResponse("timestampUtc", Type = "string", Description = "Current UTC timestamp from the host")]
public object Ping()
{
return new
{
message = "Hello from Lib.GAB",
timestampUtc = DateTime.UtcNow.ToString("O")
};
}
[Tool(
"hello/echo",
Description = "Echo a message back to the caller.",
ResultDescription = "The message returned by the host.")]
[ToolResponse("message", Type = "string", Description = "Echoed message from the host")]
public object Echo(
[ToolParameter(Description = "Message to echo back", Required = false, DefaultValue = "Hello GABS")]
string message = "Hello GABS")
{
return new { message };
}
}