jquery signalr客户端工作 – 但在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); } }); 

但是,当我尝试使用SignalR Java Client在java android中模拟此代码时,我得到了大量的日志输出(吨),并且没有连接完成。 它到达awaitConnection.get();行的调试awaitConnection.get(); 并且从不打印第二行调试,而是打印无数(数千)半乱码线(它不是管道,它就像是某种“SSL握手”,但它没有做任何事情,只是重复记录同样的事情,非常奇怪)无论如何它永远不会运行“我的”调试的第二行

 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 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(){ @Override public void run( String status ){ Log.v("CONANDispatch :=", status); } }, String.class); } } 

任何人都可以帮助我将工作的jQuery SignalR客户端代码转换为可用的Java客户端代码吗? 我没有关于集线器的任何信息,所以我不知道代理或函数名称,我想看到一切(如jQuery)。

编辑

为了测试一些东西,我改变了我用来说的原始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收到消息:”根本不出现

    由于您不知道集线器名称,因此必须直接在连接对象上使用句柄事件(就像在js客户端中一样)。

    这是一个示例代码,可以帮助您入门(注意,这不是测试,只是从头顶写):

     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() { @Override public void run(Void obj) throws Exception { System.out.println("Connected"); } }); } 

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

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