implement client side
This commit is contained in:
parent
86804cf5f8
commit
228a9c96e3
19 changed files with 541 additions and 0 deletions
123
client/spacetime/Program.cs
Normal file
123
client/spacetime/Program.cs
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
// using SpacetimeDB;
|
||||
// using SpacetimeDB.Types;
|
||||
// using System;
|
||||
// using System.Collections.Concurrent;
|
||||
//
|
||||
// // our local client SpacetimeDB identity
|
||||
// Identity? local_identity = null;
|
||||
//
|
||||
// // declare a thread safe queue to store commands
|
||||
// var input_queue = new ConcurrentQueue<(string Command, string Args)>();
|
||||
//
|
||||
// void Main()
|
||||
// {
|
||||
// // Initialize the `AuthToken` module
|
||||
// AuthToken.Init(".spacetime_csharp_quickstart");
|
||||
// // Builds and connects to the database
|
||||
// DbConnection? conn = null;
|
||||
// conn = ConnectToDB();
|
||||
// // Registers to run in response to database events.
|
||||
// RegisterCallbacks(conn);
|
||||
// // Declare a threadsafe cancel token to cancel the process loop
|
||||
// var cancellationTokenSource = new CancellationTokenSource();
|
||||
// // Spawn a thread to call process updates and process commands
|
||||
// var thread = new Thread(() => ProcessThread(conn, cancellationTokenSource.Token));
|
||||
// thread.Start();
|
||||
// // Handles CLI input
|
||||
// InputLoop();
|
||||
// // This signals the ProcessThread to stop
|
||||
// cancellationTokenSource.Cancel();
|
||||
// thread.Join();
|
||||
// }
|
||||
//
|
||||
// /// 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 = "quickstart-chat";
|
||||
//
|
||||
// /// Load credentials from a file and connect to the database.
|
||||
// DbConnection ConnectToDB()
|
||||
// {
|
||||
// DbConnection? conn = null;
|
||||
// conn = DbConnection.Builder()
|
||||
// .WithUri(HOST)
|
||||
// .WithModuleName(DB_NAME)
|
||||
// .WithToken(AuthToken.Token)
|
||||
// .OnConnect(OnConnected)
|
||||
// .OnConnectError(OnConnectError)
|
||||
// .OnDisconnect(OnDisconnected)
|
||||
// .Build();
|
||||
// return conn;
|
||||
// }
|
||||
//
|
||||
// /// Our `OnConnected` callback: save our credentials to a file.
|
||||
// void OnConnected(DbConnection conn, Identity identity, string authToken)
|
||||
// {
|
||||
// local_identity = identity;
|
||||
// AuthToken.SaveToken(authToken);
|
||||
// }
|
||||
//
|
||||
// /// Our `OnConnectError` callback: print the error, then exit the process.
|
||||
// void OnConnectError(Exception e)
|
||||
// {
|
||||
// Console.Write($"Error while connecting: {e}");
|
||||
// }
|
||||
//
|
||||
// /// Our `OnDisconnect` callback: print a note, then exit the process.
|
||||
// void OnDisconnected(DbConnection conn, Exception? e)
|
||||
// {
|
||||
// if (e != null)
|
||||
// {
|
||||
// Console.Write($"Disconnected abnormally: {e}");
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// Console.Write($"Disconnected normally.");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /// Register all the callbacks our app will use to respond to database events.
|
||||
// void RegisterCallbacks(DbConnection conn)
|
||||
// {
|
||||
// conn.Db.User.OnInsert += User_OnInsert;
|
||||
// conn.Db.User.OnUpdate += User_OnUpdate;
|
||||
//
|
||||
// conn.Db.Message.OnInsert += Message_OnInsert;
|
||||
//
|
||||
// conn.Reducers.OnSetName += Reducer_OnSetNameEvent;
|
||||
// conn.Reducers.OnSendMessage += Reducer_OnSendMessageEvent;
|
||||
// }
|
||||
//
|
||||
// /// If the user has no set name, use the first 8 characters from their identity.
|
||||
// string UserNameOrIdentity(User user) => user.Name ?? user.Identity.ToString()[..8];
|
||||
//
|
||||
// /// Our `User.OnInsert` callback: if the user is online, print a notification.
|
||||
// void User_OnInsert(EventContext ctx, User insertedValue)
|
||||
// {
|
||||
// if (insertedValue.Online)
|
||||
// {
|
||||
// Console.WriteLine($"{UserNameOrIdentity(insertedValue)} is online");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /// Our `User.OnUpdate` callback:
|
||||
// /// print a notification about name and status changes.
|
||||
// void User_OnUpdate(EventContext ctx, User oldValue, User newValue)
|
||||
// {
|
||||
// if (oldValue.Name != newValue.Name)
|
||||
// {
|
||||
// Console.WriteLine($"{UserNameOrIdentity(oldValue)} renamed to {newValue.Name}");
|
||||
// }
|
||||
// if (oldValue.Online != newValue.Online)
|
||||
// {
|
||||
// if (newValue.Online)
|
||||
// {
|
||||
// Console.WriteLine($"{UserNameOrIdentity(newValue)} connected.");
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// Console.WriteLine($"{UserNameOrIdentity(newValue)} disconnected.");
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
1
client/spacetime/Program.cs.uid
Normal file
1
client/spacetime/Program.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://dtnd8t4orjlip
|
||||
88
client/spacetime/Spacetime.cs
Normal file
88
client/spacetime/Spacetime.cs
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
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 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