jQuery信号客户端工作 – 但在Java中,它不能工作

我们有一个SignalR中心。 以下jQuery代码连接并正确处理cordova上的SignalR客户端“呼叫”事件。

var connectionURL = "https://SOMEURL.azurewebsites.net/message"; connection = $.connection(connectionURL); connection.start().done(function () { console.log("Connected to hub again"); }); connection.disconnected(function () { setTimeout(function () { connection.start().done(function () { console.log("Disconnected and Connected to hub again"); }); }, 5000); }); connection.stateChanged(function (change) { if (change.newState == $.signalR.connectionState.reconnecting) { } else if (change.newState == $.signalR.connectionState.connected) { } else if (change.newState == $.signalR.connectionState.disconnected) { }// else if }); connection.received(function (data) { connectId = connection.id + ""; console.log("onDeviceReady run"); // call the function to parse the data if (data.PayloadType == "Dispatch") { dataDispatch(data); } if (data.PayloadType == "ConnectionAcknowledge") { sendConnectionAcknowledge(data); } }); 

但是,当我尝试在java android中使用SignalR Java客户端来模拟这段代码时,我得到了大量的日志输出(吨),没有连接完成。 它可以在线awaitConnection.get();执行debuggingawaitConnection.get(); 并且从不打印第二行debugging,而是打印无数(半数)的半字节线(不是pipe道,就像是某种“SSL握手”),但它不会做任何事情,只是反复地logging相同的事情,很奇怪)无论如何它永远不会运行“我的”debugging的第二行

 package com.some.thing; import android.util.Log; import java.util.concurrent.ExecutionException; import microsoft.aspnet.signalr.client.Platform; import microsoft.aspnet.signalr.client.SignalRFuture; import microsoft.aspnet.signalr.client.http.android.AndroidPlatformComponent; import microsoft.aspnet.signalr.client.hubs.HubConnection; import microsoft.aspnet.signalr.client.hubs.HubProxy; import microsoft.aspnet.signalr.client.hubs.SubscriptionHandler1; public class SignalRClient { public static void startConnection() { Platform.loadPlatformComponent(new AndroidPlatformComponent()); String host = "https://SOMEURL.azurewebsites.net/message"; HubConnection connection = new HubConnection(host); HubProxy hub = connection.createHubProxy( "IDoNoHaveThisNorKnowIt" ); SignalRFuture<Void> awaitConnection = connection.start(); try { Log.v("CONANSignalR :=", "CONNECTING"); awaitConnection.get(); Log.v("CONANSignalR :=", "CONNECTED"); } catch (InterruptedException e) { // Handle ... } catch (ExecutionException e) { // Handle ... } hub.on("IDoNotKnowThisEither", new SubscriptionHandler1<String>(){ @Override public void run( String status ){ Log.v("CONANDispatch :=", status); } }, String.class); } } 

任何人都可以帮我翻译工作,jQuery的SignalR客户端代码成可用的Java客户端代码? 我没有关于中心的任何信息,所以我不知道代理或函数名称,我想看到的一切(如jQuery)。

编辑

为了testing,我改变了原来的jQuery代码

 console.log("onDeviceReady run"); 

现在说

 console.log("SignalR Raw Data:" + JSON.stringify(data)); 

当我这样做,这是jQuery的回报

 SignalR Raw Data:{"ConnectionId":"9c4b4ba5-cb6e-4dcb-8df9-069cbf749873","OrderId":null,"SenderId":null,"PayloadType":"ConnectionAck","Message":"Welcome, you are connected to the Hub!","Payload":null,"Initiator":"HUB","Version":null} 

然而,没有一个出现在java等价的内部

 connection.received(new MessageReceivedHandler() { @Override public void onMessageReceived(JsonElement json) { System.out.println("RAW received message: " + json.toString()); // ADD HANDLING OF RECEIVED IN HERE } }); 

即文本“RAW received message:”根本不出现

    既然你不知道中心名称,你必须直接使用连接对象上的句柄事件(就像你在js客户端中那样)。

    这里是示例代码,可以让你开始(注意,这是没有testing,从头开始写):

     public static void startConnection() { Platform.loadPlatformComponent(new AndroidPlatformComponent()); String host = "https://SOMEURL.azurewebsites.net/message"; HubConnection connection = new HubConnection(host); // subscribe to received - equal to `connection.received(function (data)` from javascript connection.received(new MessageReceivedHandler() { @Override public void onMessageReceived(JsonElement json) { System.out.println("RAW received message: " + json.toString()); // ADD HANDLING OF RECEIVED IN HERE } }); // equal to `connection.disconnected(function ()` from javascript connection.closed(new Runnable() { @Override public void run() { // ADD CODE TO HANDLE DISCONNECTED EVENT } }); // equal to `connection.stateChanged(function (change)` connection.stateChanged(new StateChangedCallback() { @Override public void stateChanged(ConnectionState oldState, ConnectionState newState) { // ADD CODE TO HANDLE STATE CHANGES } }); // start the connection connection.start() .done(new Action<Void>() { @Override public void run(Void obj) throws Exception { System.out.println("Connected"); } }); } 

    此外,我build议你检查示例聊天客户端的Java可以在这里find: https : //github.com/SignalR/java-samples/blob/master/signalr-sample-chat/src/microsoft/aspnet/signalr/样本/聊天/ Program.java

    我发布的大部分代码都是基于该代码的。