Feedjit

Articles for you

Saturday, June 22, 2013

WPF Client for Console Server Signalr Server. ASP.NET, Signalr, WPF Client

Main Window.Xaml
////////////////////////////
<Window x:Class="Virtual_Trainer_Client.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button x:Name="Button1" Content="Send" HorizontalAlignment="Left" Margin="268,35,0,0" VerticalAlignment="Top" Width="75" Click="Button1_Click"/>
        <Button x:Name="Button2" Content="Connect" HorizontalAlignment="Left" Margin="385,35,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.5,0.5" Click="Button2_Click"/>
        <ListBox x:Name="listBox1" HorizontalAlignment="Left" Height="100" Margin="183,107,0,0" VerticalAlignment="Top" Width="258" RenderTransformOrigin="0.231,0.401"/>
        <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="39" Margin="87,63,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="127"/>
        <TextBox x:Name="textbox2" HorizontalAlignment="Left" Height="80" Margin="29,107,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="149"/>
        <Label x:Name="label_welcome" Content="" HorizontalAlignment="Left" Margin="19,10,0,0" VerticalAlignment="Top" Height="26" Width="149"/>

    </Grid>
</Window>
///////////////////////////////////////////////////////////////////
Main Window.xaml.cs
///////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Client;
using Microsoft.AspNet.SignalR.Client.Hubs;
//using System.Windows.Threading.Dispatcher;


namespace Virtual_Trainer_Client
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        IHubProxy proxy;
        HubConnection connection;
        public MainWindow()
        {
            InitializeComponent();
         
       
        }
        private void Button2_Click(object sender, RoutedEventArgs e)
        {
            connection = new HubConnection(@"http://localhost:8081/");
            proxy = connection.CreateHubProxy("commHub");
           
          
            proxy.On("broadCastToClients", message =>function(message));
            proxy.On("joined",mess=>this.Dispatcher.Invoke((Action)(()=>label_welcome.Content="Wellcome from Server")));
          
            connection.Error += connection_Error;
            connection.Start().Wait();
        }
        void connection_Error(Exception obj)
        {
            MessageBox.Show(obj.Message);
        }

        void function(string mess)
        {
            this.Dispatcher.Invoke((Action)(() => { textbox2.Text += mess+'\n'; }));
        }
      

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
      
          
            proxy.Invoke("send", textBox1.Text);
            textBox1.Text = null;
       
        }
        private  void UpdateApplicationDataUI(string msg)
        {
            //txtStatus.Text = "test";
            textbox2.Text = msg;
        }
   
       
    }
    public static class ControlExtensions
    {
        public static void Invoke(this Control Control, Action Action)
        {
            Control.Invoke(Action);
        }
    }
}
/////////////////////////////////////////////////////
Run Server
then Client
Click on connect a welcome message will be sent from server to client.
Then any message sent by the client will be received back from the server.

Read More

Articles for you