SignalR Java客户端的`hubConnection.on()`方法在接收到数据后未触发任何方法。

huangapple go评论79阅读模式
英文:

SignalR java client hubConnection.on() method not trigger any method after receiving data

问题

我在我的安卓应用程序中使用SignalR接收实时数据。当我的"hubConnection.on()"方法启动时,它成功接收数据,但在这个方法内部,它不允许我使用那些数据。就像当我写下这样的代码:

hubConnection = HubConnectionBuilder.create("http://www.example.com").build();

hubConnection.start();

hubConnection.on("ReceiveLocation", (lat, lng) -> {

    Log.i("dataCheck", "-> " + Double.parseDouble(lat));
    Log.i("dataCheck", "-> " + Double.parseDouble(lng));
    if(hubConnection.getConnectionState() == HubConnectionState.DISCONNECTED)
        hubConnection.start();

}, String.class, String.class);

在上面的情况下,数据成功接收并且也在日志中打印出来。

但是当我尝试使用这些"lat"和"lng"变量时,比如我尝试显示一个带有"lat"值的Toast,数据不会在这个方法中接收到,而且Toast也不会显示在屏幕上。就像我使用以下代码:

hubConnection = HubConnectionBuilder.create("http://www.example.com").build();

hubConnection.start();

hubConnection.on("ReceiveLocation", (lat, lng) -> {

    Toast.makeText(OrderTrackingActivity.this, "->>>" + Double.parseDouble(lat), Toast.LENGTH_SHORT).show();

    Log.i("dataCheck", "-> " + Double.parseDouble(lat));
    Log.i("dataCheck", "-> " + Double.parseDouble(lng));
    if(hubConnection.getConnectionState() == HubConnectionState.DISCONNECTED)
        hubConnection.start();

}, String.class, String.class);

在第二段代码中,数据不会被接收,Toast也不会显示。请为我提供解决这个问题的可能解决方案。我需要使用这些传递过来的数据。

根据建议,我尝试将"lat"和"lng"(.on()函数的变量)的值存储到我自己定义的变量中,然后将这些变量传递给我的函数名为"myFuntion",就像这样:

hubConnection.on("ReceiveLocation", (lat, lng) -> {

    varbl1 = lat;
    varbl2 = lng;
    Log.i("dataCheck", "-> " + Double.parseDouble(lat));
    Log.i("dataCheck", "-> " + Double.parseDouble(lng));

    myFuntion(varbl1, varbl2);

    if(hubConnection.getConnectionState() == HubConnectionState.DISCONNECTED)
        hubConnection.start();

}, String.class, String.class);

然后在"myFuntion"函数中,我将这些值打印到日志中,成功地打印出这些值,但是当我使用这些值,比如在Toast中显示或者执行其他操作时,SignalR的"hubConnection.on()"函数停止工作。以下是我的"myFuntion"方法的代码:

public void myFuntion(String value, String value2){
       
    Toast.makeText(this, "" + value, Toast.LENGTH_SHORT).show();
    
}
英文:

I use SignalR in my android app to receive real time data. When my "hubConnection.on()" method start it receive data successfully but inside this method it not allow me to use that data. Like when i write code like this :

   hubConnection = HubConnectionBuilder.create("http://www.example.com").build();

    hubConnection.start();

    hubConnection.on("ReceiveLocation", (lat, lng) -> {

        Log.i("dataCheck", "-> "+Double.parseDouble(lat));
        Log.i("dataCheck", "-> "+Double.parseDouble(lng));
        if(hubConnection.getConnectionState() == HubConnectionState.DISCONNECTED)
            hubConnection.start();

    }, String.class, String.class);

The this above case data receive successfully and also print in Log.

But when i try use this "lat" , "lng" variables, like i try to show a toast with "lat" value, data not receive this method and also toast not show on screen. Like i use code like this:

        hubConnection = HubConnectionBuilder.create("http://www.example.com").build();

    hubConnection.start();

    hubConnection.on("ReceiveLocation", (lat, lng) -> {


        Toast.makeText(OrderTrackingActivity.this, "->>>"+Double.parseDouble(lat), Toast.LENGTH_SHORT).show();


        Log.i("dataCheck", "-> "+Double.parseDouble(lat));
        Log.i("dataCheck", "-> "+Double.parseDouble(lng));
        if(hubConnection.getConnectionState() == HubConnectionState.DISCONNECTED)
            hubConnection.start();

    }, String.class, String.class);

In this 2nd code the data not receive and toast not show. Please suggest me the possible solutions to solve this problem. I need to use this coming data.

