Add websockets

This commit is contained in:
2026-01-22 20:41:28 -07:00
parent 21c59bf669
commit e422d30f2c
21 changed files with 1125 additions and 355 deletions

View File

@@ -1,8 +1,10 @@
public class Endpoints
{
private static readonly string HOST = "games.lncvrt.xyz";
private static readonly int WS_PORT = 3342;
public static readonly string BASE_URL = $"https://{HOST}/api";
public static readonly string BERRYDASH_ENDPOINT = BASE_URL + "/berrydash";
public static readonly string WS_ENDPOINT = $"ws://{HOST}:{WS_PORT}/api/ws";
public static readonly string LATEST_VERSION_ENDPOINT = BERRYDASH_ENDPOINT + "/latest-version";
public static readonly string LEADERBOARDS_ENDPOINT = BERRYDASH_ENDPOINT + "/leaderboards";
public static readonly string LEADERBOARDS_SCORE_ENDPOINT = LEADERBOARDS_ENDPOINT + "/score";

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 651269be0f6324e8aa495c48fc501516
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,9 @@
using System;
[Serializable]
public class ConnectedWithCurrentSceneMessage
{
public string type = "connected_with_current_scene";
public string sceneName;
public long timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: bc9d79fcdc1a24dcd988203a8fa66d84

View File

@@ -0,0 +1,9 @@
using System;
[Serializable]
public class RequestInfoMessage
{
public string type = "info_request";
public string kind;
public long timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: c41428221da68496c8489cc51d5a32b4

View File

@@ -0,0 +1,10 @@
using System;
[Serializable]
public class SceneChangeMessage
{
public string type = "scene_change";
public string to;
public string from;
public long timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 3facbfc80cf794283a44832185785aca

View File

@@ -0,0 +1,124 @@
using MikeSchweitzer.WebSocket;
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class WebsocketHandler : MonoBehaviour
{
public static WebsocketHandler Instance;
public WebSocketConnection _Connection;
private bool isReconnecting = false;
private bool intentionalDisconnect = false;
private static readonly WaitForSeconds _waitForSeconds2 = new(2f);
public string lastSceneName = null;
private void Awake()
{
if (Instance != null)
{
Destroy(gameObject);
return;
}
Instance = this;
DontDestroyOnLoad(gameObject);
_Connection = gameObject.AddComponent<WebSocketConnection>();
_Connection.StateChanged += OnStateChanged;
_Connection.ErrorMessageReceived += OnErrorMessageReceived;
SceneManager.sceneLoaded += OnSceneLoaded;
Connect();
}
private void OnDestroy()
{
if (_Connection != null)
{
_Connection.StateChanged -= OnStateChanged;
_Connection.ErrorMessageReceived -= OnErrorMessageReceived;
}
}
public void Connect()
{
intentionalDisconnect = false;
if (_Connection.State == WebSocketState.Connected || _Connection.State == WebSocketState.Connecting) return;
_Connection.Connect(Endpoints.WS_ENDPOINT);
}
public void Disconnect()
{
intentionalDisconnect = true;
_Connection.Disconnect();
}
private void OnStateChanged(WebSocketConnection connection, WebSocketState oldState, WebSocketState newState)
{
Debug.Log($"WS state changed from \"{oldState}\" to \"{newState}\"");
if (newState == WebSocketState.Disconnected && !intentionalDisconnect && !isReconnecting)
{
StartCoroutine(ReconnectWithDelay());
}
if (newState == WebSocketState.Connected)
{
var currentSceneName = SceneManager.GetActiveScene().name;
ConnectedWithCurrentSceneMessage msg = new()
{
sceneName = currentSceneName
};
_Connection.AddOutgoingMessage(JsonUtility.ToJson(msg));
if (currentSceneName == "ChatroomMenu")
{
RequestInfoMessage msg2 = new()
{
kind = "chatroom_messages"
};
_Connection.AddOutgoingMessage(JsonUtility.ToJson(msg2));
}
}
}
private void OnErrorMessageReceived(WebSocketConnection connection, string errorMessage)
{
Debug.LogError($"WS error {errorMessage}");
}
private IEnumerator ReconnectWithDelay()
{
isReconnecting = true;
yield return _waitForSeconds2;
isReconnecting = false;
Debug.Log("Trying to connect again");
Connect();
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
if (_Connection.State == WebSocketState.Connected)
{
SceneChangeMessage msg = new()
{
to = scene.name,
from = lastSceneName
};
_Connection.AddOutgoingMessage(JsonUtility.ToJson(msg));
if (scene.name == "ChatroomMenu")
{
RequestInfoMessage msg2 = new()
{
kind = "chatroom_messages"
};
_Connection.AddOutgoingMessage(JsonUtility.ToJson(msg2));
}
}
lastSceneName = scene.name;
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 25ed18bf58d6948d095504532c27683b