Skip to content

Commit 6b883c9

Browse files
committed
update README
1 parent 77e3da9 commit 6b883c9

1 file changed

Lines changed: 85 additions & 1 deletion

File tree

README.md

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Used in [Colyseus Unity SDK](https://github.com/colyseus/colyseus-unity-sdk).
2727
3. Click Add Package From Git URL
2828
4. Enter URL: `https://github.com/endel/NativeWebSocket.git#upm-2.0`
2929

30-
If you need the old 1.x package instead, use `https://github.com/endel/NativeWebSocket.git#upm-1.x` in UPM, or check out the repository sources from the `1.x` branch.
30+
If you need the previous 1.x package instead, use `https://github.com/endel/NativeWebSocket.git#upm-1.x` in UPM, or check out the repository sources from the [`1.x` branch](https://github.com/endel/NativeWebSocket/tree/1.x).
3131

3232
**Via .unitypackage:**
3333
1. Download `NativeWebSocket.unitypackage` from the [Releases](https://github.com/endel/NativeWebSocket/releases) page
@@ -265,6 +265,90 @@ new WebSocket(string url, List<string> subprotocols)
265265

266266
---
267267

268+
# Migrating from 1.x
269+
270+
## Breaking changes
271+
272+
### Universal .NET library
273+
274+
The core library no longer depends on `UnityEngine`. It targets `netstandard2.0` and `net6.0`, and works across Unity, MonoGame, Godot, and any .NET project. Unity-specific code (WebGL) has been moved to separate integration files.
275+
276+
### `MainThreadUtil`, `WaitForUpdate`, and `WaitForBackgroundThread` removed
277+
278+
These Unity-specific classes have been removed. Event dispatching is now handled automatically via `SynchronizationContext`. Remove any references to these classes from your code.
279+
280+
```csharp
281+
// 1.x — these no longer exist in 2.x
282+
MainThreadUtil.Instance
283+
MainThreadUtil.synchronizationContext
284+
new WaitForUpdate()
285+
new WaitForBackgroundThread()
286+
```
287+
288+
### Automatic event dispatching — no more `Update()` dispatch loop
289+
290+
In 1.x, you had to call `DispatchMessageQueue()` every frame from `Update()`:
291+
292+
```csharp
293+
// 1.x — REQUIRED in Update()
294+
void Update() {
295+
websocket.DispatchMessageQueue();
296+
}
297+
```
298+
299+
In 2.x, events are automatically dispatched to the main thread via `SynchronizationContext` in Unity, Godot, and MonoGame (with `WebSocketGameComponent`). **Remove the `Update()` dispatch call.** `DispatchMessageQueue()` is only needed in environments without a `SynchronizationContext` (e.g. console apps).
300+
301+
### `Close()` accepts close code and reason
302+
303+
```csharp
304+
// 1.x — no parameters
305+
await websocket.Close();
306+
307+
// 2.x — optional close code and reason
308+
await websocket.Close(WebSocketCloseCode code = WebSocketCloseCode.Normal, string reason = null);
309+
```
310+
311+
Existing `Close()` calls without arguments still compile. However, if you implemented the `IWebSocket` interface directly, you must update your implementation to match the new signature.
312+
313+
### `IWebSocket` interface expanded
314+
315+
The interface now declares methods in addition to events and state:
316+
317+
```csharp
318+
// 2.x interface
319+
public interface IWebSocket {
320+
event WebSocketOpenEventHandler OnOpen;
321+
event WebSocketMessageEventHandler OnMessage;
322+
event WebSocketErrorEventHandler OnError;
323+
event WebSocketCloseEventHandler OnClose;
324+
325+
WebSocketState State { get; }
326+
327+
// New in 2.x
328+
Task Connect();
329+
Task Close(WebSocketCloseCode code = WebSocketCloseCode.Normal, string reason = null);
330+
Task Send(byte[] data);
331+
Task SendText(string message);
332+
}
333+
```
334+
335+
Any custom `IWebSocket` implementation must now include these methods.
336+
337+
### Source files cannot be copied directly
338+
339+
In 1.x, you could copy `NativeWebSocket/Assets/WebSocket/WebSocket.cs` into your Unity project. In 2.x, the core source lives in `src/NativeWebSocket/` and requires a build-time transformation to add WebGL conditional compilation guards. Use UPM or the `.unitypackage` instead of copying raw files.
340+
341+
## Migration checklist
342+
343+
| What changed | Action required |
344+
|---|---|
345+
| `MainThreadUtil` / `WaitForUpdate` / `WaitForBackgroundThread` removed | Delete any code using these classes |
346+
| Automatic event dispatching | Remove `DispatchMessageQueue()` from `Update()` (Unity/Godot/MonoGame) |
347+
| Custom `IWebSocket` implementations | Add `Connect()`, `Close()`, `Send()`, `SendText()` methods |
348+
| Manual file copy installs | Switch to UPM or `.unitypackage` |
349+
350+
---
351+
268352
## Acknowledgements
269353

270354
Big thanks to [Jiri Hybek](https://github.com/jirihybek/unity-websocket-webgl).

0 commit comments

Comments
 (0)