SignalR Java 客户端无法在 Android 12 中运行

发布于 2025-01-16 10:55:29 字数 3490 浏览 0 评论 0原文

我在我的 Android 项目中实现了 SignalR Java 客户端。它在 Android 版本 6 到 11 上运行良好,但在 Android 12 上失败,我仅在 Android 12 上收到此错误 java.net.SocketException: 套接字已关闭 [Ljava.lang.StackTraceElement;@e95116d,这是我的代码:

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.microsoft.signalr.HubConnection;
import com.microsoft.signalr.HubConnectionBuilder;
import com.microsoft.signalr.OnClosedCallback;

import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import timber.log.Timber;

public class SignalRChatService {

Timer timer = new Timer();

public void startTimer()
{
    timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            // Your code here
            Timer_Elapsed();
        }
    }, 1000, 2000);
}

private void Timer_Elapsed()
{
    try
    {
        module.checkNetwork(context);

        if(!Connected)
        {
            Init();
        }
    }
    catch (Exception e)
    {
        Connected = false;
    }
}

private HubConnection _connection;

public boolean Connected;

public String instanceName;
public int instanceProfileId;

private boolean connecting;

private SignalRMessageReceivedEvents signalRMessageReceivedEvents;

private final Gson gson;

private final Context context;

public SignalRChatService(Context context, String instanceName, int instanceProfileId, SignalRMessageReceivedEvents signalRMessageReceivedEvents) {
    this.instanceName = instanceName;
    this.instanceProfileId = instanceProfileId;
    this.connecting = false;
    this.context = context;
    this.signalRMessageReceivedEvents = signalRMessageReceivedEvents;
    gson = new GsonBuilder().setPrettyPrinting().create();
}

public void Stop()
{
    try
    {
        _connection.stop();
    }
    catch(Exception ignored)
    {}
}

public void Init()
{
    if (connecting)
    {
        return;
    }
    try
    {
        connecting = true;

        _connection  = HubConnectionBuilder.create("https://" + instanceName.trim().toLowerCase() + ".com").build();

        try
        {
            _connection.start();
            Connected = true;
        }
        catch (Exception e)
        {
            Connected = false;
            Timber.e("SignalR Push connect: %s", e.getLocalizedMessage());
            return;
        }

        _connection.on("Message", (message) ->
        {
            try
            {
                // Separate this code affterwads
                JSONObject messageObject =  new JSONObject(message);
                String Messagetype = (String) messageObject.get("messageType");
                HandleWebSocketMessage(Messagetype, message);
            }
            catch (Exception ignored)
            {

            }
        }, String.class);

        _connection.onClosed(new OnClosedCallback() {
            @Override
            public void invoke(Exception exception) {
                handleClosed();
            }
        });

    }
    catch (Exception ignored)
    {

    }
    finally
    {
        connecting = false;
        startTimer();
    }

}

private void handleClosed()
{
    try
    {
        TimeUnit.MILLISECONDS.sleep(100);
    }
    catch (Exception ignored)
    {

    }
    Init();
}
}

我尝试升级到 SignalR Java 客户端的最新版本

 implementation "com.microsoft.signalr:signalr:6.0.3"

,但它仍然无法正常工作。它只是不会收到任何信号。

I implemented SignalR Java Client in my Android Project. It works well on Android Versions 6 to 11 but fails on Android 12, I'm getting this error only on Android 12 java.net.SocketException: Socket closed [Ljava.lang.StackTraceElement;@e95116d, here is my code:

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.microsoft.signalr.HubConnection;
import com.microsoft.signalr.HubConnectionBuilder;
import com.microsoft.signalr.OnClosedCallback;

import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import timber.log.Timber;

public class SignalRChatService {

Timer timer = new Timer();

public void startTimer()
{
    timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            // Your code here
            Timer_Elapsed();
        }
    }, 1000, 2000);
}

private void Timer_Elapsed()
{
    try
    {
        module.checkNetwork(context);

        if(!Connected)
        {
            Init();
        }
    }
    catch (Exception e)
    {
        Connected = false;
    }
}

private HubConnection _connection;

public boolean Connected;

public String instanceName;
public int instanceProfileId;

private boolean connecting;

private SignalRMessageReceivedEvents signalRMessageReceivedEvents;

private final Gson gson;

private final Context context;

public SignalRChatService(Context context, String instanceName, int instanceProfileId, SignalRMessageReceivedEvents signalRMessageReceivedEvents) {
    this.instanceName = instanceName;
    this.instanceProfileId = instanceProfileId;
    this.connecting = false;
    this.context = context;
    this.signalRMessageReceivedEvents = signalRMessageReceivedEvents;
    gson = new GsonBuilder().setPrettyPrinting().create();
}

public void Stop()
{
    try
    {
        _connection.stop();
    }
    catch(Exception ignored)
    {}
}

public void Init()
{
    if (connecting)
    {
        return;
    }
    try
    {
        connecting = true;

        _connection  = HubConnectionBuilder.create("https://" + instanceName.trim().toLowerCase() + ".com").build();

        try
        {
            _connection.start();
            Connected = true;
        }
        catch (Exception e)
        {
            Connected = false;
            Timber.e("SignalR Push connect: %s", e.getLocalizedMessage());
            return;
        }

        _connection.on("Message", (message) ->
        {
            try
            {
                // Separate this code affterwads
                JSONObject messageObject =  new JSONObject(message);
                String Messagetype = (String) messageObject.get("messageType");
                HandleWebSocketMessage(Messagetype, message);
            }
            catch (Exception ignored)
            {

            }
        }, String.class);

        _connection.onClosed(new OnClosedCallback() {
            @Override
            public void invoke(Exception exception) {
                handleClosed();
            }
        });

    }
    catch (Exception ignored)
    {

    }
    finally
    {
        connecting = false;
        startTimer();
    }

}

private void handleClosed()
{
    try
    {
        TimeUnit.MILLISECONDS.sleep(100);
    }
    catch (Exception ignored)
    {

    }
    Init();
}
}

I've tried upgrading to the latest version of SignalR Java Client

 implementation "com.microsoft.signalr:signalr:6.0.3"

and it is still not working. It just wont receive any Signal.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文