Skip to content

Commit

Permalink
fix: WebSocketTransport Re-use and Compatibility (#188)
Browse files Browse the repository at this point in the history
* fix:Unity WebGL throws Error: "ReferenceError: Runtime is not defined"

It seams that in unity 2021.2 variable Runtime doesn't exist and can be replaced with Module['dynCall_*'].
In webSocket.jslib change all Runtime.dynCall('*1', *2, [*3, *4]) for Module['dynCall_*1'](*2, *3, *4)

* Updated web socket transport to not use static fields

* Reset started value on disconnect

* Fixed the flag for is started on shutdown

* Reset web socket client on shutdown

* Added catch statement to close socket

* Changed close on already closed to end quietly

* Fixed some errors with web socket client

* Fixed small syntax error
  • Loading branch information
nicholas-maltbie authored Jan 4, 2023
1 parent f8944ff commit c7246c7
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var LibraryWebSocket = {
}

if (state.onOpen) {
Runtime.dynCall('v', state.onOpen, []);
Module['dynCall_v'](state.onOpen);
}
};

Expand All @@ -59,7 +59,7 @@ var LibraryWebSocket = {
HEAPU8.set(dataBuffer, buffer);

try {
Runtime.dynCall('vii', state.onMessage, [buffer, dataBuffer.length]);
Module['dynCall_vii'](state.onMessage, buffer, dataBuffer.length);
} finally {
_free(buffer);
}
Expand All @@ -78,7 +78,7 @@ var LibraryWebSocket = {
stringToUTF8(msg, msgBuffer, msgBytes);

try {
Runtime.dynCall('vi', state.onError, [msgBuffer]);
Module['dynCall_vi'](state.onError, msgBuffer)
} finally {
_free(msgBuffer);
}
Expand All @@ -91,7 +91,7 @@ var LibraryWebSocket = {
}

if (state.onClose) {
Runtime.dynCall('vi', state.onClose, [ev.code]);
Module['dynCall_vi'](state.onClose, ev.code)
}
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void Close(CloseStatusCode code = CloseStatusCode.Normal, string reason =

if (ReadyState == WebSocketSharp.WebSocketState.Closed)
{
throw new InvalidOperationException("Socket is already closed");
return;
}

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace Netcode.Transports.WebSocket
{
public class WebSocketClientFactory
{
#if UNITY_WEBGL
public static JSWebSocketClient Client = new JSWebSocketClient();
#if (UNITY_WEBGL && !UNITY_EDITOR)
public static JSWebSocketClient Client;

internal delegate void OnOpenCallback();
internal delegate void OnMessageCallback(IntPtr messagePointer, int messageSize);
Expand Down Expand Up @@ -67,7 +67,8 @@ internal static void OnCloseEvent(int disconnectCode)

public static IWebSocketClient Create(string url)
{
#if UNITY_WEBGL
#if (UNITY_WEBGL && !UNITY_EDITOR)
Client = new JSWebSocketClient();
_SetUrl(url);
_SetOnOpen(OnOpenEvent);
_SetOnMessage(OnMessageEvent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace Netcode.Transports.WebSocket
{
public class WebSocketTransport : NetworkTransport
{
private static WebSocketServer WebSocketServer = null;
private static IWebSocketClient WebSocketClient = null;
private static bool IsStarted = false;
private WebSocketServer WebSocketServer = null;
private IWebSocketClient WebSocketClient = null;
private bool IsStarted = false;

[Header("Transport")]
public string ConnectAddress = "127.0.0.1";
Expand Down Expand Up @@ -101,6 +101,7 @@ public override void Shutdown()
{
WebSocketServer.Stop();
}
IsStarted = false;
}

public override bool StartClient()
Expand Down

0 comments on commit c7246c7

Please sign in to comment.