Registering to WebSocketServer events cause commands to not be executed ?

Oct 17, 2012 at 12:25 AM
Edited Oct 17, 2012 at 12:45 AM

Hello,

I found that there was events in the WebSocketServer so i decided to listen to them this way.

wsGameServer.NewDataReceived += wsGameServer_NewDataReceived;           wsGameServer.NewMessageReceived += wsGameServer_NewMessageReceived;            wsGameServer.NewSessionConnected += wsGameServer_NewSessionConnected;            wsGameServer.SessionClosed += wsGameServer_SessionClosed;    

However, i found out that when i listen to thoses events specially the NewMessageReceived event, the commands arent executed. My questions are :

1- It is normal ?

2- Are the others events causing such behaviors ?

3- It is obligated to use commands or we can simply put our logic in the NewMessageReceived event ? The problem is i need to raise an event from a specific class and if i put the command in a different class i can't raise this specific event. Is there a way to inject a class in the session from where i started the server ?

On a side note i really like your WebSocketServe continue the good work ! :-) It work great !

Oct 17, 2012 at 1:30 AM
Edited Oct 17, 2012 at 1:49 AM

Nevermind. Ive looked in the source and yeah its normal. Anyway i found out that you have access to the appServer in the command session instance so i made my own class which inherited from WebSocketServer and added my stuff there so i can have access to them after a cast in the command. There i could embed my UI interaction delegates which is fantastic place to do so.

Coordinator
Oct 17, 2012 at 1:48 AM

Commands cannot work together with NewMessageReceived event handler. If you have define commands, you cannot register NewMessageReceived event handler any more.

Oct 17, 2012 at 2:28 AM
Edited Oct 17, 2012 at 2:28 AM

Yeah i figured out but anyway i prefer the commands than the events for the new messages, i think its a very nice way to do things... I found a way to pass my data directly in an underlying base class of WebSocketServer. Is it the best practice to do it ?

Thanks!

Coordinator
Oct 17, 2012 at 2:32 AM

How do you do it?

Oct 17, 2012 at 2:40 AM
Edited Oct 17, 2012 at 3:00 AM

I created a class named MyWebSocketServer which inherit from WebSocketServer like this :

public class MyWebSocketServer : WebSocketServer
{
    public delegate void OnNotificationDelegate(object sender, NotificationEventArgs e);
    public OnNotificationDelegate Notify { get; set; }

    public string ServerName { get; set; }
}

This is the class i instanciate when i create the server.

 

/// <summary>
/// Start the server.
/// </summary>
public override void Start()
{
    // Attach the notification event to this class so we can send notification to the UI console.
    this.OnNotificationEvent += new EventHandler<NotificationEventArgs>(this.OnNotification);

    // Instanciate the server.
    MyWebSocketServer wsGameServer = new MyWebSocketServer();
            
    // TEST OF THE CUSTOM PROPERTY SET TO MYWEBSOCKETSERVER.
    wsGameServer.ServerName = "This is my WebSocketServer name";
      
    // Configure the server.
    if (!wsGameServer.Setup(8880))
    {
        RaiseNotificationEvent(this, new NotificationEventArgs(NotificationType.Error, String.Format("The WebSocket server failed to setup.", 8880)));
        return;
    }

    // Starting the server.
        if (!wsGameServer.Start())
    {
        RaiseNotificationEvent(this, new NotificationEventArgs(NotificationType.Error, String.Format("The WebSocket server failed to listening on the port {0}.", 8880)));
        return;
    }

    // Set the current server instance to a property inside the class for access by the events.
    Instance = wsGameServer;

    // Register to some server events..
    wsGameServer.NewSessionConnected += wsGameServer_NewSessionConnected;
    wsGameServer.SessionClosed += wsGameServer_SessionClosed;

    // Passing the RaiseNotificationEvent method to the Notify delegate of the game server (hold by MyWebSocketServer)
    wsGameServer.Notify = RaiseNotificationEvent;

    // Send a notification to the console UI and change status bar text.
    RaiseNotificationEvent(this, new NotificationEventArgs(NotificationType.Notice, String.Format("WebSocket server started successfully and listening on port {0} ...", Port)));
    SetStatusText("Server is ONLINE.");
}

 

Then in the command i call my data using a cast. Like you see appServer.ServerName attribute is only part of the MyWebSocketServer class.

public override void ExecuteCommand(SuperWebSocket.WebSocketSession session, SubRequestInfo requestInfo)
{
    // Get instance of the server, since we want to access our custom data we do a cast.
    var appServer = (MyWebSocketServer) session.AppServer;

    appServer.Notify(this, new NotificationEventArgs(NotificationType.Warning, "HELLO FROM THE HELLO COMMAND"));

    session.Send("HEY! HELLO!" + appServer.ServerName);
}
This seem to work perfectly ! Anyway it seem to be what your doing here : http://supersocket.codeplex.com/wikipage?title=Access%20AppServer%20in%20AppSession%20and%20Commands the only difference is that you don't have to cast it anymore since you added it directly to the session. So to do what you say there i only inherit from the WebSocketSession and then using this session type in the commands class ? (SubCommandBase<CustomSession>)
Seriously your websocket server implementation rock ! I can't describe the feeling i have right now lol