Reputation: 84
I have made my one to one and group chat application using socket.io , everything is working fine , but If I am interacting with UI it establish the connection but after some time when I stop interacting with the application then the connect break and user goes offline as I am not using any event for putting my user offline. Can anyone help me in this.
public SocketConnection() {
{
try {
nSocket = IO.socket(Constants.NAME_SPACE);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
}
public Socket getSocket(){
return nSocket;
}
this is where I am putting my user online.
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);
}
}
Upvotes: 2
Views: 1081
Reputation: 8835
You need to use Socket IO options.
IO.Options options = IO.Options()
options.setTimeout(60000L) // 60 seconds
options.setReconnection(true)
and then use it like this while making a connection.
nSocket = IO.socket(Constants.NAME_SPACE, options);
Upvotes: 0