在运行前景或后台的整个应用程序中保持Socket.IO连接

时间:2019-07-12 04:59:06

标签: android socket.io

我已经使用socket.io制作了一个一对一的群聊应用程序,一切正常,但是如果我与UI进行交互,它将建立连接,但是一段时间之后,我停止与应用程序进行交互,则连接中断并且用户下线,因为我没有使用任何事件使用户下线。有人可以帮我吗?

 public  SocketConnection() {
    {
        try {
            nSocket = IO.socket(Constants.NAME_SPACE);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }
}


public Socket getSocket(){
    return  nSocket;
}

这是我使用户在线的地方。

 public class SplashActivity extends ActivityManagePermission {
    String permissionAsk[] = {PermissionUtils.Manifest_CAMERA
           , PermissionUtils.Manifest_WRITE_EXTERNAL_STORAGE
          , PermissionUtils.Manifest_READ_EXTERNAL_STORAGE
           };
     Context context = SplashActivity.this;
    private JSONObject mJson;
    SessionManagement session;
private Socket mSocket;
int SPLASH_TIME_OUT = 2000;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    session = new SessionManagement(context);

    /**
     * Creating the socket connection.
     */
    SocketConnection obj = new SocketConnection();
    try {
        mSocket = obj.getSocket();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }




    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            askCompactPermissions(permissionAsk, new PermissionResult() {
                @Override
                public void permissionGranted() {
                    if(session.isSignedIn()) {
                        mJson = new JSONObject(); //creating json object
                        Object type = "M";
                        Object ID_USER = session.getUserDetails().get(SessionManagement.USER_ID);
                        try {
                            mJson.put("userId", ID_USER);
                            mJson.put("connection_type",type);
                        }catch (JSONException e) {
                            e.printStackTrace();
                        }

                        mSocket.emit(Constants.event_new_user_online,mJson.toString());
                        mSocket.connect();
                        Intent i = new Intent(context,HomeActivity.class);
                        startActivity(i);
                        finish();
                    }else {
                        Intent i = new Intent(context, LoginActivity.class);
                        startActivity(i);
                        finish();
                    }
                }

                @Override
                public void permissionDenied() {
                    Intent i = new Intent(context, SplashActivity.class);
                    startActivity(i);
                    finish();
                }

                @Override
                public void permissionForeverDenied() {
                    Toast.makeText(context,
                            "Please allow the permissions"
                            ,Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(context,SplashActivity.class));
                }
            });

        }

    }, SPLASH_TIME_OUT);
}

}

1 个答案:

答案 0 :(得分:1)

您需要使用Socket IO options

IO.Options options = IO.Options()

options.setTimeout(60000L) // 60 seconds
options.setReconnection(true)

,然后在建立连接时像这样使用它。

nSocket = IO.socket(Constants.NAME_SPACE, options);