sta / websocket-sharp

A C# implementation of the WebSocket protocol client and server

Home Page:http://sta.github.io/websocket-sharp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

dewdrinker19 opened this issue · 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();

            
        }
    }

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();
        }
    }