sta / websocket-sharp

A C# implementation of the WebSocket protocol client and server
http://sta.github.io/websocket-sharp
MIT License
5.74k stars 1.66k forks source link

How to make WebSocketService TBehavior list or access WebSocketService TBehavior list? #721

Closed kjyoffice closed 3 months ago

kjyoffice commented 1 year ago

Hello. I am make websocket server used this library. And want access HelloWebSocketService class property.

How to aceess HelloWebSocketService class?

Please go down "// Situation." line.

Thanks.

public class Program 
{
    public static void Main(string[] args) 
    {
        var wss = new WebSocketSharp.Server.WebSocketServer(37000);
        wss.AddWebSocketService<HelloWebSocketService>("/HelloWebSocket");

        // Situation.
        //
        // - Start server
        //
        // few times...
        //
        // - Connect 5 client (ID1, ID2, ID3, ID4, ID5)
        //   and no close active status.
        //   (** ID1, ID3, ID5 is QueryString, xtext=hello)
        // 
        // and
        // please go down "else if (inText == "CMD1")" line 

        wss.Start();
        Console.Out.WriteLine("server start.");
        Console.Out.WriteLine(" - Command : Stop, Cmd1");

        var inText = Console.In.ReadLine().Trim().ToUpper();

        if (inText == "STOP")
        {
            wss.Stop();
            Console.Out.WriteLine("server stop.");
            Console.Out.WriteLine("bye");
        }
        else if (inText == "CMD1")
        {
            // Want
            // IsAllowCmd1 property is True, call SendStatusUpdateData method.

            WebSocketSharp.Server.WebSocketServiceHost wsHost = wss.WebSocketServices["/HelloWebSocket"];

            // Question
            // How to make HelloWebSocketService list or 
            // How to access HelloWebSocketService list?

            // like
            var hwssList = wsHost.CALL_MAGIC_PROPERTY_OR_METHOD;
            // or 
            var hwssList = wss.CALL_MAGIC_PROPERTY_OR_METHOD;

            hwssList.Where(x => (x.IsAllowCmd1 == true)).ToList().ForEach(x => x.SendStatusUpdateData("Hi there. :)"));            
        }
    }
}
public class HelloWebSocketService : WebSocketSharp.Server.WebSocketBehavior
{
    public string XText { get; set; }
    public bool IsAllowCmd1 
    {
        get 
        {
            return 
            (
                ((this.XText != string.Empty) && (this.XText.ToUpper().IndexOf("HELLO") >= 0)) && 
                (this.Sessions.ActiveIDs.Where(x => (x == this.ID)).Count() > 0)
            );
        }    
    }

    public void SendStatusUpdateData(string data)
    {
        this.Send(data);
    }

    protected override void OnOpen()
    {
        // get QueryString key/value
        this.XText = (this.QueryString["xtext"] ?? string.Empty).Trim().ToLower();

        // Echo Welcome
        // ...

        base.OnOpen();
    }

    protected override void OnMessage(WebSocketSharp.MessageEventArgs e)
    {
        // Echo
        // ...

        base.OnMessage(e);
    }

    protected override void OnError(WebSocketSharp.ErrorEventArgs e)
    {
        // Notify
        // ...

        base.OnError(e);
    }

    protected override void OnClose(WebSocketSharp.CloseEventArgs e)
    {
        // Close 
        // ...

        base.OnClose(e);
    }
}
kjyoffice commented 1 year ago

Self answer. 😂😂😂

            // like
            var hwssList = wsHost.CALL_MAGIC_PROPERTY_OR_METHOD;
            // or 
            var hwssList = wss.CALL_MAGIC_PROPERTY_OR_METHOD;

Solution.

            var hwssList = wsHost.Sessions.Sessions.Cast<HelloWebSocketService>().ToList();