mirror of
https://github.com/clockworklabs/SpacetimeDB.git
synced 2026-05-06 15:49:35 -04:00
98585e858d
# Description of Changes Modules are like programs, databases are like processes. Your client connects to a remote database, not a remote module. <!-- Please describe your change, mention any related tickets, and so on here. --> # API and ABI breaking changes Yep! # Expected complexity level and risk 2? I made this change with find + replace, and it's possible that I borked the edit, or missed some reference somehow. It seems unlikely, however, that this change will have deeper implications we haven't considered. # Testing N/a
79 lines
1.8 KiB
C#
79 lines
1.8 KiB
C#
using System;
|
|
using System.Threading;
|
|
using SpacetimeDB;
|
|
using SpacetimeDB.Types;
|
|
|
|
/// The URI of the SpacetimeDB instance hosting our chat module.
|
|
string HOST = Environment.GetEnvironmentVariable("SPACETIMEDB_HOST") ?? "http://localhost:3000";
|
|
|
|
/// The module name we chose when we published our module.
|
|
string DB_NAME = Environment.GetEnvironmentVariable("SPACETIMEDB_DB_NAME") ?? "my-db";
|
|
|
|
void Main()
|
|
{
|
|
// Initialize the AuthToken module
|
|
AuthToken.Init(".spacetime_csharp");
|
|
|
|
// Build and connect to the database
|
|
var conn = DbConnection.Builder()
|
|
.WithUri(HOST)
|
|
.WithDatabaseName(DB_NAME)
|
|
.WithToken(AuthToken.Token)
|
|
.OnConnect(OnConnected)
|
|
.OnConnectError(OnConnectError)
|
|
.OnDisconnect(OnDisconnected)
|
|
.Build();
|
|
|
|
// Keep the connection alive and process updates
|
|
try
|
|
{
|
|
while (true)
|
|
{
|
|
conn.FrameTick();
|
|
Thread.Sleep(100);
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
conn.Disconnect();
|
|
}
|
|
}
|
|
|
|
void OnConnected(DbConnection conn, Identity identity, string authToken)
|
|
{
|
|
Console.WriteLine($"Connected to {DB_NAME}");
|
|
Console.WriteLine($"Identity: {identity}");
|
|
|
|
// Save credentials for future sessions
|
|
AuthToken.SaveToken(authToken);
|
|
|
|
// Subscribe to all tables to receive updates
|
|
conn.SubscriptionBuilder()
|
|
.OnApplied(OnSubscriptionApplied)
|
|
.SubscribeToAllTables();
|
|
}
|
|
|
|
void OnConnectError(Exception e)
|
|
{
|
|
Console.WriteLine($"Connection error: {e.Message}");
|
|
}
|
|
|
|
void OnDisconnected(DbConnection conn, Exception? e)
|
|
{
|
|
if (e != null)
|
|
{
|
|
Console.WriteLine($"Disconnected with error: {e.Message}");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Disconnected");
|
|
}
|
|
}
|
|
|
|
void OnSubscriptionApplied(SubscriptionEventContext ctx)
|
|
{
|
|
Console.WriteLine("Subscription applied - ready to receive updates");
|
|
}
|
|
|
|
Main();
|