yoshd / UnityWebSocket

WebSocket client for Unity that also works with WebGL.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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://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.

License:MIT License


Languages

Language:C# 80.9%Language:JavaScript 19.1%