java android 中的信号器连接事件句柄

signalr connection event handle in java android

我正在使用 SignalR 制作一款双人在线游戏。到目前为止,一切都很好。但是当其中一个玩家的互联网被切断(几毫秒内)或服务器因任何原因被切断时,该程序将不再运行。如何收到连接事件的通知? 如何自动重新连接?

研究了很久,一直没有找到解决办法

Asp.NET 核心 5.0 服务器端:

    public class ChatHub : Hub
    {
        LogHelper logHelper = new LogHelper("hub");

        public async Task Move(float x, float y)
        {
                await Clients.Others.SendAsync("ReceivePosition", x, y);
        }
}

客户端javaandroid工作室:

public class MainActivity extends AppCompatActivity {


    private static final String TAG = "qazwsx";
    Button btnStart;
    View viewMove;

    HubConnection hubConnection;

    @SuppressLint({"ClickableViewAccessibility", "CheckResult"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        hubConnection = HubConnectionBuilder.create("http://192.168.1.10:45455/notification").build();

        hubConnection.on("ReceivePosition", (x, y) -> {

            runOnUiThread(() -> {
                viewMove.setX(x);
                viewMove.setY(y);

                Log.i(TAG, String.format("onCreate: x:%s  y:%s", x, y));
            });

        }, Float.class, Float.class);

        btnStart = findViewById(R.id.bntStart);
        viewMove = findViewById(R.id.viewMove);


        btnStart.setOnClickListener(view -> {
            Log.i(TAG, "onCreate > btnStart.setOnClickListener > status:" + hubConnection.getConnectionState());
            if (btnStart.getText().toString().equals("START")) {
                if (hubConnection.getConnectionState() == HubConnectionState.DISCONNECTED) {
                    btnStart.setText("STOP");
                }
            } else {
                if (hubConnection.getConnectionState() == HubConnectionState.CONNECTED) {
                    hubConnection.stop();

                }
            }
        });

        final float[] dX = new float[1];
        final float[] dY = new float[1];
        viewMove.setOnTouchListener((view, event) -> {

            switch (event.getAction()) {
                //this is your code
                case MotionEvent.ACTION_DOWN:
                    dX[0] = view.getX() - event.getRawX();
                    dY[0] = view.getY() - event.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    view.animate()
                            .x(event.getRawX() + dX[0])
                            .y(event.getRawY() + dY[0])
                            .setDuration(0)
                            .start();
                    if (hubConnection.getConnectionState() == HubConnectionState.CONNECTED) {
                        hubConnection.send("Move", event.getRawX() + dX[0], event.getRawY() + dY[0]);
                        Log.i(TAG, String.format("move: x:%s  y:%s", dX[0], dY[0]));

                    }
                    break;
                default:
                    return false;
            }
            return true;

        });
    }


}

经过大量研究,我设法解决了这个问题。我正在写这个问题的解决方案。我希望它能解决别人的问题

处理信号器连接关闭:

hubConnection.onClosed(exception -> {
            //do something
            //attemt to connect
            //note: exception is null when the user stop connection
        });

处理信号器连接开始:

    hubConnection.start()
                            .doOnError(throwable -> {
                                Log.e(TAG, "doInBackground > doOnError: ", throwable);
//start fail , try again    
//note: the start function need try chach when we use this function
                            })
                            .doOnComplete(() -> {
                                Log.i(TAG, "doInBackground > doOnComplete.");
                                //start complated
                            })
                            .blockingAwait();//you must write this function ,else other function not worck