如何使用Microsoft Java客户端接收信号广播消息?

如何使用Microsoft Java客户端接收信号广播消息?,java,android,eclipse,signalr,Java,Android,Eclipse,Signalr,我需要将消息从ASP.NET服务器推送到Android设备,以便在记录状态发生更改时发出通知。因此,我想使用GitHub的新MicrosoftSignalrJava客户端,使用Eclipse和Android ADT捆绑包以及Java 我不熟悉Java和SignalR。我有SignalrHub和JavaScript客户端在HTML中工作,它可以发送和接收消息。但我无法进入下一步,让Java客户机在Eclipse/Java/Android中工作。我不知道如何注册以接收广播消息。当我使用HTM

我需要将消息从ASP.NET服务器推送到Android设备,以便在记录状态发生更改时发出通知。因此,我想使用GitHub的新MicrosoftSignalrJava客户端,使用Eclipse和Android ADT捆绑包以及Java

我不熟悉Java和SignalR。我有SignalrHub和JavaScript客户端在HTML中工作,它可以发送和接收消息。但我无法进入下一步,让Java客户机在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!'); });

});

$(函数(){
$.connection.hub.logging=true;
//声明代理以引用中心。
var ptThroughput=$.connection.ptthroughputhhub;
//创建一个中心可以调用以广播消息的函数。
ptThroughput.client.broadcastMessage=函数(消息){
警报(信息);
};
//启动连接。
$.connection.hub.start()
.done(函数(){
警报('作为'+$.connection.hub.id'成功连接);
ptThroughput.server.joinGroup(“Group1”);
$('#btnSend')。单击(函数(){
//在集线器上调用Send方法。
ptThroughput.server.sendGroup('Group1','My Message Test Here!');
})
})
.fail(函数(){$(“#连接”).text('cannotconnecttosignarhub!');});
});
然而,在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());
导入microsoft.aspnet.signalr.client.SignalRFuture;
导入microsoft.aspnet.signal.client.hubs.HubConnection;
导入microsoft.aspnet.signal.client.hubs.HubProxy;
导入microsoft.aspnet.signal.client.hubs.SubscriptionHandler;
//设置连接
字符串服务器=”http://Servername/Applications/ThroughputMobile/";
HubConnection连接=新的HubConnection(服务器);
HubProxy=connection.createHubProxy(“ptThroughputHub”);
//--在这里,我不确定如何设置它以接收通知---
//设置接收广播消息
在(“broadcastMessage”,new SubscriptionHandler(){
@凌驾
public void run(){
Log.i(“测试消息”,“接收到广播消息…”);
}
});
//---------------------------------------------------------------------------
//启动连接
SignalRFuture awaitConnection=connection.start();
试一试{
等待连接。get();
}捕捉(中断异常e){
//日志连接ID
Log.i(“测试消息”,“连接ID=“+Connection.getConnectionId());

同样,我是否使用proxy.on()设置接收广播通知或其他内容?我应该向该方法传递什么字符串?“broadcastMessage”或“sendAll”?如果有任何帮助,将不胜感激。

我没有调用
JoinGroup()问题中显示的
信号器的
集线器中的
方法。您需要加入集线器才能接收广播,这很有意义。因此,在我的后台服务
onStart()
中,我调用
调用(“JoinGroup”,“Group1”)
。然后调用on()方法来设置接收到的消息的处理程序。
Group1
是一个可配置的组名,集线器使用它来分割广播,以便不同的组可以使用同一集线器。请参阅下面的完整代码和注释解决方案块。希望这能节省一些时间

下面是帮助我的完整Android后台服务代码:

//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;
    }
}
//调用JoinGroup以开始接收广播消息
代理。调用(“JoinGroup”、“Group1”);
//然后调用()来处理接收到的消息。
proxy.on(“broadcastMessage”,newsubscriptionhandler1()
{
@凌驾
公共无效运行(字符串消息){
Log.d(“结果:=”,msg);
}
},String.class);
以下是完整的Android后台服务代码:

//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;
    }
}
import java.util.concurrent.ExecutionException;
导入android.app.Notification;
导入android.app.NotificationManager;
导入android.app.pendingent;
导入android.app.Service;
导入android.content.Context;
导入android.content.Intent;
导入android.os.IBinder;
导入android.widget.Toast;
导入microsoft.aspnet.signal.client.*;
导入microsoft.aspnet.signal.client.hubs.*;
公共类NotifyService扩展了服务{
@凌驾
public void onCreate(){
super.onCreate();
}
@抑制警告(“弃用”)
@凌驾
公共void onStart(Intent Intent,int startId){
super.onStart(intent,startId);
Toast.makeText(这是“服务开始”,Toast.LENGTH_LONG).show();
字符串服务器=”http://Servername/Applications/ThroughputMobile/";
HubConnection连接=新的HubConnection(服务器);
HubProxy=connection.createHubProxy(“ptThroughputHub”);
SignalRFuture awaitConnection=connection.start();
试一试{
等待康涅狄格州