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> <script src="Scripts/jquery.signalR-2.0.3.min.js"></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.