Registering to WebSocketServer events cause commands to not be executed ?

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


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 2:30 AM
Edited Oct 17, 2012 at 2: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.

Oct 17, 2012 at 2: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 3:28 AM
Edited Oct 17, 2012 at 3: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 ?


Oct 17, 2012 at 3:32 AM

How do you do it?

Oct 17, 2012 at 3:40 AM
Edited Oct 17, 2012 at 4: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();
    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)));

    // 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)));

    // 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 : 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