Hi there. Long time listener, first time caller. I’m Mike Schweitzer, a generalist engineer in the game industry.
I made an open source WebSocket client for Unity that works on web and “non-web” platforms without any additional dll downloads or installs.
This is a runtime client only (MonoBehaviour), so not for use in Editor code.
https://github.com/mikerochip/unity-websocket
Details are on the github page, but here is a copy of the features:
-
Easy to use
-
Doesn’t force you to use async/await: use event listeners, coroutines, or polling
-
Doesn’t force you to use #if for WebGL: no platform-specific code required to use it
-
WebSocketConnection is just a MonoBehaviour
-
Public API prevents you from corrupting an active connection
-
Reusable: connect, disconnect, change URL, connect again, etc
-
Flexible config
-
URL is the only required config
-
Sane defaults
-
Optionally set subprotocols, max send, and max receive bytes
-
Wide platform support
-
No external install requirements or dependencies
-
string is treated as text and byte[ ] as binary (some servers care)
-
Customizable ping-pong support for servers that enforce idle timeouts
-
Web uses a JavaScript lib WebSocket.jslib, and includes support for WebAssembly.Table from Unity 2023.2+
-
Other platforms use the built-in System.Net.WebSockets
This is why I did it, maybe you’ve had a similar story and would consider using this package for similar reasons:
-
In the earlier Unity days, I manually downloaded and vendored WebSocket4Net from NuGet
-
Never feels great to manually download things from NuGet
-
The NuGetForUnity package has several issues that make me not a fan of using it in modern Unity projects
-
More recently tried using NativeWebSocket, but encountered DX issues (developer experience)
-
Requires async/await
-
Requires conditional compilation for Web
-
After wanting to make my own fixes / modifications, I found the source difficult to navigate
-
I put a lot of value on ease-of-use and ergonomics when building workflows and tools
No shade to NativeWebSocket, my work is based on that. Very grateful for that. I just wanted to fix the issues and the fixes I wanted to make were way too extensive to open a PR that might not get merged.
Thanks!