This project is read-only.

NullReference when the server goes down and comes back up.

Dec 3, 2013 at 8:53 PM
I'm not sure if this is a problem with my C# or the library.

I try to connect to the server, and if it's not up, or can't connect, I'll keep retrying.

After I connect, if the server goes down, then comes back up, I'll get this error:
Unhandled Exception: System.NullReferenceException: Object reference not set to
an instance of an object.
   at SocketIOClient.Client.OnMessageEvent(IMessage msg)
   at SocketIOClient.Client.wsClient_OpenEvent(Object sender, EventArgs e)
   at WebSocket4Net.WebSocket.OnHandshaked()
   at WebSocket4Net.Command.Handshake.ExecuteCommand(WebSocket session, WebSocke
tCommandInfo commandInfo)
   at WebSocket4Net.WebSocket.ExecuteCommand(WebSocketCommandInfo commandInfo)
   at WebSocket4Net.WebSocket.OnDataReceived(Byte[] data, Int32 offset, Int32 le
ngth)
   at WebSocket4Net.WebSocket.client_DataReceived(Object sender, DataEventArgs e
)
   at SuperSocket.ClientEngine.ClientSession.OnDataReceived(Byte[] data, Int32 o
ffset, Int32 length)
   at SuperSocket.ClientEngine.AsyncTcpSession.ProcessReceive(SocketAsyncEventAr
gs e)
   at SuperSocket.ClientEngine.AsyncTcpSession.SocketEventArgsCompleted(Object s
ender, SocketAsyncEventArgs e)
   at System.Net.Sockets.SocketAsyncEventArgs.OnCompleted(SocketAsyncEventArgs e
)
   at System.Net.Sockets.SocketAsyncEventArgs.ExecutionCallback(Object ignored)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionCo
ntext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, C
ontextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, C
ontextCallback callback, Object state)
   at System.Net.Sockets.SocketAsyncEventArgs.FinishOperationSuccess(SocketError
 socketError, Int32 bytesTransferred, SocketFlags flags)
   at System.Net.Sockets.SocketAsyncEventArgs.CompletionPortCallback(UInt32 erro
rCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
   at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32
errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
Here's the class that handles the socket stuff
 class Connector
    {
       
        public Connector()
        {
            Program.socket = new Client("http://ip:3000");
            Program.socket.Opened += SocketOpened;
            Program.socket.Message += SocketMessage;
            Program.socket.SocketConnectionClosed += SocketConnectionClosed;
            Program.socket.Error += SocketError;

            Program.socket.On("connect", (fn) =>
            {
                Program.toLog.Add("connected to server");
            });

            Program.socket.Connect();
        }

        public void tryToConnect()
        {
            while (true)
            {
                while (!Program.socket.IsConnected)
                {
                    System.Threading.Thread.Sleep(5000);
                    Program.socket.Connect();
                }
            }
        }


        private void SocketError(object sender, SocketIOClient.ErrorEventArgs e)
        {
            Program.toLog.Add("error connecting to node server");
        }

        private void SocketConnectionClosed(object sender, EventArgs e)
        {
            Program.toLog.Add("connection closed");
        }

        private  void SocketMessage(object sender, MessageEventArgs e)
        {
            //
        }

        private  void SocketOpened(object sender, EventArgs e)
        {
            //
        }
  
    }
The "tryToConnect" method is on it's own thread.
Coordinator
Dec 4, 2013 at 3:07 AM
I'll take a look and let you know.

What flavor of SocketIO4Net are you using, the latest version from NuGet or from source/zip from codeplex?
Coordinator
Dec 4, 2013 at 1:17 PM
I think the issue your having is with your tryConnect routine. Essentially you are fighting with with SocketIO4Net's own retry logic.

You can watch the ConnectionRetryAttempt event if you take the server down once connected to see the retry actions.
Marked as answer by JimHouseman on 12/4/2013 at 8:00 AM
Dec 4, 2013 at 4:00 PM
I'm using the NuGet version.

I didn't know there was inherent retry logic inside SocketIO4Net. I'll use that instead, thanks.