Xamarin Android - A Quick Demonstration Of SignalR

Introduction

Last time, I had made a simple signalR chat client app. Today, I shall show you a quick demonstration of SignalR.

What is SignalR?

Signalr is a library for asp.net developers that revamps the path toward adding progressing web value to applications. Consistent web convenience is the ability to have server code push substance to related clients immediately as it twists up recognizably available, rather than having the server sit tight for a client to request new data.



Prerequisites

You are required to have basic knowledge of asp.net web application and intermediate level of knowledge xamarin.android and basic idea of microsoft azure web app publishing.
  1. Asp.Net Web Application
  2. Microsoft Azure account
  3. Xamarin Android app
  4. SignalR Component
Server Side Application

Step 1

Open Visual Studio-> New Project-> Templates-> Visual C#-> Web -> ASP.NET Web Application. Give the project name SignalR_Service and select empty template.

(ProjectName: SignalR_Service)

Step 2

Next go to Solution Explorer-> Project Name-> then Right Click to Add -> HTML Page.

(HtmlPage Name: Index)



Step 3

Again go to Solution Explorer-> Project Name-> then Right Click to Add -> New Item-> SignalR Hub Class (v2) with name ChatHub.cs. The hub class essentially is a type of connection that makes connectivity between server and client.

(Class Name: NewHub.cs)

Step 4

We need to add a Startup class so, go to Solution Explorer-> Project Name-> then Right Click to Add -> New Item-> OWIN Startup Class with name Startup.cs

(Class Name: Startup)

 

Now we need to configure owin startup class. Go to Solution Explorer-> Project Name-> Startup.cs and write following code in startup class. 

 
  1. //Any Connection or Hub wire up and configuration should go here.
  2. app.MapSignalR();

Step 5

Next go to Solution Explorer-> Project Name-> ChatHub then create a chathub class and replace following code with Hello method.

(File Name: ChatHub.cs)


  1. public void Send(string name, string message)   
  2. {  
  3.     // Call the broadcastMessage method to update clients.  
  4.     Clients.All.broadcastMessage(name, message);  
  5. }  
  6. public void DragView(float rawX, float initX, float rawY, float initY)   
  7. {  
  8.     Clients.Others.UpdateView(rawX, initX, rawY, initY);  
  9. }  

Step 7

Bulid and run the project. Web app publishing is a very simple method therefore, i am skipping these process.

Go to Solution Explorer-> Project Name-> then Right Click -> Publish.

 

This service successfully publish on microsoft azure. Now everyone can use and access this service from given url.

http://azuresignalrserver.azurewebsites.net/

Client Side

Step 1

Open Visual Studio-> New Project-> Templates-> Visual C#-> Android-> Blank app and give the project name SignalR_Drag.

(ProjectName: SignalR_Drag)

Step 2

Next, open Solution Explorer-> Project Name-> Resources-> Layout-> Main.axml. Open main.axml file and add following code.

(FileName: Main.axml)

AXML Code 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3. android:id="@+id/root" 
  4. android:orientation="vertical" 
  5. android:layout_width="match_parent" 
  6. android:layout_height="match_parent">  
  7.     <View android:id="@+id/draggableView" 
  8. android:layout_width="100dp" 
  9. android:layout_height="100dp" 
  10. android:background="@android:color/holo_green_dark" 
  11. android:layout_gravity="center" />   
  12. </FrameLayout>  

Layout

 

Step 3

Go to Solution Explorer-> Components -> then Right Click-> Get More Components. In this way, you can move on xamarin components store then search SignalR and add to app.



Step 4

Now, go to solution Explorer-> Project Name-> MainActivity file and add following code and use appropriate namespaces.

(FileName: MainActivity.cs)

MainActivity C# Code
  1. using Android.App;  
  2. using Android.Widget;  
  3. using Android.OS;  
  4. using Android.Views;  
  5. using Microsoft.AspNet.SignalR.Client;  
  6. using System;  
  7. namespace SignlR_Android {  
  8.     [Activity(Label = "SignlR_Drag", MainLauncher = true)]  
  9.     public class MainActivity: Activity {  
  10.         private FrameLayout mRoot;  
  11.         private View draggableView;  
  12.         private IHubProxy mhubProxy;  
  13.         private float XInit = 0;  
  14.         private float YInit = 0;  
  15.         protected async override void OnCreate(Bundle savedInstanceState) {  
  16.             base.OnCreate(savedInstanceState);  
  17.             // Set our view from the "main" layout resource  
  18.             SetContentView(Resource.Layout.Main);  
  19.             mRoot = FindViewById < FrameLayout > (Resource.Id.root);  
  20.             draggableView = FindViewById(Resource.Id.draggableView);  
  21.             draggableView.Touch += DraggableView_Touch;  
  22.             HubConnection hubConnection = new HubConnection("http://azuresignalrserver.azurewebsites.net/");  
  23.             mhubProxy = hubConnection.CreateHubProxy("ChatHub");  
  24.             try {  
  25.                 await hubConnection.Start();  
  26.             } catch (Exception) {  
  27.                 //Catch handle Errors.   
  28.             }  
  29.             mhubProxy.On < floatfloatfloatfloat > ("UpdateView", (rawX, initX, rawY, initY) => {  
  30.                 RunOnUiThread(() => {  
  31.                     draggableView.Animate().X(rawX + initX).Y(rawY + initY).SetDuration(0).Start();  
  32.                     mRoot.Invalidate();  
  33.                 });  
  34.             });  
  35.         }  
  36.         private async void DraggableView_Touch(object sender, View.TouchEventArgs e) {  
  37.             float x = e.Event.RawX;  
  38.             float y = e.Event.RawY;  
  39.             View touchedView = sender as View;  
  40.             switch (e.Event.Action) {  
  41.                 case MotionEventActions.Down:  
  42.                     XInit = touchedView.GetX() - x;  
  43.                     YInit = touchedView.GetY() - y;  
  44.                     break;  
  45.                 case MotionEventActions.Move:  
  46.                     touchedView.Animate().X(e.Event.RawX + XInit).Y(e.Event.RawY + YInit).SetDuration(0).Start();  
  47.                     break;  
  48.                 default:  
  49.                     break;  
  50.             }  
  51.             mRoot.Invalidate();  
  52.             await mhubProxy.Invoke("DragView"new object[] {  
  53.                 e.Event.RawX, XInit, e.Event.RawY, YInit  
  54.             });  
  55.         }  
  56.     }  
  57. }  
Output

Finally, we have done our SignalR application, just rebuild and run the project. You will have the result like below.

 


Summary
  • What is SiganlR
  • Asp.Net Web Application
  • Publish to Azure
  • Xamrin.Android Application
  • Add SignalR Component
  • Output

Up Next
    Ebook Download
    View all
    Learn
    View all