繁体   English   中英

Azure Signalr + Azure函数+ wpf客户端-如何使用azure函数和自托管的Azure SignalR App创建wpf客户端应用程序

[英]Azure Signalr +Azure function + wpf client - how to have a wpf client application using azure funtion and self hosted Azure SignalR App

当搜索通过功能连接的天蓝色信号R时,几乎所有结果都会出现在Asp客户端中。 是否可以将wpf client(4.7)或wpf core应用程序作为客户端?

是的,您可以将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(); 
            } 
        }   
    } 
} 

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM