我正在使用SignalR在服务器和客户端之间进行双向通信,服务器和客户端都在使用Win应用程序。
服务器端,我在DropDownlist中添加所有已连接的客户端ID,现在我想做两件事。
1)我要从下拉列表中选择客户端ID,然后在单击按钮时将字符串发送到所选客户端。
我的服务器端在这里:
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))
))
);
希望有帮助
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句