eclipse 如何使用 Microsoft Java-Client 接收 SignalR 广播消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26067685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to receive SignalR broadcast message using Microsoft Java-Client?
提问by Andy
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.
我需要将消息从 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/
- 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
我是 Java 和 SignalR 的新手。我有使用 HTML 的 SignalR Hub 和 JavaScript 客户端,它可以发送和接收消息。但是我无法进行下一步并使 Java 客户端在 Eclipse/Java/Android 中工作。我不确定如何注册以接收广播消息。当我使用 HTML/JavaScript 页面发送消息时,我没有在 Android 应用程序、模拟器和 Eclipse 中接收到消息。我能够
I have the following ASP.NET SignalR Hub.cs file set up:
我设置了以下 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);
}
}
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.
在 JavaScript 中,我还可以使用从 ASP.NET Hub.cs 自动生成的 Hubs.js 文件通过集线器发送和接收消息。
<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?
但是,在 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());
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.
那么,我是否使用 proxy.on() 来设置接收广播通知或其他什么?我传递给方法的字符串是什么?在我的情况下是“broadcastMessage”还是“sendAll”?任何帮助将不胜感激。
回答by Andy
I was not invoking the JoinGroup()
method from the SignalR
Hub shown in the Question. It makes sense that you need to join the Hub to be able to receive a broadcast. So in my Background Service onStart()
, I call invoke("JoinGroup", "Group1")
. Then I call the on() method to set up the handler of the message that gets received. Group1
is a configurable group name that the hub uses to split out broadcasts so different groups can use the same Hub. See the full code and commented solution block below. Hope this save someone some time!
我没有JoinGroup()
从SignalR
问题中显示的中心调用方法。您需要加入集线器才能接收广播,这是有道理的。所以在我的后台服务中onStart()
,我调用invoke("JoinGroup", "Group1")
. 然后我调用 on() 方法来设置接收到的消息的处理程序。 Group1
是一个可配置的组名,集线器用于拆分广播,以便不同的组可以使用相同的集线器。请参阅下面的完整代码和注释解决方案块。希望这可以节省一些时间!
Here is what helped me followed by the full Android Background Service code:
以下是帮助我遵循完整 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);
Here is the full Android Background Service code:
这是完整的 Android 后台服务代码:
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;
}
}