problem to use websocketsharp in lower layer

Open
#447 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
20/100
Issue type
Refactor
Clarity
Needs clarification
Activity status
Stale
Tech stack
csharp

Research direction

The issue only shows websocket usage in Form1 and its button and load handlers; start by tracing those handlers and the WebSocket event callbacks. Determine whether the connection and message handling can be separated from the Windows Forms controls, with the data-layer class exposing a usable connection, send, receive, and close workflow.

Written by the indexing model from the issue text.

Description

Hi guys
i am programing on windows form project
can any body help me to use websocketsharp in lower layer (Data layer) ??
this code works fine to me, but i dont know how must be use in data layer as a class

` private WebSocket _webSocket;
const string Uri = "wss://api.com/ws";
public Form1()
{
InitializeComponent();
}

    private void Form1_Load(object sender, EventArgs e)
    {
        try
        {
            _webSocket = new WebSocket(Uri);
            _webSocket.OnOpen += (ss, ee) =>
               listBox1.Items.Add(string.Format("Connected to {0} successfully ", Uri));
            _webSocket.OnError += (ss, ee) =>
               listBox1.Items.Add("     Error: " + ee.Message);
            _webSocket.OnMessage += (ss, ee) =>
                this.Invoke((MethodInvoker)delegate
                {
                    richTextBox1.Text = ee.Data.ToString();
                });
            _webSocket.OnClose += (ss, ee) =>
               listBox1.Items.Add(string.Format("Disconnected with {0}", Uri));
        }
        catch (Exception exception)
        {
            MessageBox.Show(exception.Message);
            throw;
        }

        
    }

    private void btnConnect_Click(object sender, EventArgs e)
    {
        _webSocket.Connect();
    }

    private void btnDisconnect_Click(object sender, EventArgs e)
    {
        _webSocket.Close();
    }

    private void btnSend_Click(object sender, EventArgs e)
    {
        var content = inputBox.Text;
        if (!string.IsNullOrEmpty(content))
            _webSocket.Send(content);
    }`       
Dominant language
C#
Stars
6.1k
Forks
1.7k
PR merge metrics
No merged PRs in 30d

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.

More from sta/websocket-sharp

All issues in sta/websocket-sharp

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.