Android service persistent connection

Post here your questions about the Java client / Android API for SFS2X

Moderators: Lapo, Bax

sitz
Posts: 14
Joined: 05 Aug 2019, 16:07

Android service persistent connection

Postby sitz » 05 Aug 2019, 16:19

Starting with Android OS 7 and really cracking down in OS 8, whenever you minimize the Activity is pauses completely. This disconnects you from SFS2X. You can do an instant reconnect when the app is brought back up, but in this case I need the connection to be maintained through a Service. Those do not pause in the background and it's the only way to keep a persistent connection going.

it crashes on connect(ip, port). If it comment this out, the service starts. The port is good, the ip is good, other clients connect to the server just fine. I changed it from my cloud host to localhost in this code sample. I tried it with ConfigData, still crashed. I'm on Android OS9.

Code: Select all

package com.ronin.connect;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
import sfs2x.client.SmartFox;
import sfs2x.client.core.BaseEvent;
import sfs2x.client.core.IEventListener;
import sfs2x.client.core.SFSEvent;
import sfs2x.client.requests.JoinRoomRequest;
import sfs2x.client.requests.LoginRequest;
import sfs2x.client.util.ConfigData;

public class MyService extends Service implements IEventListener {
    int mStartMode;
    IBinder Binder;
    boolean mAllowRebind;

    SmartFox sfsClient;
    //ConfigData cfg = new ConfigData();

    @Override
    public void onCreate() {
        // The service is being created
        initSmartFox();
    }

    private void initSmartFox() {
        // Instantiate SmartFox client
        sfsClient = new SmartFox();

        // Add event listeners
        sfsClient.addEventListener(SFSEvent.CONNECTION, this);
        sfsClient.addEventListener(SFSEvent.CONNECTION_LOST, this);
        sfsClient.addEventListener(SFSEvent.LOGIN, this);
        sfsClient.addEventListener(SFSEvent.ROOM_JOIN, this);

        Toast.makeText(getApplicationContext(),"Connecting",Toast.LENGTH_SHORT).show();

        // CRASHING RIGHT HERE
        sfsClient.connect("10.1.1.10", 9933);
    }

    @Override
    public void dispatch(final BaseEvent event)  {
        if (event.getType().equalsIgnoreCase(SFSEvent.CONNECTION)) {
            if (event.getArguments().get("success").equals(true)) {
                // login to current zone
                // sfsClient.send(new LoginRequest("test", "test"));
            } else {
                // failed connection
            }
        } else if (event.getType().equalsIgnoreCase(SFSEvent.CONNECTION_LOST)) {
            disconnect();
        } else if (event.getType().equalsIgnoreCase(SFSEvent.LOGIN)) {
            sfsClient.send(new JoinRoomRequest("1"));
        } else if (event.getType().equalsIgnoreCase(SFSEvent.ROOM_JOIN)) {
            // room join code
        }
    }

    private void disconnect() {
        if (sfsClient.isConnected()) {
            sfsClient.disconnect();
        }
    }

    public void onStartService(Intent intent, int startId) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Connector Started", Toast.LENGTH_LONG).show();
        super.onStart(intent, startId);
    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        // All clients have unbound with unbindService()
        return mAllowRebind;
    }
    @Override
    public void onRebind(Intent intent) {
        // A client is binding to the service with bindService(),

        // after onUnbind() has already been called
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Toast.makeText(this, "Connector Stopped", Toast.LENGTH_LONG).show();
    }


}
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Android service persistent connection

Postby Lapo » 05 Aug 2019, 16:30

Hi,
what does it mean exactly that it crashes? Do you have a stack trace that we can see, with details?

Thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
sitz
Posts: 14
Joined: 05 Aug 2019, 16:07

Re: Android service persistent connection

Postby sitz » 05 Aug 2019, 16:39

It dumps you back to the OS home screen and a few seconds later the App Keeps Stopping error pops up. It's not sending any packets out for the login, no data used at all, nothing on the server log. I'm setting up debug with Android Studio right now to get more data.
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Android service persistent connection

Postby Lapo » 05 Aug 2019, 17:36

Thanks, we need more details.
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Java / Android API”

Who is online

Users browsing this forum: No registered users and 8 guests