0

I am trying to send data from a C# form which I have stored in a variable called ClientMsg. I am using SocketIoClientDotNet to communicate to the node server. I have the connection setup which is all fine but I am unable to send data from the form to my server.

Could someone tell me how to do this as I cant find anything online?

Code Code Node

Update (added code):

private void socketManager()
    {
        var server = IO.Socket("http://localhost");
        server.On(Socket.EVENT_CONNECT, () =>
        {
            UpdateStatus("Connected");
        });
        server.On(Socket.EVENT_DISCONNECT, () =>
        {
            UpdateStatus("disconnected");
        });
        server.Emit("admin", ClientMsg);
    }

Button:

private void btnSend_Click(object sender, EventArgs e)
    {
        String ClientMsg = txtSend.Text;
        if (ClientMsg.Length == 0)
        {
            txtSend.Clear();
        }
        else
        {
            txtSend.Clear();
            lstMsgs.Items.Add("You:" + " " + ClientMsg);
        }
    }
8
  • 1
    Can you please give us code snippets (not code as picture)? It's hard to try code without the possibility to copy/paste it into IDE or whatever. Commented Aug 18, 2018 at 13:59
  • @colidyre sorry about that new to all this! Added Commented Aug 18, 2018 at 14:08
  • I'm not sure I get your code, where are you sending data from your form? Commented Aug 18, 2018 at 14:14
  • @Haytam I am trying to send data from my textbox to the server Commented Aug 18, 2018 at 14:15
  • Oh I get it now. What you're doing now will not work as you're not actually sending anything when the button is clicked. Commented Aug 18, 2018 at 14:16

1 Answer 1

2

The problem with your code is that you're trying to send a message directly after connecting, using the ClientMsg variable which is null at first.
Even if you type something in your textbox, it'll stay null because in your button click event you're declaring a new ClientMsg which is local, so you're not working with the global one.

Here's how it should be:

// Save your connection globally so that you can
// access it in your button clicks etc...
Socket client;

public Form1()
{
    InitializeComponent();
    InitializeClient();
}

private void InitializeClient()
{
    client = IO.Socket("http://localhost");
    client.On(Socket.EVENT_CONNECT, () =>
    {
        UpdateStatus("Connected");
    });
    client.On(Socket.EVENT_DISCONNECT, () =>
    {
        UpdateStatus("disconnected");
    });
}

private void btnSend_Click(object sender, EventArgs e)
{
    String clientMsg = txtSend.Text;
    if (ClientMsg.Length == 0)
    {
        // No need to clear, its already empty
        return;
    }
    else
    {
        // Send the message here
        client.Emit("admin", clientMsg);
        lstMsgs.Items.Add("You:" + " " + clientMsg);
        txtSend.Clear();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for the help, Really appreciate it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.