• SignalR示例


    做了个SignalR的示例,官方的示例服务是宿主在asp.net core的。

    aspnet/SignalR-samples: Samples for ASP.NET Core SignalR (github.com)

    SignalR/samples/SignalRSamples at master · aspnet/SignalR (github.com)

    参考网上的文章做了个宿主在winform上的示例,几点总结记录一下:

    1. winform上使用的是owin提供网站服务,但owin不支持.net core,只能用.net framework。最先用.net 6.0启动服务一直报错。可直接Nuget安装 Microsoft.AspNet.SignalR.SelfHost.

    2. 客户端连接要用new HubConnection的方式,用官网的winform示例会一直报连接中断。

    3. 不同客户端可通过CreateHubProxy里指定不同的Hub名称实现类似Topic的功能。

    4. 服务端可通过 GlobalHost.ConnectionManager.GetHubContext();取得Hub上下文然后发送消息。

    5. 获得主窗体和跨线程处理以及代理方法的写法,winform 和 wpf 有区别。WPF是 Application.Current.Dispatcher.Invoke,在Winform里Application.OpenForms[0].Invoke

    C# WinForm获得主窗体——如何判断哪个是主窗体 - 白い - 博客园 (cnblogs.com)

    服务端程序后台代码:

    1. public partial class Form1 : Form
    2. {
    3. string ServerUri = "http://localhost:8011/signalr";
    4. private IDisposable SignalR { get; set; }
    5. public Form1()
    6. {
    7. InitializeComponent();
    8. }
    9. private void btnStart_Click(object sender, EventArgs e)
    10. {
    11. ServerUri=txtSvrUri.Text;
    12. StartServer();
    13. }
    14. ///
    15. /// 启动SignalR服务,将SignalR服务寄宿在WPF程序中
    16. ///
    17. private void StartServer()
    18. {
    19. try
    20. {
    21. SignalR = WebApp.Start(ServerUri); // 启动SignalR服务
    22. }
    23. catch (TargetInvocationException)
    24. {
    25. LogMsg("一个服务已经运行在:" + ServerUri);
    26. // Dispatcher回调来设置UI控件状态
    27. //this.Invoke(new Action(()=> btnStart.Enabled = true));
    28. return;
    29. }
    30. this.Invoke(new Action(() => btnStart.Enabled = false));
    31. this.Invoke(new Action(() => btnStop.Enabled = true));
    32. LogMsg("服务已经成功启动,地址为:" + ServerUri);
    33. }
    34. public delegate void UpdateForm_dl();
    35. public void LogMsg(string msg)
    36. {
    37. richTxtMsg.AppendText($"{msg}{Environment.NewLine}");
    38. }
    39. private void btnStop_Click(object sender, EventArgs e)
    40. {
    41. IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext();
    42. hubContext.Clients.All.SendClose("服务端已关闭");
    43. SignalR.Dispose();
    44. LogMsg("服务已经成功停止,地址为:" + ServerUri);
    45. this.Invoke(new Action(() => btnStart.Enabled = true));
    46. this.Invoke(new Action(() => btnStop.Enabled = false));
    47. }
    48. }
    49. public class Startup
    50. {
    51. public void Configuration(IAppBuilder app)
    52. {
    53. // 有关如何配置应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkID=316888
    54. // 允许CORS跨域
    55. //app.UseCors(CorsOptions.AllowAll);
    56. app.MapSignalR();
    57. }
    58. }
    59. public class ChatHub : Hub
    60. {
    61. public void Send(string name, string message)
    62. {
    63. Clients.All.AddMessage(name, message);
    64. }
    65. public override Task OnConnected()
    66. {
    67. //
    68. Application.OpenForms[0].Invoke(new Action(() =>
    69. ((Form1)Application.OpenForms[0]).LogMsg("客户端连接,连接ID是: " + Context.ConnectionId)));
    70. return base.OnConnected();
    71. }
    72. public override Task OnDisconnected(bool stopCalled)
    73. {
    74. Application.OpenForms[0].Invoke(new Action(() =>
    75. ((Form1)Application.OpenForms[0]).LogMsg("客户端断开连接,连接ID是: " + Context.ConnectionId)));
    76. return base.OnDisconnected(true);
    77. }
    78. }
    79. public class ChatHub2 : Hub
    80. {
    81. public void Send(string name, string message)
    82. {
    83. Clients.All.AddMessage(name, message);
    84. }
    85. public override Task OnConnected()
    86. {
    87. //
    88. Application.OpenForms[0].Invoke(new Action(() =>
    89. ((Form1)Application.OpenForms[0]).LogMsg("客户端连接2,连接ID是: " + Context.ConnectionId)));
    90. return base.OnConnected();
    91. }
    92. public override Task OnDisconnected(bool stopCalled)
    93. {
    94. Application.OpenForms[0].Invoke(new Action(() =>
    95. ((Form1)Application.OpenForms[0]).LogMsg("客户端断开连接2,连接ID是: " + Context.ConnectionId)));
    96. return base.OnDisconnected(true);
    97. }
    98. }

    客户端后台代码:

    1. public partial class Form1 : Form
    2. {
    3. public IHubProxy HubProxy { get; set; }
    4. string ServerUri = "http://localhost:8011/signalr";
    5. public HubConnection Connection { get; set; }
    6. public Form1()
    7. {
    8. InitializeComponent();
    9. }
    10. private void btnConnect_Click(object sender, EventArgs e)
    11. {
    12. ServerUri = txtSvrUri.Text;
    13. ConnectAsync();
    14. }
    15. private void btnSend_Click(object sender, EventArgs e)
    16. {
    17. // 通过代理来调用服务端的Send方法
    18. // 服务端Send方法再调用客户端的AddMessage方法将消息输出到消息框中
    19. HubProxy.Invoke("Send", GenerateRandomName(4), txtSend.Text.Trim());
    20. txtSend.Text = String.Empty;
    21. txtSend.Focus();
    22. }
    23. public void LogMsg(string msg)
    24. {
    25. richTxtMsg.AppendText($"{msg}{Environment.NewLine}");
    26. }
    27. private async void ConnectAsync()
    28. {
    29. Connection = new HubConnection(ServerUri);
    30. Connection.Closed += Connection_Closed;
    31. Connection.Received += Connection_Received;
    32. // 创建一个集线器代理对象
    33. HubProxy = Connection.CreateHubProxy(txtTopic.Text);
    34. // 供服务端调用,将消息输出到消息列表框中
    35. HubProxy.On<string, string>("AddMessage", (name, message) =>
    36. this.Invoke(new Action(() =>
    37. LogMsg(String.Format("{0}: {1}", name, message)))
    38. ));
    39. try
    40. {
    41. await Connection.Start();
    42. }
    43. catch (HttpRequestException)
    44. {
    45. // 连接失败
    46. return;
    47. }
    48. // 显示聊天控件
    49. //ChatPanel.Visibility = Visibility.Visible;
    50. btnSend.Enabled = true;
    51. txtSend.Focus();
    52. LogMsg("连上服务:" + ServerUri);
    53. }
    54. private void Connection_Closed()
    55. {
    56. this.Invoke(new Action(() =>
    57. LogMsg("connection closed"))
    58. );
    59. }
    60. private void Connection_Received(string data)
    61. {
    62. this.Invoke(new Action(() =>
    63. LogMsg($"recived data:{data}"))
    64. );
    65. }
    66. public string GenerateRandomName(int length)
    67. {
    68. var newRandom = new System.Text.StringBuilder(62);
    69. var rd = new Random();
    70. for (var i = 0; i < length; i++)
    71. {
    72. newRandom.Append(Constant[rd.Next(62)]);
    73. }
    74. return newRandom.ToString();
    75. }
    76. private readonly char[] Constant =
    77. {
    78. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    79. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
    80. 'w', 'x', 'y', 'z',
    81. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
    82. 'W', 'X', 'Y', 'Z'
    83. };
    84. private void btnDisconnect_Click(object sender, EventArgs e)
    85. {
    86. Connection.Stop();
    87. }
    88. }

    [Asp.net 开发系列之SignalR篇]专题一:Asp.net SignalR快速入门 - Learning hard - 博客园 (cnblogs.com)

    基于SignalR的服务端和客户端通讯处理_独星的博客-CSDN博客

  • 相关阅读:
    redis学习笔记
    https攻击怎么防御
    玩转Matlab-Simscape(初级)- 10 - 基于COMSOL&Simulink 凸轮机构的控制仿真
    同志们,都开什么题啊?
    [云原生] K8S 日志收集方案
    这些 MySQL 最朴素的监控方式!用完爱不释手!
    命令行中引导用户指定选择文档
    echarts 实现tooltip自动轮播显示,鼠标悬浮时暂停
    戴尔服务器安装Debian11过程
    第七章 排序
  • 原文地址:https://blog.csdn.net/kuui_chiu/article/details/127443952