Fix Error WebSocketException When Disconnecting Unity Editor from Server

Hi, I have been testing the connection between my Unity Editor and Nakama Server running locally. However I got this error after I disconnecting socket:

System.Net.WebSockets.WebSocketException (0x80004005): The remote party closed the WebSocket connection without completing the close handshake. ---> System.IO.IOException: Unable to read data from the transport connection: Operation aborted. ---> System.Net.Sockets.SocketException: Operation aborted
   --- End of inner exception stack trace ---

This is my Disconnect Function:

public async void Disconnect()
{
    if (_socket != null)
    {
        if (_socket.IsConnected)
            await _socket.CloseAsync();

        _socket.Connected -= ListenOnSocketConnected;
        _socket.Closed -= ListenOnSocketClosed;

        _socket = null;
    }

    _session = null;
    _client = null;
    _status = NakamaConnectionStatus.Disconnect;
}

The error happens while in game, this is what I did:

1. Connect
2. Disconnect
3. Connect
4. Disconnect -> WebSocketException

It seems fine for runtime but I need to know how to fix this or just letting it be and ignore the error.

  • I’m using Linux - Fedora 43, Nakama Unity SDK version 3.19.0
  • Nakama Server running in local using Docker Compose, version Nakama 3.34.1

Any answer and solution for this issue? I can provide more from my code that I made and currently still working on my game.

Thank You!

Hi @JoenTNT,

This is just a mishandling of the socket closure handshake, leading to the exception. A fix is already in place in the latest Nakama versions, but I think the related fix in the client hasn’t been released yet.

You can ignore these exceptions when closing the socket.

Best

Aight, Much Obliged! Even thought the client connection logic flow has no problem and runs normally, I will ignore this error until next release.

I have updated to Unity SDK 3.21.0, the error is now gone and it runs with no issue.