How to receive SignalR broadcast message using Microsoft Java-Client?

I need to send a message from an ASP.NET server to Android devices to notify me when the recording status changes. So I would like to use the new SignalR Java client from GitHub using Eclipse with Android ADT Bundle and Java.

I am new to Java and SignalR. I have a SignalR Hub and JavaScript client that works in HTML, and it can send and receive messages. But I can not go to the next step and get Java-Client to work in Eclipse / Java / Android. I am not sure how to register to receive a broadcast message. When I send a message using an HTML / JavaScript page, I do not receive the message in an Android application in an emulator in Eclipse. I can

I have the following ASP.NET SignalR Hub.cs file:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using Microsoft.AspNet.SignalR; using Microsoft.AspNet.SignalR.Hubs; using System.Threading.Tasks; public class PtThroughputHub : Hub { public void SendAll(string message) { Clients.All.broadcastMessage(message); } public void SendGroup(string groupName, string message) { Clients.Group(groupName).broadcastMessage(message); } public Task JoinGroup(string groupName) { return Groups.Add(Context.ConnectionId, groupName); } public Task LeaveGroup(string groupName) { return Groups.Remove(Context.ConnectionId, groupName); } } 

In JavaScript, I can also use the Hubs.js file, which is automatically generated from the ASP.NET Hub.cs node to send and receive messages through the hub.

 <script src="Scripts/jquery-2.1.0.min.js"></script> <!--Reference the SignalR library. --> <script src="Scripts/jquery.signalR-2.0.3.min.js"></script> <!--Reference the autogenerated SignalR hub script. --> <script src="signalr/hubs"></script> $(function () { $.connection.hub.logging = true; // Declare a proxy to reference the hub. var ptThroughput = $.connection.ptThroughputHub; // Create a function that the hub can call to broadcast messages. ptThroughput.client.broadcastMessage = function (message) { alert(message); }; // Start the connection. $.connection.hub.start() .done(function () { alert('Successfully connected as ' + $.connection.hub.id); ptThroughput.server.joinGroup("Group1"); $('#btnSend').click(function () { // Call the Send method on the hub. ptThroughput.server.sendGroup('Group1', 'My Message Test Here!'); }) }) .fail(function () { $("#connection").text('Can not connect to SignalR hub!'); }); }); 

However, in Eclipse / Java / Android, I'm not sure which code I need to use to receive a broadcast message. I can start the connection and get the connection identifier at the end of the following code sample, but I'm stuck there. Do I need to use .on () or .subscribe () somehow? When I run this code, I see the connection identifier, but I cannot call the broadcast message from the html / JavaScript page. Nothing is received, and I do not receive anything. Does anyone know what I'm missing here?

 import microsoft.aspnet.signalr.client.SignalRFuture; import microsoft.aspnet.signalr.client.hubs.HubConnection; import microsoft.aspnet.signalr.client.hubs.HubProxy; import microsoft.aspnet.signalr.client.hubs.SubscriptionHandler; //Setup connection String server = "http://Servername/Applications/ThroughputMobile/"; HubConnection connection = new HubConnection(server); HubProxy proxy = connection.createHubProxy("ptThroughputHub"); //--HERE IS WHERE I AM NOT SURE HOW TO SET IT UP TO RECEIVE A NOTIFICATION--- //Setup to receive broadcast message proxy.on("broadcastMessage",new SubscriptionHandler() { @Override public void run() { Log.i("Test Message", "broadcastMessage received..."); } }); //--------------------------------------------------------------------------- //Start connection SignalRFuture<Void> awaitConnection = connection.start(); try { awaitConnection.get(); } catch (InterruptedException e) { //Log Connection ID Log.i("Test Message", "Connection ID = " + connection.getConnectionId()); 

So, am I using proxy.on () to set up things to get notified, or something else? And what line do I pass to the method? "broadcastMessage" or maybe "sendAll" in my case? Any help would be greatly appreciated.

+6
source share
1 answer

I did not call the JoinGroup() method from the SignalR shown in the question. It makes sense that you need to join the hub in order to be able to receive the broadcast. Therefore, in my background service onStart() I call invoke("JoinGroup", "Group1") . Then I call the on () method to configure the handler of the received message. Group1 is the configurable group name that the hub uses to separate broadcast messages, so different groups can use the same hub. See full code and commented block of solutions below. Hope this saves someone for a while!

Here's what helped me with the complete Android background service code:

 //Invoke JoinGroup to start receiving broadcast messages proxy.invoke("JoinGroup", "Group1"); //Then call on() to handle the messages when they are received. proxy.on( "broadcastMessage", new SubscriptionHandler1<String>() { @Override public void run(String msg) { Log.d("result := ", msg); } }, String.class); 

Here is the full Android background service code:

 import java.util.concurrent.ExecutionException; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; import android.os.IBinder; import android.widget.Toast; import microsoft.aspnet.signalr.client.*; import microsoft.aspnet.signalr.client.hubs.*; public class NotifyService extends Service { @Override public void onCreate() { super.onCreate(); } @SuppressWarnings("deprecation") @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); Toast.makeText(this, "Service Start", Toast.LENGTH_LONG).show(); String server = "http://Servername/Applications/ThroughputMobile/"; HubConnection connection = new HubConnection(server); HubProxy proxy = connection.createHubProxy("ptThroughputHub"); SignalRFuture<Void> awaitConnection = connection.start(); try { awaitConnection.get(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); } //--HERE IS MY SOLUTION----------------------------------------------------------- //Invoke JoinGroup to start receiving broadcast messages proxy.invoke("JoinGroup", "Group1"); //Then call on() to handle the messages when they are received. proxy.on( "broadcastMessage", new SubscriptionHandler1<String>() { @Override public void run(String msg) { Log.d("result := ", msg); } }, String.class); //-------------------------------------------------------------------------------- } @Override public void onDestroy() { super.onDestroy(); } @Override public IBinder onBind(Intent arg0) { return null; } } 
+8
source

Source: https://habr.com/ru/post/975902/


All Articles