Skip to content

WebSocket client for Unity that also works with WebGL.

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
LICENSE.meta
Notifications You must be signed in to change notification settings

yoshd/UnityWebSocket

Repository files navigation

UnityWebSocket

UnityWebSocket is a client library that allows transparent use of WebSocket on both WebGL and other platforms.

Unity WebGL has the limitation that the browser cannot handle the socket API directly, so WebSocket must be used from Javascript via Emscripten. (See Documentation)

Requirements

  • Unity 2021.3 or later
  • It also assumes a dependency on UniTask .

Usage

You can add "com.yoshd.unitywebsocket": "https://github.com/yoshd/UnityWebSocket.git#v0.1.4" to your manifest.json .

Messages are always sent in binary type.

using UnityWebSocket;

async UniTask SampleAsync()
{
    var client = new WebSocketClient();
    client.ConnectAsync(new Uri("ws:https://localhost:8080/hello"), CancellationToken.None);
    var msg = System.Text.Encoding.UTF8.GetBytes("Hello!");
    await client.SendAsync(msg.AsMemory(), CancellationToken.None);
    var buf = new Memory<byte>(new byte[1024]);
    var r = await client.ReceiveAsync(buf, CancellationToken.None);
    await client.CloseAsync(WebSocketCloseStatus.NormalClosure, "close", CancellationToken.None);
    client.Dispose();
}

About

WebSocket client for Unity that also works with WebGL.

Resources

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
LICENSE.meta

Stars

Watchers

Forks

Packages

No packages published