工程─但在java中无法进行工作的jQuery signalr客户端 [英] jquery signalr client works- but in java it cannot be made to work

查看:252
本文介绍了工程─但在java中无法进行工作的jQuery signalr客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个SignalR枢纽。下面的jQuery的code连接,并妥善处理SignalR客户端科尔多瓦呼的事件成功。

We have a SignalR hub. The following jQuery code connects and properly processes SignalR client "call" events successfully on cordova.

        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客户端效仿这一code在java的机器人,我得到一个很多日志输出(吨),并没有连接有史以来完成。它尽可能得到作为调试在行 awaitConnection.get(); 从不打印出的第二线半乱码的行调试,而不是它打印不已(千)(它不是管道,就好像它是某种形式的SSL握手,但它没有做任何事情,但记录了同样的事情反反复复,很奇怪),反正它永远不会运行的二号线我的调试

However when I try to emulate this code in java android using the SignalR Java Client, I get a lot of log output (tonnes), and no connection ever finishes. it gets as far as the debug at line awaitConnection.get(); and never prints the second line of debug, instead it prints endless (thousands) of lines of semi gibberish (it's not piping, it's like it's some sort of "SSL handshake" but it's not doing anything but logging the same thing repeatedly, very odd) anyway it never runs the 2nd line of "my" debug

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客户端code转化为可用的Java客户端code?我没有在轮毂上的任何信息,所以无法知道代理或函数名,我想看到​​的一切(如jQuery的)。

Can anyone help me translate the working, jQuery SignalR client code into usable java client code? I don't have any information on the hub so I cannot know the proxy or the function names, I'd like to see everything (like the jQuery).

修改

要测试的东西,我已经改变了我原来的jQuery code它使用说

To test things, I have altered my original jquery code it use to say

console.log("onDeviceReady run");

现在它说:

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

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

when I do this this is what the jquery returns

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相当于在

however none of this appears inside the java equivalent

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收到的消息:没有出现在所有

i.e. the text "RAW received message:" doesn't appear at all

推荐答案

既然你不知道枢纽的名字,你必须直接使用的连接对象处理事件(如你在JS客户端做的)。

Since you don't know hub name, you have to use handle events on connection object directly (like you do in js client).

下面是它可以让你开始(注意,这不是测试,笔试只是从我的头顶)样品code:

Here is sample code which could get you started (note, this is not tested, written just from top of my head):

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");
            }
        });
}

另外,我建议你检查Java示例聊天客户端,它可以在这里找到:<一href="https://github.com/SignalR/java-samples/blob/master/signalr-sample-chat/src/microsoft/aspnet/signalr/samples/chat/Program.java" rel="nofollow">https://github.com/SignalR/java-samples/blob/master/signalr-sample-chat/src/microsoft/aspnet/signalr/samples/chat/Program.java

大多数code我张贴是基于之一。

Most of code I posted is based on that one.

这篇关于工程─但在java中无法进行工作的jQuery signalr客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