Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将数据从C#表单发送到node server socket.io_C#_Node.js_Socket.io - Fatal编程技术网

将数据从C#表单发送到node server socket.io

将数据从C#表单发送到node server socket.io,c#,node.js,socket.io,C#,Node.js,Socket.io,我试图从一个C#表单发送数据,该表单存储在一个名为ClientMsg的变量中。我正在使用SocketIoClientDotNet与节点服务器通信。我有连接设置,一切正常,但我无法将数据从表单发送到服务器 谁能告诉我怎么做,因为我在网上找不到任何东西 更新(添加代码): 按钮: private void btnSend_Click(object sender, EventArgs e) { String ClientMsg = txtSend.Text;

我试图从一个C#表单发送数据,该表单存储在一个名为
ClientMsg
的变量中。我正在使用
SocketIoClientDotNet
与节点服务器通信。我有连接设置,一切正常,但我无法将数据从表单发送到服务器

谁能告诉我怎么做,因为我在网上找不到任何东西

更新(添加代码):

按钮:

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

您的代码存在的问题是,您试图在连接后直接发送消息,使用了
ClientMsg
变量,该变量最初为空。
即使在文本框中键入内容,它也将保持为空,因为在按钮单击事件中,您正在声明一个新的
ClientMsg
,它是本地的,因此您不能使用全局的

以下是它应该是什么样子:

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

您的代码存在的问题是,您试图在连接后直接发送消息,使用了
ClientMsg
变量,该变量最初为空。
即使在文本框中键入内容,它也将保持为空,因为在按钮单击事件中,您正在声明一个新的
ClientMsg
,它是本地的,因此您不能使用全局的

以下是它应该是什么样子:

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

你能不能给我们一些代码片段(不是图片上的代码)?如果不可能将代码复制/粘贴到IDE或其他任何地方,就很难尝试代码。@colidyre很抱歉这一切都是新的!AddedI我不确定我是否收到了你的代码,你从表格中的数据发送到哪里?@Haytam我正在尝试将数据从我的文本框发送到服务器哦,我现在收到了。您现在所做的将不起作用,因为当单击按钮时,您实际上没有发送任何内容。您能给我们一些代码片段(而不是像图片一样的代码)吗?如果不可能将代码复制/粘贴到IDE或其他任何地方,就很难尝试代码。@colidyre很抱歉这一切都是新的!AddedI我不确定我是否收到了你的代码,你从表格中的数据发送到哪里?@Haytam我正在尝试将数据从我的文本框发送到服务器哦,我现在收到了。你现在所做的将不起作用,因为当点击按钮时,你实际上没有发送任何内容。非常感谢你的帮助,非常感谢!非常感谢您的帮助,非常感谢!