Error on disconnection!

Hi,

When I m stopping nakama server from cmd. This error show up in unity logs:

System.IO.EndOfStreamException: Unexpected end of stream encountered whilst attempting to read 2 bytes
  at Nakama.Ninja.WebSockets.Internal.BinaryReaderWriter+<ReadExactly>d__0.MoveNext () [0x00118] in <ff5e1f0106cf42e4af6615b3989ce4dc>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <9577ac7a62ef43179789031239ba8798>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <9577ac7a62ef43179789031239ba8798>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <9577ac7a62ef43179789031239ba8798>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <9577ac7a62ef43179789031239ba8798>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00000] in <9577ac7a62ef43179789031239ba8798>:0 
  at Nakama.Ninja.WebSockets.Internal.WebSocketFrameReader+<ReadAsync>d__0.MoveNext () [0x0009b] in <ff5e1f0106cf42e4af6615b3989ce4dc>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <9577ac7a62ef43179789031239ba8798>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <9577ac7a62ef43179789031239ba8798>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <9577ac7a62ef43179789031239ba8798>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <9577ac7a62ef43179789031239ba8798>:0 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <9577ac7a62ef43179789031239ba8798>:0 
  at Nakama.Ninja.WebSockets.Internal.WebSocketImplementation+<ReceiveAsync>d__33.MoveNext () [0x000e7] in <ff5e1f0106cf42e4af6615b3989ce4dc>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <9577ac7a62ef43179789031239ba8798>:0 
  at Nakama.Ninja.WebSockets.Internal.WebSocketImplementation+<ReceiveAsync>d__33.MoveNext () [0x0034a] in <ff5e1f0106cf42e4af6615b3989ce4dc>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <9577ac7a62ef43179789031239ba8798>:0 
  at Nakama.Ninja.WebSockets.Internal.WebSocketImplementation+<ReceiveAsync>d__33.MoveNext () [0x00841] in <ff5e1f0106cf42e4af6615b3989ce4dc>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <9577ac7a62ef43179789031239ba8798>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <9577ac7a62ef43179789031239ba8798>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <9577ac7a62ef43179789031239ba8798>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <9577ac7a62ef43179789031239ba8798>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in <9577ac7a62ef43179789031239ba8798>:0 
  at Nakama.WebSocketAdapter+<ReceiveLoop>d__35.MoveNext () [0x0009f] in <ff5e1f0106cf42e4af6615b3989ce4dc>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <9577ac7a62ef43179789031239ba8798>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <9577ac7a62ef43179789031239ba8798>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <9577ac7a62ef43179789031239ba8798>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <9577ac7a62ef43179789031239ba8798>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00000] in <9577ac7a62ef43179789031239ba8798>:0 
  at Nakama.WebSocketAdapter+<Connect>d__31.MoveNext () [0x001a5] in <ff5e1f0106cf42e4af6615b3989ce4dc>:0 
UnityEngine.Debug:LogError(Object)
Nakama.Socket:<.ctor>b__51_2(Exception)
Nakama.<Connect>d__31:MoveNext()
UnityEngine.UnitySynchronizationContext:ExecuteTasks()

@faisal Please provide more information for the community to be able to help:

  1. What version of the Unity client do you use?
  2. What does your specific socket management code using the client SDK look like?
  3. What socket event handlers have you registered to receive errors from?
  1. Unity SDK v2.6.0

  2.      public ISocket Socket
         {
             get
             {
                 if (_socket == null)
                 {
                     if(_client != null)
                     {
                         _socket = _client.NewSocket();
                         _socket.Connected += OnConnect;
                         _socket.Closed += OnDisconnect;
                     }
                 }
                 return _socket;
             }
         }
         
         private async Task<bool> ConnectSocketAsync()
         {
             try
             {
                 if (_socket != null)
                 {
                     await DisconnectAsync();
                 }
             }
             catch (Exception e)
             {
                 Debugger.LogError(name, "Couldn't disconnect the socket: " + e);
             }
    
             try
             {
                 await Socket.ConnectAsync(Session);
                 Debugger.LogError(name, "Socket connected successfully");
                 return true;
             }
             catch (Exception e)
             {
                 Debugger.LogError(name, "An error has occured while connecting socket: " + e);
                 return false;
             }
         }
    
        public async Task DisconnectAsync()
         {
             if (Socket.IsConnected)
             {
                 await Socket.CloseAsync();
             }
         }
    
    1. _socket.Connected += OnConnect;
       _socket.Closed += OnDisconnect;

@novabyte I have closed unity play mode than stopped nakama server and this error still showing in unity logs, socket disconnect handler also called.

@faisal If you’re running the game with Play Mode in Unity editor you should be sure to use the lifecycle function to stop the socket otherwise the way that Unity hot reloads C# code can leave the socket open because it executes on its own thread. Have a look at the example snippets for what to do:

Alright, but I m still seeing that “Unexpected end of stream encountered” issue posted above.