Skip to content
WebSocketClient for Unity.(based on .NET Standard2.0)
C# ShaderLab HLSL
Branch: master
Clone or download
Latest commit 22775c6 Nov 8, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
Sample add: server Oct 30, 2019
Unity/UniWebSocket fix: git colliding Nov 7, 2019
.gitignore add: .gitignore Oct 30, 2019
LICENSE Create LICENSE Oct 30, 2019 add: readme Oct 31, 2019


UniWebSocket is a WebSocket client for Unity. Since Unity 2018 supports .NET Standard 2.0, ClientWebSocket can be used. Therefore, WebSocket protocol can be used without an extra library. However, ClientWebSocket is very cumbersome. UniWebSocket is a wrapper of ClientWebSocket for easy handling. UniWebSocket was created with reference to the websocket-client (Released under the MIT License).


How to use

var webSocketClient = new WebSocketClient(new Uri("wss://~~~"));

//binary receive
    .Where(x => x.MessageType == WebSocketMessageType.Binary)
    .Select(x => x.Binary)
    .Subscribe(x => DoSometine(x));
    .Subscribe(x => DoSomething(x));
//text receive
    .Subscribe(x => DoSomething(x));

    .Subscribe(x => DoSomething(x));

    .Subscribe(x => DoSomething(x));

//start connect and start listening in background thread
//Task.CompletedTask will return when it starts to be received.
//It will not be blocked while receiving.
await webSocketClient.ConnectAndStartListening();

//send bin
byte[] array = MakeSomeArray();

//send text
//The Send function guarantees the transmission order using queue.
//await for pushed to the queue.
//It does not wait for the transmission to complete.
await webSocketClient.Send("string or byte[]");

//The SendInstant function ignores the queue used inside the Send function and sends it immediately.
//await for transmission to complete.
await webSocketClient.SendInstant("string or byte[]");

//You can decide whether to dispose at the same time as Close with the last bool parameter.
await _webSocketClient.CloseAsync(WebSocketCloseStatus.NormalClosure, "description", true);

If you want to make detailed settings for WebSocket, use the factory method.

var factory = new Func<ClientWebSocket>(() => new ClientWebSocket
    Options =
        KeepAliveInterval = TimeSpan.FromSeconds(5),
        Proxy = ...
        ClientCertificates = ...

var webSocketClient = new WebSocketClient(url, factory);

The maximum size of messages to be received is set to 512KB by default. If you need more, set it with the following constructor.

public WebSocketClient(Uri url, int maxReceivedMessageSize, ILogger logger = null, Func<ClientWebSocket> clientFactory = null)


WebSocketClient issues all events from thread pool. Therefore, you cannot operate the components on Unity in Subscribe directly. So please handle from the main thread using an operator such as 'ObserveOnMainThread' as follows.

//error will occur.
    .Subscribe(x => unityObj.text = x);
//The following is correct.
    .Subscribe(x => unityObj.text = x);


I prepared a simple chat app as a sample. When the server starts, connect to ws://ip:port/ws.

Unity 2018

open this scene and run.

Server(C#/ ASP.NET Core3.0)

Requires .NET Core3.0. First, set your ip here. Then type the following command

$ cd Sample/Server/WebSocketChat/WebSocketChat/
$ dotnet run


open UniWebSocket/Sample/Web/WebSocketChat.html

You can’t perform that action at this time.