Compare commits

..

5 commits

Author SHA1 Message Date
Benjamin Palko
b7921ddb36 DOCS 2025-05-22 15:39:48 -04:00
Benjamin Palko
000f040652 add color to server 2025-05-22 14:19:37 -04:00
Benjamin Palko
8fc179c406 uHM AKTHWUALLY 2025-05-22 14:18:36 -04:00
Benjamin Palko
e680978f2d cleanup 2025-05-22 11:48:06 -04:00
Benjamin Palko
0c91aecec4 implement client side 2025-05-22 09:42:11 -04:00

View file

@ -1,86 +1,88 @@
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
public static Spacetime Instance
{
get
{
get
{
if (_instance == null)
{
_instance = new Spacetime(HOST, DB_NAME);
}
return _instance;
}
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
// 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)
{
get => Connection.Db.User.Identity.Find(Identity);
GD.PrintErr($"Disconnected abnormally: {e}");
}
private static Spacetime _instance;
public Spacetime(string host, string dbName)
else
{
// 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.");
}
GD.Print($"Disconnected normally.");
}
}
}