【问题标题】:Azure Signalr +Azure function + wpf client - how to have a wpf client application using azure funtion and self hosted Azure SignalR AppAzure Signalr +Azure 功能 + wpf 客户端 - 如何使用 azure 功能和自托管 Azure SignalR 应用程序拥有 wpf 客户端应用程序
【发布时间】:2019-08-09 16:09:01
【问题描述】:

在搜索通过函数连接的 azure signalR 时,几乎所有结果都是 Asp 客户端。是否可以将 wpf 客户端(4.7)或 wpf 核心应用程序作为客户端?

【问题讨论】:

标签: c# wpf azure azure-signalr


【解决方案1】:

是的,您可以使用 WPF 作为客户端,您可以查看 main.xaml 示例文件:

using System; 
using System.Net.Http; 
using System.Windows; 
using Microsoft.AspNet.SignalR.Client; 

namespace WPFClient 
{    
    /// <summary> 
    /// SignalR client hosted in a WPF application. The client 
    /// lets the user pick a user name, connect to the server asynchronously 
    /// to not block the UI thread, and send chat messages to all connected  
    /// clients whether they are hosted in WinForms, WPF, or a web application. 
    /// For simplicity, MVVM will not be used for this sample. 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
        /// <summary> 
        /// This name is simply added to sent messages to identify the user; this  
        /// sample does not include authentication. 
        /// </summary> 
        public String UserName { get; set; } 
        public IHubProxy HubProxy { get; set; } 
        const string ServerURI = "http://localhost:8080/signalr"; 
        public HubConnection Connection { get; set; } 

        public MainWindow() 
        { 
            InitializeComponent(); 
        } 

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

        /// <summary> 
        /// Creates and connects the hub connection and hub proxy. This method 
        /// is called asynchronously from SignInButton_Click. 
        /// </summary> 
        private async void ConnectAsync() 
        { 
            Connection = new HubConnection(ServerURI); 
            Connection.Closed += Connection_Closed; 
            HubProxy = Connection.CreateHubProxy("MyHub"); 
            //Handle incoming event from server: use Invoke to write to console from SignalR's thread 
            HubProxy.On<string, string>("AddMessage", (name, message) => 
                this.Dispatcher.Invoke(() => 
                    RichTextBoxConsole.AppendText(String.Format("{0}: {1}\r", name, message)) 
                ) 
            ); 
            try 
            { 
                await Connection.Start(); 
            } 
            catch (HttpRequestException) 
            { 
                StatusText.Content = "Unable to connect to server: Start server before connecting clients."; 
                //No connection: Don't enable Send button or show chat UI 
                return; 
            } 

            //Show chat UI; hide login UI 
            SignInPanel.Visibility = Visibility.Collapsed; 
            ChatPanel.Visibility = Visibility.Visible; 
            ButtonSend.IsEnabled = true; 
            TextBoxMessage.Focus(); 
            RichTextBoxConsole.AppendText("Connected to server at " + ServerURI + "\r"); 
        } 

        /// <summary> 
        /// If the server is stopped, the connection will time out after 30 seconds (default), and the  
        /// Closed event will fire. 
        /// </summary> 
        void Connection_Closed() 
        { 
            //Hide chat UI; show login UI 
            var dispatcher = Application.Current.Dispatcher; 
            dispatcher.Invoke(() => ChatPanel.Visibility = Visibility.Collapsed); 
            dispatcher.Invoke(() => ButtonSend.IsEnabled = false); 
            dispatcher.Invoke(() => StatusText.Content = "You have been disconnected."); 
            dispatcher.Invoke(() => SignInPanel.Visibility = Visibility.Visible); 
        } 

        private void SignInButton_Click(object sender, RoutedEventArgs e) 
        { 
            UserName = UserNameTextBox.Text; 
            //Connect to server (use async method to avoid blocking UI thread) 
            if (!String.IsNullOrEmpty(UserName)) 
            {      
                StatusText.Visibility = Visibility.Visible; 
                StatusText.Content = "Connecting to server..."; 
                ConnectAsync(); 
            } 
        } 

        private void WPFClient_Closing(object sender, System.ComponentModel.CancelEventArgs e) 
        { 
            if (Connection != null) 
            { 
                Connection.Stop(); 
                Connection.Dispose(); 
            } 
        }   
    } 
} 

查看此link 以获取更多参考。希望对您有所帮助。

【讨论】:

  • 感谢您的回复 Mohit,但我的问题是使用 azure 功能进行消息传递的 azure signalr 应用程序
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多