implement client side
This commit is contained in:
parent
86804cf5f8
commit
f486ec8ea0
17 changed files with 415 additions and 0 deletions
86
client/spacetime/Spacetime.cs
Normal file
86
client/spacetime/Spacetime.cs
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
using System;
|
||||
using Godot;
|
||||
using SpacetimeDB;
|
||||
using SpacetimeDB.Types;
|
||||
|
||||
public class Spacetime
|
||||
{
|
||||
/// 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";
|
||||
|
||||
public static Spacetime 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)
|
||||
{
|
||||
GD.PrintErr($"Disconnected abnormally: {e}");
|
||||
}
|
||||
else
|
||||
{
|
||||
GD.Print($"Disconnected normally.");
|
||||
}
|
||||
}
|
||||
}
|
||||
1
client/spacetime/Spacetime.cs.uid
Normal file
1
client/spacetime/Spacetime.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://dug30f321xvvt
|
||||
Loading…
Add table
Add a link
Reference in a new issue