Compare commits

..

5 commits

Author SHA1 Message Date
Benjamin Palko
c8d4423ba8 DOCS 2025-05-22 15:48:46 -04:00
Benjamin Palko
d818b735e5 add color to server 2025-05-22 15:48:46 -04:00
Benjamin Palko
6a4f5d9400 uHM AKTHWUALLY 2025-05-22 15:48:46 -04:00
Benjamin Palko
cd4842a7c4 cleanup 2025-05-22 15:48:46 -04:00
Benjamin Palko
f486ec8ea0 implement client side 2025-05-22 15:48:46 -04:00

View file

@ -1,88 +1,86 @@
using System;
using Godot;
using SpacetimeDB;
using SpacetimeDB.Types;
using System;
using System.Linq;
public class Spacetime
{
/// The URI of the SpacetimeDB instance hosting our chat database and module.
const string HOST = "http://localhost:3000";
/// The URI of the SpacetimeDB instance hosting our chat database and module.
const string HOST = "http://localhost:3000";
/// The database name we chose when we published our module.
const string DB_NAME = "massive";
/// The database name we chose when we published our module.
const string DB_NAME = "massive";
public static Spacetime Instance
{
get
public static Spacetime Instance
{
if (_instance == null)
{
_instance = new Spacetime(HOST, DB_NAME);
}
return _instance;
get
{
if (_instance == null)
{
_instance = new Spacetime(HOST, DB_NAME);
}
return _instance;
}
}
}
// our local client SpacetimeDB identity
public Identity Identity { get; private set; }
public DbConnection Connection { get; private set; }
public User Me
{
get => Connection.Db.User.Identity.Find(Identity);
}
private static Spacetime _instance;
public Spacetime(string host, string dbName)
{
// Initialize the `AuthToken` module
AuthToken.Init(".spacetime_csharp_quickstart");
Connection = DbConnection.Builder()
.WithUri(host)
.WithModuleName(dbName)
.WithToken(AuthToken.Token)
.OnConnect(OnConnected)
.OnConnectError(OnConnectError)
.OnDisconnect(OnDisconnected)
.Build();
}
/// Our `OnConnected` callback: save our credentials to a file.
void OnConnected(DbConnection conn, Identity identity, string authToken)
{
Identity = identity;
AuthToken.SaveToken(authToken);
conn.SubscriptionBuilder()
.OnApplied(OnSubscriptionApplied)
.SubscribeToAllTables();
}
/// Our `OnSubscriptionApplied` callback:
/// sort all past messages and print them in timestamp order.
void OnSubscriptionApplied(SubscriptionEventContext ctx)
{
GD.Print("Connected");
}
/// Our `OnConnectError` callback: print the error, then exit the process.
void OnConnectError(Exception e)
{
GD.PrintErr($"Error while connecting: {e}");
}
/// Our `OnDisconnect` callback: print a note, then exit the process.
void OnDisconnected(DbConnection conn, Exception? e)
{
if (e != null)
// our local client SpacetimeDB identity
public Identity Identity { get; private set; }
public DbConnection Connection { get; private set; }
public User Me
{
GD.PrintErr($"Disconnected abnormally: {e}");
get => Connection.Db.User.Identity.Find(Identity);
}
else
private static Spacetime _instance;
public Spacetime(string host, string dbName)
{
GD.Print($"Disconnected normally.");
// Initialize the `AuthToken` module
AuthToken.Init(".spacetime_csharp_quickstart");
Connection = DbConnection
.Builder()
.WithUri(host)
.WithModuleName(dbName)
.WithToken(AuthToken.Token)
.OnConnect(OnConnected)
.OnConnectError(OnConnectError)
.OnDisconnect(OnDisconnected)
.Build();
}
/// Our `OnConnected` callback: save our credentials to a file.
void OnConnected(DbConnection conn, Identity identity, string authToken)
{
Identity = identity;
AuthToken.SaveToken(authToken);
conn.SubscriptionBuilder().OnApplied(OnSubscriptionApplied).SubscribeToAllTables();
}
/// Our `OnSubscriptionApplied` callback:
/// sort all past messages and print them in timestamp order.
void OnSubscriptionApplied(SubscriptionEventContext ctx)
{
GD.Print("Connected");
}
/// Our `OnConnectError` callback: print the error, then exit the process.
void OnConnectError(Exception e)
{
GD.PrintErr($"Error while connecting: {e}");
}
/// Our `OnDisconnect` callback: print a note, then exit the process.
void OnDisconnected(DbConnection conn, Exception? e)
{
if (e != null)
{
GD.PrintErr($"Disconnected abnormally: {e}");
}
else
{
GD.Print($"Disconnected normally.");
}
}
}
}