Compare commits

...

6 commits

Author SHA1 Message Date
Benjamin Palko
d337c0507d add color to server 2025-05-22 18:11:17 -04:00
Benjamin Palko
2caf0d4d91 uHM AKTHWUALLY 2025-05-22 15:49:32 -04:00
Benjamin Palko
0017fc4c9f cleanup 2025-05-22 15:49:32 -04:00
Benjamin Palko
edb2cb6ef9 implement client side 2025-05-22 15:49:32 -04:00
Benjamin Palko
d828eeb1f7 followed docs on server setup 2025-05-22 15:49:32 -04:00
Benjamin Palko
2414b3f5b6 init bun and docker compose 2025-05-20 14:26:53 -04:00
32 changed files with 795 additions and 0 deletions

46
README.md Normal file
View file

@ -0,0 +1,46 @@
# Massive
<!--toc:start-->
- [Massive](#massive)
- [Setup](#setup) - [Install run-times](#install-run-times) - [Install SpacetimeDB](#install-spacetimedb)
<!--toc:end-->
## Setup
### Install run-times
```bash
mise install
```
### Install SpacetimeDB
```bash
curl -sSf https://install.spacetimedb.com | sh
```
Installs to
```
CLI configuration directory: /home/baobeld/.config/spacetime/
Spacetime binary: /home/baobeld/.local/bin/spacetime
directory for installed SpacetimeDB versions: /home/baobeld/.local/share/spacetime/bin
database directory: /home/baobeld/.local/share/spacetime/data
```
And enable WebAssembly:
```bash
dotnet workload install wasi-experimental
```
```bash
bun server:up
```
Add the spacetime docker server to your cli
```bash
spacetime server add --url http://localhost:3000 massive
```

BIN
bun.lockb Executable file

Binary file not shown.

5
client/.editorconfig Normal file
View file

@ -0,0 +1,5 @@
root = true
[*]
end_of_line = lf
insert_final_newline = true

4
client/.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
# Godot 4+ specific ignores
.godot/
/android/
module_bindings/

22
client/Game.cs Normal file
View file

@ -0,0 +1,22 @@
using Godot;
public partial class Game : Node
{
private Spacetime spacetime;
public override void _EnterTree()
{
spacetime = Spacetime.Instance;
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
spacetime.Connection.FrameTick();
}
public override void _ExitTree()
{
spacetime.Connection.Disconnect();
}
}

1
client/Game.cs.uid Normal file
View file

@ -0,0 +1 @@
uid://bg6n0cm6dds7j

17
client/UserUtils.cs Normal file
View file

@ -0,0 +1,17 @@
using Godot;
using SpacetimeDB.Types;
public static class UserUtils
{
public static Color ParseColor(User user)
{
return user.Color != null && Color.HtmlIsValid(user.Color)
? Color.FromHtml(user.Color)
: Colors.AliceBlue;
}
public static string UserNameOrIdentity(User user)
{
return user != null ? user.Name ?? user.Identity.ToString()[..8] : "unknown";
}
}

1
client/UserUtils.cs.uid Normal file
View file

@ -0,0 +1 @@
uid://dnaxnse8ipje0

39
client/chat/ChatInput.cs Normal file
View file

@ -0,0 +1,39 @@
using Godot;
using SpacetimeDB;
using SpacetimeDB.Types;
public partial class ChatInput : LineEdit
{
public override void _EnterTree()
{
TextSubmitted += OnMessageInput;
}
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
DbConnection conn = Spacetime.Instance.Connection;
RegisterSubscriptions(conn);
}
void OnMessageInput(string text)
{
Spacetime.Instance.Connection.Reducers.SendMessage(text);
Text = "";
}
void RegisterSubscriptions(DbConnection conn)
{
conn.Reducers.OnSendMessage += Reducer_OnSendMessageEvent;
}
/// Our `OnSendMessageEvent` callback: print a warning if the reducer failed.
void Reducer_OnSendMessageEvent(ReducerEventContext ctx, string text)
{
var e = ctx.Event;
if (e.CallerIdentity == Spacetime.Instance.Identity && e.Status is Status.Failed(var error))
{
GD.PrintErr($"Failed to send message {text}: {error}");
}
}
}

View file

@ -0,0 +1 @@
uid://115ssbk8epio

94
client/chat/ChatLog.cs Normal file
View file

@ -0,0 +1,94 @@
using Godot;
using SpacetimeDB;
using SpacetimeDB.Types;
public partial class ChatLog : RichTextLabel
{
private static readonly Color SystemColor = Colors.Gray;
public override void _EnterTree()
{
ClearLog();
}
public override void _Ready()
{
DbConnection conn = Spacetime.Instance.Connection;
RegisterSubscriptions(conn);
}
void PushMessage(string name, string message, Color color)
{
string entry = $"[color={color.ToHtml()}]{name}:[/color] {message}";
this.Text += $"{entry}\n";
}
void ClearLog()
{
Text = "";
}
void RegisterSubscriptions(DbConnection conn)
{
conn.Db.User.OnInsert += User_OnInsert;
conn.Db.User.OnUpdate += User_OnUpdate;
conn.Db.Message.OnInsert += Message_OnInsert;
}
void User_OnInsert(EventContext ctx, User insertedValue)
{
if (ctx.Event is Event<Reducer>.SubscribeApplied)
{
return;
}
PushMessage(
"System",
$"{UserUtils.UserNameOrIdentity(insertedValue)} connected",
SystemColor
);
}
void User_OnUpdate(EventContext ctx, User oldValue, User newValue)
{
if (oldValue.Name != newValue.Name)
{
PushMessage(
"System",
$"{UserUtils.UserNameOrIdentity(oldValue)} renamed to {UserUtils.UserNameOrIdentity(newValue)}",
SystemColor
);
}
if (oldValue.Online != newValue.Online)
{
if (newValue.Online)
{
PushMessage(
"System",
$"{UserUtils.UserNameOrIdentity(newValue)} connected",
SystemColor
);
}
else
{
PushMessage(
"System",
$"{UserUtils.UserNameOrIdentity(newValue)} disconnected",
SystemColor
);
}
}
}
void Message_OnInsert(EventContext ctx, Message insertedValue)
{
User sender = ctx.Db.User.Identity.Find(insertedValue.Sender);
Color color = ctx.Identity == sender.Identity ? UserUtils.ParseColor(sender) : Colors.Red;
if (ctx.Event is Event<Reducer>.SubscribeApplied)
{
PushMessage(UserUtils.UserNameOrIdentity(sender), insertedValue.Text, color);
return;
}
PushMessage(UserUtils.UserNameOrIdentity(sender), insertedValue.Text, color);
}
}

View file

@ -0,0 +1 @@
uid://cgn6wa7td0ekp

105
client/chat/ChatOptions.cs Normal file
View file

@ -0,0 +1,105 @@
using Godot;
using SpacetimeDB;
using SpacetimeDB.Types;
public partial class ChatOptions : HBoxContainer
{
public LineEdit Username { get; private set; }
public ColorPickerButton ColorPicker { get; private set; }
public override void _EnterTree()
{
Username = GetNode<LineEdit>("Username");
ColorPicker = GetNode<ColorPickerButton>("ColorPicker");
Username.TextSubmitted += OnUsernameChanged;
Username.FocusExited += ResetUsername;
ColorPicker.ColorChanged += OnColorChange;
}
public override void _Ready()
{
DbConnection conn = Spacetime.Instance.Connection;
RegisterSubscriptions(conn);
}
void ResetUsername()
{
Username.Text = UserUtils.UserNameOrIdentity(Spacetime.Instance.Me);
}
void OnUsernameChanged(string text)
{
Spacetime.Instance.Connection.Reducers.SetName(text);
}
void OnColorChange(Color color)
{
Spacetime.Instance.Connection.Reducers.SetColor(color.ToHtml(false));
}
void RegisterSubscriptions(DbConnection conn)
{
conn.Db.User.OnInsert += User_OnInsert;
conn.Db.User.OnUpdate += User_OnUpdate;
conn.Reducers.OnSetName += Reducer_OnSetNameEvent;
conn.Reducers.OnSetColor += Reducer_OnSetColorEvent;
}
void User_OnInsert(EventContext ctx, User insertedValue)
{
// It's me
if (ctx.Identity == insertedValue.Identity)
{
Username.Text = insertedValue.Name;
ColorPicker.Color =
insertedValue.Color != null && Color.HtmlIsValid(insertedValue.Color)
? Color.FromHtml(insertedValue.Color)
: Colors.AliceBlue;
}
}
void User_OnUpdate(EventContext ctx, User oldValue, User newValue)
{
if (ctx.Identity == oldValue.Identity && ctx.Identity == newValue.Identity)
{
Username.Text = newValue.Name;
ColorPicker.Color =
newValue.Color != null && Color.HtmlIsValid(newValue.Color)
? Color.FromHtml(newValue.Color)
: Colors.AliceBlue;
}
}
/// Our `OnSetNameEvent` callback: print a warning if the reducer failed.
void Reducer_OnSetNameEvent(ReducerEventContext ctx, string name)
{
var e = ctx.Event;
if (e.CallerIdentity != Spacetime.Instance.Identity)
{
// Not me
return;
}
if (e.Status is Status.Failed(var error))
{
GD.PrintErr($"Failed to change name to {name}: {error}");
return;
}
}
void Reducer_OnSetColorEvent(ReducerEventContext ctx, string color)
{
var e = ctx.Event;
if (e.CallerIdentity != Spacetime.Instance.Identity)
{
// Not me
return;
}
if (e.Status is Status.Failed(var error))
{
GD.PrintErr($"Failed to change color to {color}: {error}");
return;
}
}
}

View file

@ -0,0 +1 @@
uid://tff5u6blrc1d

16
client/chat/ChatWindow.cs Normal file
View file

@ -0,0 +1,16 @@
using Godot;
public partial class ChatWindow : VBoxContainer
{
private ChatOptions _options;
private ChatLog _log;
private LineEdit _input;
// Called when the node enters the scene tree for the first time.
public override void _EnterTree()
{
_options = GetNode<ChatOptions>("Options");
_log = GetNode<ChatLog>("ChatLog");
_input = GetNode<ChatInput>("ChatInput");
}
}

View file

@ -0,0 +1 @@
uid://c3s41dv7hv4md

View file

@ -0,0 +1,44 @@
[gd_scene load_steps=5 format=3 uid="uid://cqmy41vtnqd6f"]
[ext_resource type="Script" uid="uid://c3s41dv7hv4md" path="res://chat/ChatWindow.cs" id="1_d8jvm"]
[ext_resource type="Script" uid="uid://cgn6wa7td0ekp" path="res://chat/ChatLog.cs" id="2_fkxbv"]
[ext_resource type="Script" uid="uid://tff5u6blrc1d" path="res://chat/ChatOptions.cs" id="2_lvlsn"]
[ext_resource type="Script" uid="uid://115ssbk8epio" path="res://chat/ChatInput.cs" id="4_yd183"]
[node name="Chat Window" type="VBoxContainer"]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_d8jvm")
[node name="Options" type="HBoxContainer" parent="."]
layout_mode = 2
alignment = 2
script = ExtResource("2_lvlsn")
[node name="Label" type="Label" parent="Options"]
layout_mode = 2
text = "Username:"
[node name="Username" type="LineEdit" parent="Options"]
custom_minimum_size = Vector2(120, 0)
layout_mode = 2
[node name="ColorPicker" type="ColorPickerButton" parent="Options"]
layout_mode = 2
text = "Color"
[node name="ChatLog" type="RichTextLabel" parent="."]
layout_mode = 2
size_flags_vertical = 3
bbcode_enabled = true
text = "BBCode [color=green]test[/color]"
scroll_following = true
vertical_alignment = 2
script = ExtResource("2_fkxbv")
[node name="ChatInput" type="LineEdit" parent="."]
layout_mode = 2
script = ExtResource("4_yd183")

9
client/client.csproj Normal file
View file

@ -0,0 +1,9 @@
<Project Sdk="Godot.NET.Sdk/4.4.1">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SpacetimeDB.ClientSDK" Version="1.1.1" />
</ItemGroup>
</Project>

19
client/client.sln Normal file
View file

@ -0,0 +1,19 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "client", "client.csproj", "{CCE38DAE-1CEC-451D-8D68-43EEB50F989E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
ExportDebug|Any CPU = ExportDebug|Any CPU
ExportRelease|Any CPU = ExportRelease|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{CCE38DAE-1CEC-451D-8D68-43EEB50F989E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CCE38DAE-1CEC-451D-8D68-43EEB50F989E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CCE38DAE-1CEC-451D-8D68-43EEB50F989E}.ExportDebug|Any CPU.ActiveCfg = ExportDebug|Any CPU
{CCE38DAE-1CEC-451D-8D68-43EEB50F989E}.ExportDebug|Any CPU.Build.0 = ExportDebug|Any CPU
{CCE38DAE-1CEC-451D-8D68-43EEB50F989E}.ExportRelease|Any CPU.ActiveCfg = ExportRelease|Any CPU
{CCE38DAE-1CEC-451D-8D68-43EEB50F989E}.ExportRelease|Any CPU.Build.0 = ExportRelease|Any CPU
EndGlobalSection
EndGlobal

1
client/icon.svg Normal file
View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="128" height="128"><rect width="124" height="124" x="2" y="2" fill="#363d52" stroke="#212532" stroke-width="4" rx="14"/><g fill="#fff" transform="translate(12.322 12.322)scale(.101)"><path d="M105 673v33q407 354 814 0v-33z"/><path fill="#478cbf" d="m105 673 152 14q12 1 15 14l4 67 132 10 8-61q2-11 15-15h162q13 4 15 15l8 61 132-10 4-67q3-13 15-14l152-14V427q30-39 56-81-35-59-83-108-43 20-82 47-40-37-88-64 7-51 8-102-59-28-123-42-26 43-46 89-49-7-98 0-20-46-46-89-64 14-123 42 1 51 8 102-48 27-88 64-39-27-82-47-48 49-83 108 26 42 56 81zm0 33v39c0 276 813 276 814 0v-39l-134 12-5 69q-2 10-14 13l-162 11q-12 0-16-11l-10-65H446l-10 65q-4 11-16 11l-162-11q-12-3-14-13l-5-69z"/><path d="M483 600c0 34 58 34 58 0v-86c0-34-58-34-58 0z"/><circle cx="725" cy="526" r="90"/><circle cx="299" cy="526" r="90"/></g><g fill="#414042" transform="translate(12.322 12.322)scale(.101)"><circle cx="307" cy="532" r="60"/><circle cx="717" cy="532" r="60"/></g></svg>

After

Width:  |  Height:  |  Size: 994 B

37
client/icon.svg.import Normal file
View file

@ -0,0 +1,37 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://bqjxu2injm0kq"
path="res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://icon.svg"
dest_files=["res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
svg/scale=1.0
editor/scale_with_editor_scale=false
editor/convert_colors_with_editor_theme=false

26
client/project.godot Normal file
View file

@ -0,0 +1,26 @@
; Engine configuration file.
; It's best edited using the editor UI and not directly,
; since the parameters that go here are not all obvious.
;
; Format:
; [section] ; section goes between []
; param=value ; assign values to parameters
config_version=5
[application]
config/name="Massive"
run/main_scene="uid://bukwg8y6gdt3g"
config/features=PackedStringArray("4.4", "C#", "Forward Plus")
config/icon="res://icon.svg"
[dotnet]
project/assembly_name="client"
[file_customization]
folder_colors={
"res://module_bindings/": "gray"
}

9
client/root.tscn Normal file
View file

@ -0,0 +1,9 @@
[gd_scene load_steps=3 format=3 uid="uid://bukwg8y6gdt3g"]
[ext_resource type="PackedScene" uid="uid://cqmy41vtnqd6f" path="res://chat/ChatWindow.tscn" id="1_pq8q7"]
[ext_resource type="Script" uid="uid://bg6n0cm6dds7j" path="res://Game.cs" id="1_pyidc"]
[node name="Root" type="Node"]
script = ExtResource("1_pyidc")
[node name="Chat Window" parent="." instance=ExtResource("1_pq8q7")]

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

View file

@ -0,0 +1 @@
uid://dug30f321xvvt

6
docker-compose.yml Normal file
View file

@ -0,0 +1,6 @@
services:
spacetime:
image: clockworklabs/spacetime
ports:
- 3000:3000
command: start

28
massive.sln Normal file
View file

@ -0,0 +1,28 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "client", "client\client.csproj", "{BDFCBF69-C44E-4188-A9FC-2CFED6EC2246}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StdbModule", "server\StdbModule.csproj", "{25B51793-082D-44ED-A8D2-4A87A11F1882}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BDFCBF69-C44E-4188-A9FC-2CFED6EC2246}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BDFCBF69-C44E-4188-A9FC-2CFED6EC2246}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BDFCBF69-C44E-4188-A9FC-2CFED6EC2246}.Release|Any CPU.ActiveCfg = Debug|Any CPU
{BDFCBF69-C44E-4188-A9FC-2CFED6EC2246}.Release|Any CPU.Build.0 = Debug|Any CPU
{25B51793-082D-44ED-A8D2-4A87A11F1882}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{25B51793-082D-44ED-A8D2-4A87A11F1882}.Debug|Any CPU.Build.0 = Debug|Any CPU
{25B51793-082D-44ED-A8D2-4A87A11F1882}.Release|Any CPU.ActiveCfg = Release|Any CPU
{25B51793-082D-44ED-A8D2-4A87A11F1882}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

13
package.json Normal file
View file

@ -0,0 +1,13 @@
{
"name": "massive",
"scripts": {
"server:up": "docker compose up -d",
"server:down": "docker compose down",
"server:build": "dotnet build server",
"server:publish": "spacetime publish --project-path server massive",
"server:generate": "spacetime generate --lang csharp --out-dir client/module_bindings --project-path server"
},
"devDependencies": {
"@types/bun": "latest"
}
}

2
server/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
bin
obj

140
server/Lib.cs Normal file
View file

@ -0,0 +1,140 @@
using System.Text.RegularExpressions;
using SpacetimeDB;
public static partial class Module
{
[Table(Name = "User", Public = true)]
public partial class User
{
[PrimaryKey]
public Identity Identity;
public string? Name;
public string? Color;
public bool Online;
}
[Table(Name = "Message", Public = true)]
public partial class Message
{
public Identity Sender;
public Timestamp Sent;
public string Text = "";
}
[Reducer]
public static void SetName(ReducerContext ctx, string name)
{
name = ValidateName(name);
var user = ctx.Db.User.Identity.Find(ctx.Sender);
if (user is not null)
{
user.Name = name;
ctx.Db.User.Identity.Update(user);
}
}
/// Takes a name and checks if it's acceptable as a user's name.
private static string ValidateName(string name)
{
if (string.IsNullOrEmpty(name))
{
throw new Exception("Names must not be empty");
}
return name;
}
[Reducer]
public static void SetColor(ReducerContext ctx, string color)
{
color = ValidateColor(color);
var user = ctx.Db.User.Identity.Find(ctx.Sender);
if (user is not null)
{
user.Color = color;
ctx.Db.User.Identity.Update(user);
}
}
private static string ValidateColor(string color)
{
var regex = new Regex("^([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$");
if (!regex.IsMatch(color))
{
throw new Exception("Invalid color code");
}
return color;
}
[Reducer]
public static void SendMessage(ReducerContext ctx, string text)
{
text = ValidateMessage(text);
Log.Info(text);
ctx.Db.Message.Insert(
new Message
{
Sender = ctx.Sender,
Text = text,
Sent = ctx.Timestamp,
}
);
}
/// Takes a message's text and checks if it's acceptable to send.
private static string ValidateMessage(string text)
{
if (string.IsNullOrEmpty(text))
{
throw new ArgumentException("Messages must not be empty");
}
return text;
}
[Reducer(ReducerKind.ClientConnected)]
public static void ClientConnected(ReducerContext ctx)
{
Log.Info($"Connect {ctx.Sender}");
var user = ctx.Db.User.Identity.Find(ctx.Sender);
if (user is not null)
{
// If this is a returning user, i.e., we already have a `User` with this `Identity`,
// set `Online: true`, but leave `Name` and `Identity` unchanged.
user.Online = true;
ctx.Db.User.Identity.Update(user);
}
else
{
// If this is a new user, create a `User` object for the `Identity`,
// which is online, but hasn't set a name.
ctx.Db.User.Insert(
new User
{
Name = null,
Identity = ctx.Sender,
Online = true,
}
);
}
}
[Reducer(ReducerKind.ClientDisconnected)]
public static void ClientDisconnected(ReducerContext ctx)
{
var user = ctx.Db.User.Identity.Find(ctx.Sender);
if (user is not null)
{
// This user should exist, so set `Online: false`.
user.Online = false;
ctx.Db.User.Identity.Update(user);
}
else
{
// User does not exist, log warning
Log.Warn("Warning: No user found for disconnected client.");
}
}
}

14
server/StdbModule.csproj Normal file
View file

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<RuntimeIdentifier>wasi-wasm</RuntimeIdentifier>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SpacetimeDB.Runtime" Version="1.1.*" />
</ItemGroup>
</Project>

6
server/global.json Normal file
View file

@ -0,0 +1,6 @@
{
"sdk": {
"version": "8.0.400",
"rollForward": "latestMinor"
}
}