websocket-sharp icon indicating copy to clipboard operation
websocket-sharp copied to clipboard

OnMessage() running some code before Send() closes connection

Open dewdrinker19 opened this issue 3 years ago • 1 comments

Hello, I have a switch setup so depending on what the incoming message from the client is my program will do different things. I pasted the code below, the case on top works fine, the one on bottom crashes and says connection is not open. I'd like to be able to process information and then send a message back to the client. I apologize if I'm an idiot and doing this completely wrong all together.

public class Website : WebSocketBehavior
    {
        protected override void OnMessage(MessageEventArgs e)
        {
            string msg = "";
            switch (e.Data)
            {
                case var s when e.Data.StartsWith("~echo~"):
                    msg = e.Data.Substring(6);
                    Send(msg);
                    MessageBox.Show(msg);
                    break;
                case var s when e.Data.StartsWith("~getid~"):
                    msg = e.Data.Substring(7);
                    MessageBox.Show(msg);
                    Send(msg);
                    break;
                default:
                    break:
            }
        }      
    }

    internal class WebSocketClass       
    {
        internal void LoadWebSocketServer()
        {
            WebSocketServer wssv = new WebSocketServer("ws://localhost:7890");
            wssv.AddWebSocketService<Website>("/Website");
            wssv.Start();

            
        }
    }

dewdrinker19 avatar Sep 17 '22 08:09 dewdrinker19

I have a workaround. Every time I run code during the OnMessage() it closes the connection and immediately reopens a new one. So I did this and it seems to work. The reason I did the DateNow stuff if just in case my code I want to run takes longer than the new connection takes to open.

public class Website : WebSocketBehavior
    {
        public static string sendsocket = "";
        protected override void OnMessage(MessageEventArgs e)
        {
            string msg = "";
            switch (e.Data)
            {
                case var s when e.Data.StartsWith("~echo~"):
                    msg = e.Data.Substring(6);
                    sendsocket = msg;
                    MessageBox.Show(msg);
                    break;

                case var s when e.Data.StartsWith("~getid~"):
                    msg = e.Data.Substring(7);
                    MessageBox.Show(msg);
                    sendsocket = msg;
                    break;

                default:
                    break;
            }
        }

        protected override void OnOpen()
        {
            DateTime test = DateTime.Now;           
            while(test.AddSeconds(2) > DateTime.Now)
            {
                if (sendsocket != "")
                {
                    Send(sendsocket);
                    sendsocket = "";
                    break;
                }
            }
        }
    }

    internal class WebSocketClass       
    {
        internal void LoadWebSocketServer()
        {
            WebSocketServer wssv = new WebSocketServer("ws://localhost:7890");
            wssv.AddWebSocketService<Website>("/Website");
            wssv.Start();
        }
    }

dewdrinker19 avatar Sep 19 '22 07:09 dewdrinker19