.net 5 SignalR WPF 服务端+客户端

 服务端:

Microsoft.AspNetCore.SignalR;

Microsoft.AspNetCore.SignalR.Common;

Microsoft.AspNetCore.SignalR.Core;

Microsoft.Extensions.Hosting

Microsoft.AspNetCore.App;

客户端:

Microsoft.AspNetCore.SignalR.Client;

--------------------------------------------------------------

服务端代码:

 1 const string ServerURI = "http://localhost:51180";
 2         private IHost _host;
 3         public MainWindow()
 4         {
 5             InitializeComponent();
 6             
 7         }
 8         private async void Button_Click(object sender, RoutedEventArgs e)
 9         {
10             _host?.Dispose();
11             _host = Host.CreateDefaultBuilder()
12                 .ConfigureWebHostDefaults(webBuilder => webBuilder
13                     .UseUrls(ServerURI)
14                     .ConfigureServices(services => services.AddSignalR())
15                     .Configure(app =>
16                     {
17                         app.UseRouting();
18                         app.UseEndpoints(endpoints => endpoints.MapHub<MyHub>("/MyHub"));
19                     }))
20                .Build();
21             await _host.StartAsync();
22         }
 1 public class MyHub:Hub
 2     {
 3         
 4         public override Task OnConnectedAsync()
 5         {
 6            
 7             return base.OnConnectedAsync();
 8         }
 9         public override Task OnDisconnectedAsync(Exception exception)
10         {
11             return base.OnDisconnectedAsync(exception);
12         }
13         public async Task SendAsync(string name)
14         {
15             var c = Context.ConnectionId.AsEnumerable();
16             await Clients.All.SendAsync("FormServerMessage",$"客户端:{Context.ConnectionId }");
17         }
18     }

客户端代码:

 1 using Microsoft.AspNetCore.SignalR.Client;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 using System.Windows;
 8 using System.Windows.Controls;
 9 using System.Windows.Data;
10 using System.Windows.Documents;
11 using System.Windows.Input;
12 using System.Windows.Media;
13 using System.Windows.Media.Imaging;
14 using System.Windows.Navigation;
15 using System.Windows.Shapes;
16 
17 namespace WpfApp4
18 {
19     /// <summary>
20     /// Interaction logic for MainWindow.xaml
21     /// </summary>
22     public partial class MainWindow : Window
23     {
24         const string ServerURI = "http://localhost:51180/MyHub";
25         HubConnection connection;
26         public MainWindow()
27         {
28             InitializeComponent();
29         }
30 
31         private void link_Click(object sender, RoutedEventArgs e)
32         {
33             try
34             {
35                 InitializeComponent();
36                 connection = new HubConnectionBuilder()
37                    .WithUrl(ServerURI)
38                    .WithAutomaticReconnect()
39                    .Build();
40 
41                 connection.Closed += async (error) =>
42                 {
43 
44                     await connection.StartAsync();
45                 };
46                 connection.On<string>("FormServerMessage", msg =>
47                 {
48                     this.Dispatcher.BeginInvoke((Action)delegate
49                     {
50                         content.Text += $"
{msg}";
51                     });
52                 });
53                 connection.StartAsync().ConfigureAwait(true);
54             }
55             catch (Exception)
56             {
57 
58             }
59         }
60 
61         private async void send_Click(object sender, RoutedEventArgs e)
62         {
63             //connection.StartAsync();
64             try
65             {
66                 await connection.InvokeAsync("SendAsync", "name:w");
67             }
68             catch (Exception)
69             {
70 
71                 72             }
73         }
74     }
75 }
原文地址:https://www.cnblogs.com/weiruanojbk/p/14436930.html