我需要将消息从ASP.NET服务器推送到
Android设备,以便在记录状态发生变化时发出通知.所以我想使用来自GitHub的新的Microsoft SignalR Java-Client,使用Eclipse与Android ADT Bundle和Java.
> https://github.com/SignalR/java-client
> http://whathecode.wordpress.com/2014/03/20/getting-started-with-the-java-signalr-sdk/
我是Java和SignalR的新手.我有使用HTML工作的SignalR Hub和JavaScript客户端,它可以发送和接收消息.但是我无法进入下一步并让Java-Client在Eclipse / Java / Android中运行.我不知道如何注册接收广播消息.当我使用HTML / JavaScript页面发送消息时,我没有在Android应用程序中,在模拟器中,在Eclipse中收到消息.我能够
我有以下ASP.NET SignalR Hub.cs文件设置:
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); } }
在JavaScript中,我还可以使用从ASP.NET Hub.cs自动生成的Hubs.js文件来通过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!'); }); });
但是,在Eclipse / Java / Android中,我不确定需要使用哪些代码来接收广播消息.我能够启动连接并在以下代码示例的末尾接收connectionID但我被困在那里.我是否需要以某种方式使用.on()或.subscribe()?当我运行此代码时,我看到记录了连接ID,但我无法从html / JavaScript页面触发广播消息.没有收到任何东西,我不会在任何地方收到错误.有谁知道我在这里可能会缺少什么?
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());
那么,我是否再次使用proxy.on()来设置接收广播通知或其他内容?我传递给方法的字符串是什么?在我的情况下,“broadcastMessage”或者也许是“sendAll”?任何帮助将不胜感激.
我没有从问题中显示的SignalR Hub调用JoinGroup()方法.您需要加入Hub才能接收广播是有道理的.所以在我的后台服务onStart()中,我调用了invoke(“JoinGroup”,“Group1”).然后我调用on()方法来设置收到的消息的处理程序. Group1是一个可配置的组名,集线器使用该名称来拆分广播,因此不同的组可以使用相同的Hub.请参阅下面的完整代码和注释解决方案块.希望这能节省一些时间!
//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);
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; } }