Follow a suggestion i try to store the value of "lat", "lng" (variables of .on() funtion) into my own define variables and then pass these my variables to my function name "myFuntion" like this :

 hubConnection.on("ReceiveLocation", (lat, lng) -> {

        varbl1 = lat;
        varbl2 = lng;
    Log.i("dataCheck", "-> "+Double.parseDouble(lat));
    Log.i("dataCheck", "-> "+Double.parseDouble(lng));

    myFuntion(varbl1 , varbl2);

    if(hubConnection.getConnectionState() == HubConnectionState.DISCONNECTED)
        hubConnection.start();

}, String.class, String.class);

Then in "myFuntion" i Log the values and successfully values print but still when i use that values either show in Toast or do something else the signalR "hubConnection.on()" funtion stop working. Here is my "myFuntion" method code:

 public void myFuntion(String value, String value2){
   
    Toast.makeText(this, ""+value, Toast.LENGTH_SHORT).show();

}

答案1

得分: 1

[**从 Hub 调用客户端方法**][1]

使用 `hubConnection.on` 定义客户端上的方法供 Hub 调用在构建连接后但在启动连接前定义这些方法

示例

```java
hubConnection = HubConnectionBuilder.create("http://www.example.com").build();

hubConnection.on("ReceiveLocation", (lat, lng) -> {
    Log.i("dataCheck", "-> " + Double.parseDouble(lat));
    Log.i("dataCheck", "-> " + Double.parseDouble(lng));
    if (hubConnection.getConnectionState() == HubConnectionState.DISCONNECTED)
        hubConnection.start();
}, String.class, String.class);

hubConnection.start();

注意
如果你不知道从服务器发送到客户端的数据类型,可以使用 Object.class

如果你想要在 UI 中显示 toast 或进行 UI 更改,可以使用以下方法:

hubConnection.on("ReceiveLocation", (data) -> {
    this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(MainActivity.this, data.toString(), Toast.LENGTH_SHORT).show();
            txtPath.setText(data.toString());
        }
    });
}, Object.class);

<details>
<summary>英文:</summary>

[**Call client methods from hub**][1]

Use hubConnection.on to define methods on the client that the hub can call. Define the methods **after building** but **before starting** the connection.

example:

    hubConnection = HubConnectionBuilder.create(&quot;http://www.example.com&quot;).build();

    hubConnection.on(&quot;ReceiveLocation&quot;, (lat, lng) -&gt; {
        Log.i(&quot;dataCheck&quot;, &quot;-&gt; &quot;+Double.parseDouble(lat));
        Log.i(&quot;dataCheck&quot;, &quot;-&gt; &quot;+Double.parseDouble(lng));
        if(hubConnection.getConnectionState() == HubConnectionState.DISCONNECTED)
            hubConnection.start();

    }, String.class, String.class);


    hubConnection.start();

&gt; **Note**:
If you do not know the type of data sent from the server to the client, use **Object.class**.

Use this method if you want to toast or have changes in UI:

    hubConnection.on(&quot;ReceiveLocation&quot;, (data) -&gt; {
            this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(MainActivity.this, data.toString(), Toast.LENGTH_SHORT).show();
                    txtPath.setText(data.toString());
                }
            });
    },Object.class);


  [1]: https://learn.microsoft.com/en-us/aspnet/core/signalr/java-client?view=aspnetcore-7.0#call-client-methods-from-hub

</details>



# 答案2
**得分**: 0

这个问题是由于 `UI 线程` 而不是 `Signalr` 引起的,你可以尝试以下代码:

如果你在一个 `Activity` 中使用:

```java
hubConnection.on("ReceiveTxt", (txt) -> {
    this.runOnUiThread(new Runnable() {
        public void run() {
            Toast.makeText(this, ">>> :" + txt, Toast.LENGTH_SHORT).show();
        }
    });
}, String.class);

如果你在其他上下文中使用:

hubConnection.on("ReceiveTxt", (txt) -> {
    Activity activity = (Activity) context;
    activity.runOnUiThread(new Runnable() {
        public void run() {
            Toast.makeText(context, ">>> :" + txt, Toast.LENGTH_SHORT).show();
        }
    });
}, String.class);
英文:

This Problem is because of the UI Thread not Signalr, you can try this code

            hubConnection.on(&quot;ReceiveTxt&quot;,(txt)-&gt; {
            Activity activity = (Activity) context;
            activity.runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(context, &quot;-&gt;&gt;&gt; :&quot; + txt, Toast.LENGTH_SHORT).show();
                }
            });
        }, String.class);

If you are in an Activity use this

hubConnection.on(&quot;ReceiveTxt&quot;,(txt)-&gt; {
        this.runOnUiThread(new Runnable() {
            public void run() {
                Toast.makeText(this, &quot;-&gt;&gt;&gt; :&quot; + txt, Toast.LENGTH_SHORT).show();
            }
        });
    }, String.class);

huangapple
  • 本文由 发表于 2020年9月15日 19:24:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63900805.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定