System.NullReferenceException: Object reference not set to an instance to an object

Oct 9, 2011 at 6:18 PM

Hello,

i tried it a long time with different versions of SuperWebSocket-Versions but i've no success...i always get the system.NullReferenceException.

This is my code:

                tms.webSocket.WebSocket ws = new webSocket.WebSocket();
                ws.InitWebSocketServer(444, "Any", 100, "SuperWebSocket");  //Parameters are: port, ip, maxConnections, socketName
                ws.StartServer();  //<------Here comes the exception

 

This is the code of InitWebSocketServer:

        public void InitWebSocketServer(int port, string ip, int maxConnections, string socketName)
        {
            var socketServer = new WebSocketServer();
            socketServer.Setup(new RootConfig(),
                new ServerConfig
                {
                    Name = socketName,
                    Ip = ip,
                    Port = port,
                    MaxConnectionNumber = maxConnections,
                    Mode = SocketMode.Async
                }, SocketServerFactory.Instance);

            socketServer.NewMessageReceived += new SessionEventHandler<WebSocketSession, string>(m_WebSocketServer_NewMessageReceived);
            socketServer.NewSessionConnected += new SessionEventHandler<WebSocketSession>(m_WebSocketServer_NewSessionConnected);
            socketServer.SessionClosed += new SessionEventHandler<WebSocketSession, SuperSocket.SocketBase.CloseReason>(m_WebSocketServer_SessionClosed);
        }

 

How do i use the superWEbSocket correctly? What i'm doing wrong?

thanks

Oct 10, 2011 at 3:31 AM

Could you give me more information(like stacktrace) about the exception?

 

I may get the reason?

 

The variable socketServer in method InitWebSocketServer is local one, and you don't save it in class scope.

So there is no way to access the created socket server in method StartServer(). Could you show me the code of StartServer()?

Oct 10, 2011 at 9:33 AM

Hey,

i changed my code in webSocket.cs (class webSocket) to this:

        public void InitWebSocketServer(int port, string ip, int maxConnections, string socketName)
        {
            m_WebSocketServer = new WebSocketServer();
            m_WebSocketServer.Setup(new RootConfig(),
                new ServerConfig
                {
                    Name = socketName,
                    Ip = ip,
                    Port = port,
                    MaxConnectionNumber = maxConnections,
                    Mode = SocketMode.Async
                }, SocketServerFactory.Instance);

            m_WebSocketServer.NewMessageReceived += new SessionEventHandler<WebSocketSession, string>(m_WebSocketServer_NewMessageReceived);
            m_WebSocketServer.NewSessionConnected += new SessionEventHandler<WebSocketSession>(m_WebSocketServer_NewSessionConnected);
            m_WebSocketServer.SessionClosed += new SessionEventHandler<WebSocketSession, SuperSocket.SocketBase.CloseReason>(m_WebSocketServer_SessionClosed);
        }

In the class WebSocket, i've:

protected WebSocketServer m_WebSocketServer;

and

StartServer() is:

        [SetUp]
        public void StartServer()
        {
            m_WebSocketServer.Start();
        }

 

In my program.cs, i'm using:

                tms.tmCore.webSocket.WebSocket ws = new webSocket.WebSocket();
                ws.InitWebSocketServer(444, "Any", 100, "tmCore SuperWebSocket");
                ws.StartServer();  //<---Exception comes here

this is my stacktrace.

   bei SuperSocket.SocketBase.AppServerBase`2.Start()
   bei SuperSocket.SocketBase.AppServer`2.Start()
   bei tms.tmCore.webSocket.WebSocket.StartServer() in Z:\webSocket\webSocket.cs:Zeile 124.
   bei tms.tmCore.Program.MainThread() in Z:\Program.cs:Zeile 258.

 

Oct 10, 2011 at 9:37 AM
Why do you mark StartServer() as [SetUp]?
If you add it and run the test case in Nunit, the method StartServer() will be executed before any test cases.


From: rusgard
Sent: Monday, October 10, 2011 4:33 PM
To: [email removed]
Subject: Re: System.NullReferenceException: Object reference not set to an instance to an object [SuperWebSocket:275260]

From: rusgard

Hey,

i changed my code in webSocket.cs (class webSocket) to this:

public void InitWebSocketServer(int port, string ip, int maxConnections, string socketName)
{
m_WebSocketServer = new WebSocketServer();
m_WebSocketServer.Setup(new RootConfig(),
new ServerConfig
{
Name = socketName,
Ip = ip,
Port = port,
MaxConnectionNumber = maxConnections,
Mode = SocketMode.Async
}, SocketServerFactory.Instance);

m_WebSocketServer.NewMessageReceived += new SessionEventHandler<WebSocketSession, string>(m_WebSocketServer_NewMessageReceived);
m_WebSocketServer.NewSessionConnected += new SessionEventHandler<WebSocketSession>(m_WebSocketServer_NewSessionConnected);
m_WebSocketServer.SessionClosed += new SessionEventHandler<WebSocketSession, SuperSocket.SocketBase.CloseReason>(m_WebSocketServer_SessionClosed);
}

In the class WebSocket, i've:

protected WebSocketServer m_WebSocketServer;

and

StartServer() is:

[SetUp]
public void StartServer()
{
m_WebSocketServer.Start();
}

In my program.cs, i'm using:

tms.tmCore.webSocket.WebSocket ws = new webSocket.WebSocket();
ws.InitWebSocketServer(444, "Any", 100, "tmCore SuperWebSocket");
ws.StartServer(); //<---Exception comes here

this is my stacktrace.

bei SuperSocket.SocketBase.AppServerBase`2.Start()
bei SuperSocket.SocketBase.AppServer`2.Start()
bei tms.tmCore.webSocket.WebSocket.StartServer() in Z:\webSocket\webSocket.cs:Zeile 124.
bei tms.tmCore.Program.MainThread() in Z:\Program.cs:Zeile 258.

Oct 10, 2011 at 10:12 AM
kerryjiang wrote:
Why do you mark StartServer() as [SetUp]?
If you add it and run the test case in Nunit, the method StartServer() will be executed before any test cases.

Thanks for you hint. I removed the [SetUp] and similar code-line, but it is still not running.

Same problem as before. Any other ideas?

Oct 10, 2011 at 10:15 AM

How did you run this application? In Nunit?

And then compare the code in SuperWebSocketWeb in Samples folder.

 

The server in SuperWebSocketWeb can run correctly, you can follow it to implement your service.