// 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."); // } // } // }