sta / sta/websocket-sharp

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

Open
#708 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
6.1k
Forks
1.7k
PR merge metrics
No merged PRs in 30d

Description

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

            
        }
    }

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with WebSocketBehavior.OnMessage in the provided Website class and trace how Send is reached in each switch case. Then inspect LoadWebSocketServer and the WebSocketServer lifecycle to determine why the connection is closed before the second Send. Done means the requested message-processing flow sends a response without reporting that the connection is not open.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.