C# Websocket programming: the simplest example

23 Jul

Create a winform Application:


Download the Websocket-Sharp from Nuget:

PM> Install-Package WebSocketSharp -Pre

Add below code:

        private WebSocket client;
        const string host = “wss://”;

        private void Form1_Load(object sender, EventArgs e)
            client = new WebSocket(host);

            client.OnOpen += (ss, ee) =>
               listBox1.Items.Add(string.Format(“Connected to {0} successfully “, host));
            client.OnError += (ss, ee) =>
               listBox1.Items.Add(”     Error: “ + ee.Message);
            client.OnMessage += (ss, ee) =>
               listBox1.Items.Add(“Echo: “ + ee.Data);
            client.OnClose += (ss, ee) =>
               listBox1.Items.Add(string.Format(“Disconnected with {0}”, host));

Add below button click event handler:

        private void SendButton_Click(object sender, EventArgs e)
            var content = inputBox.Text;

        private void ConnectButton_Click(object sender, EventArgs e)

        private void DisconnectButton_Click(object sender, EventArgs e)

That is it! Run your app and enjoy!


Posted by on July 23, 2015 in Dotnet/C#, Programming


Tags: , , , , , , , ,

12 responses to “C# Websocket programming: the simplest example

  1. Harry

    November 23, 2015 at 4:32 pm

    First websocket example worked. Thanks.

  2. Someday Soon (RoyalVlogger)

    June 10, 2016 at 11:16 pm

    How would you send Json to the server though?

  3. Someday Soon (RoyalVlogger)

    June 10, 2016 at 11:17 pm

    private void button1_Click(object sender, EventArgs e)
    var json = “{\”op\”:\”unconfirmed_sub\”}”;

    Does not seem to work?

  4. Nick F

    March 13, 2017 at 8:26 pm

    I can only connect when I use ws:// When I send the message, it fails at the line client.OnMessage += (ss, ee) => listBox1.Items.Add(” Echo: ” + ee.Data); – I get “an error occured in sending the data”

  5. Mike Lowry

    August 9, 2017 at 10:18 pm

    Using nuget install of WebSocketSharp:
    At the client.OnMessage event I am getting an error: “System.InvalidOperationException: ‘Cross-thread operation not valid: Control ‘listBox1′ accessed from a thread other than the thread it was created on.’

  6. xinyustudio

    August 9, 2017 at 10:20 pm

    Refer to this answer.

  7. Mike Lowry

    August 9, 2017 at 11:23 pm

    That worked. Thanks.

  8. Mike Lowry

    August 10, 2017 at 1:24 am

    Can you offer advice on how to structure a form that has an interactive transaction (like the example) in one panel, and a running multi-receive panel in the other, e.g., a discussion forum that updates as new items are posted? Need to have all on the same connection, with a decision on message receipt as to which control to post to.

  9. RoySeberg

    September 6, 2018 at 11:12 pm

    I get connected successfully but the message sent is not echoed back, even after adding the cross-thread fix. What’s wrong?

  10. RoySeberg

    September 6, 2018 at 11:20 pm

    Sorry, my bad, my listBox1 was not set up correctly to add items. Now it’s echoing just fine. Thank you.

  11. ernesto

    October 18, 2018 at 1:11 am

    whats the notation for web socket using an ip address, I used “wss://” and having problems connecting. Thanks

  12. Eduardo

    February 11, 2019 at 3:51 am

    I’s possible to change, on the fly, the websockets server, in a winform application?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: