I need to push message from an ASP.NET server to Android devices for a notification whenever the status of a record is changed. So I would like to use the new Microsoft SignalR Java-Client from GitHub using Eclipse with Android ADT Bundle and Java.
- https://github.com/SignalR/java-client
- http://whathecode.wordpress.com/2014/03/20/getting-started-with-the-java-signalr-sdk/
I am new to Java and SignalR. I have the SignalR Hub and JavaScript client working in HTML and it can send and receive messages. But I am not able to go to the next step and get the Java-Client to work in Eclipse/Java/Android. I am not sure how to register to receive the broadcast message. When I send a message using the HTML/JavaScript page, I do not receive the message in the Android application, in the emulator, in Eclipse. I am able to
I have the following ASP.NET SignalR Hub.cs file set up:
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 am also able to use the Hubs.js file that is auto-generated from the ASP.NET Hub.cs 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 am not sure what code I need to use to receive a broadcast message. I am able to start the connection and receive a connectionID at the end of the following code example but I am stuck there. Do I need to use .on() or .subscribe() somehow? When I run this code I see a connection ID logged but I am not able to trigger the broadcast message from the html/JavaScript page. Nothing is received and I don't get an error anywhere. Does anyone know what I might be 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 again, do I use proxy.on() to set up to receive broadcast notifications or something else? And what string do I pass to the method? "broadcastMessage" or perhaps "sendAll" in my case? Any help would be greatly appreciated.