如何在 winForm SIgnalR 中单击按钮将数据从服务器发送到特定的客户端 ID

how to send data from server to specific client ID on button click in winForm SIgnalR

我正在使用 SignalR 在服务器和客户端之间进行双向通信,服务器和客户端都在使用 Win 应用程序。

服务器端我在 DropDownlist 中添加所有连接的客户端 ID,现在我想做两件事。

1) 我想要下拉列表中的 select 客户端 ID,然后在单击按钮时将字符串发送到 selected 客户端。

我的服务器端在这里 :

private void buttonClient_Click(object sender, EventArgs e)
        {
         // here i want write code for send data to selected client .
          string Clientid = comboBoxClients.SelectedItem.ToString();

        }

public class MyHub : Hub
    {
 public void Send(string name, string message){ Clients.All.addMessage(name, message);}
    }

我的客户端代码在这里:

private void ButtonSend_Click(object sender, EventArgs e)
        {
            HubProxy.Invoke("Send", UserName, TextBoxMessage.Text);
            TextBoxMessage.Text = String.Empty;
            TextBoxMessage.Focus();
        }

首先添加IhubContext的全局对象。

IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();



private void buttonClient_Click(object sender, EventArgs e)
        {
            string Clientid = comboBoxClients.SelectedItem.ToString();
// sendOrders(string,string) this method should make on client side with same name  and here call like thats . 
            context.Clients.Client(Clientid).sendOrders("Name","Message Server to you"); 

        }

// 我的客户端代码在这里: //创建并连接集线器连接和集线器代理并调用方法名称 SendOrders。 //在控制台窗口中显示消息

HubProxy.On<string, string>("sendOrders", (name, myString) =>
             this.Invoke((Action)(() =>
                    RichTextBoxConsole.AppendText(String.Format("{0}: {1}" + Environment.NewLine, name, myString))
                ))
            );

希望对您有所帮助

在signalr中你可以使用groups since state is missing by default. To send messages to a specific client you can map users to connections

的概念