Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do WebSocketBehaviors work with a WebSocketServer? #742

Open
jheliker opened this issue Dec 19, 2023 · 2 comments
Open

How do WebSocketBehaviors work with a WebSocketServer? #742

jheliker opened this issue Dec 19, 2023 · 2 comments

Comments

@jheliker
Copy link

Hello @sta -

Thanks for the great library! I'm having trouble understanding how WebSocketBehaviors work for a WebSocketServer.

Is there a new instance of the WebSocketBehavior instantiated for every new connection made to the server?

I'm trying to use a WebSocketBehavior to manage all connected clients and I'm getting duplicated messaging.

@GholibjonMadiyarov
Copy link

GholibjonMadiyarov commented Dec 23, 2023

`protected override void OnStart(string[] args)
{
try
{
server = new WebSocketServer("IPPort");

    server.AddWebSocketService<Web>("/Web");
    //server.AddWebSocketService<Phone>("/Phone");
    //server.AddWebSocketService<Tablet>("/Tablet");
    //server.AddWebSocketService<Desktop>("/Desktop");

    server.Start();
    Logs.toFile("OnStartLog.txt", "Success");
}
catch (Exception error)
{
    Logs.toFile("OnStartLog.txt", " Exception: " + error.Message);
}

}`

@GholibjonMadiyarov
Copy link

`class Web : WebSocketBehavior
{
public Web()
{

}

protected override void OnOpen()
{
    Logs.toFile("ClientOpenLog.txt", "Id: " + ID);
}

protected override void OnMessage(MessageEventArgs e)
{
    try
    {
        JObject data = JObject.Parse(e.Data);
        switch (data["query"].ToString())
        {
            case "query1":
                //
                break;

            case "query2":
                //
                break;

            default:
                Logs.toFile("OnMessageLog.txt", " Undifined Query:" + data["query"]);
                break;
        }
    }
    catch (Exception exception)
    {
        Logs.toFile("MessageRecievedLog.txt", "Exception:" + exception.Message);
    }
}

protected override void OnError(ErrorEventArgs e)
{
    Logs.toFile("SocketExceptionsLog.txt", "SocketException:" + e.Message);
}

protected override void OnClose(CloseEventArgs e)
{
    Logs.toFile("ClientCloseLog.txt", "Id: " + ID + ", Code: " + e.Code + ", Reason:" + e.Reason + ", WasClean:" + e.WasClean);
}

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants