-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Improvements from v0.7.2-beta * Added EditorNetworkManager * Updated Unity package version --------- Co-authored-by: John Detter <[email protected]>
- Loading branch information
Showing
6 changed files
with
608 additions
and
293 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using System; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using UnityEngine; | ||
|
||
namespace SpacetimeDB | ||
{ | ||
public class EditorNetworkManager | ||
{ | ||
SpacetimeDB.WebSocket webSocket; | ||
public bool IsConnected => isConnected; | ||
private bool isConnected; | ||
|
||
public event Action<string,ClientApi.Event.Types.Status> onTransactionComplete; | ||
|
||
public static string GetTokenKey() | ||
{ | ||
var key = "spacetimedb.identity_token"; | ||
#if UNITY_EDITOR | ||
// Different editors need different keys | ||
key += $" - {Application.dataPath}"; | ||
#endif | ||
return key; | ||
} | ||
|
||
public EditorNetworkManager(string host, string database) | ||
{ | ||
var options = new SpacetimeDB.ConnectOptions | ||
{ | ||
//v1.bin.spacetimedb | ||
//v1.text.spacetimedb | ||
Protocol = "v1.bin.spacetimedb", | ||
}; | ||
webSocket = new SpacetimeDB.WebSocket(new SpacetimeDB.UnityDebugLogger(), options); | ||
|
||
var token = PlayerPrefs.HasKey(GetTokenKey()) ? PlayerPrefs.GetString(GetTokenKey()) : null; | ||
webSocket.OnConnect += () => | ||
{ | ||
Debug.Log("Connected"); | ||
isConnected = true; | ||
}; | ||
|
||
webSocket.OnConnectError += (code, message) => | ||
{ | ||
Debug.Log($"Connection error {message}"); | ||
}; | ||
|
||
webSocket.OnClose += (code, error) => | ||
{ | ||
Debug.Log($"Websocket closed"); | ||
isConnected = false; | ||
}; | ||
|
||
webSocket.OnMessage += OnMessageReceived; | ||
|
||
if (!host.StartsWith("http://") && !host.StartsWith("https://") && !host.StartsWith("ws://") && | ||
!host.StartsWith("wss://")) | ||
{ | ||
host = $"ws://{host}"; | ||
} | ||
|
||
webSocket.Connect(token, host, database, Address.Random()); | ||
} | ||
|
||
private void OnMessageReceived(byte[] bytes) | ||
{ | ||
var message = ClientApi.Message.Parser.ParseFrom(bytes); | ||
if(message.TypeCase == ClientApi.Message.TypeOneofCase.TransactionUpdate) | ||
{ | ||
var reducer = message.TransactionUpdate.Event.FunctionCall.Reducer; | ||
var status = message.TransactionUpdate.Event.Status; | ||
onTransactionComplete?.Invoke(reducer, status); | ||
} | ||
} | ||
|
||
public async void CallReducer(string reducer, params object[] args) | ||
{ | ||
if(!isConnected) | ||
{ | ||
Debug.Log("Not connected"); | ||
} | ||
|
||
var _message = new SpacetimeDBClient.ReducerCallRequest | ||
{ | ||
fn = reducer, | ||
args = args, | ||
}; | ||
Newtonsoft.Json.JsonSerializerSettings _settings = new Newtonsoft.Json.JsonSerializerSettings | ||
{ | ||
Converters = { new SpacetimeDB.SomeWrapperConverter(), new SpacetimeDB.EnumWrapperConverter() }, | ||
ContractResolver = new SpacetimeDB.JsonContractResolver(), | ||
}; | ||
var json = Newtonsoft.Json.JsonConvert.SerializeObject(_message, _settings); | ||
webSocket.Send(Encoding.ASCII.GetBytes("{ \"call\": " + json + " }")); | ||
} | ||
|
||
public void Update() | ||
{ | ||
webSocket.Update(); | ||
} | ||
|
||
public void Close() | ||
{ | ||
if (webSocket != null) | ||
{ | ||
webSocket.Close(); | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.