Simple Java Client Examples?

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

Moderators: Lapo, Bax

homerlex
Posts: 48
Joined: 16 Aug 2012, 15:13

Simple Java Client Examples?

Postby homerlex » 16 Aug 2012, 15:23

Hello - total Smartfox2x newbie. I'd like to write some simple Java command line apps for some proof of concept stuff. I'm just wondering if there are sample non-Android Java apps somewhere just to give me a place to start. Yes, I suppose I can looked at the Android samples to help get up to speed but if there are standalone Java command line samples somewhere that is where I'd like to start.

A sample command line chat client would probably contain all the functionality I need to get going but I'll take anything to start.

Thanks.
User avatar
Bax
Site Admin
Posts: 4609
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: Simple Java Client Examples?

Postby Bax » 16 Aug 2012, 15:57

We have a very basic client showing the connection (and maybe login, if I'm not wrong). It could be a starting point.
Just drop us an email at info AT smartfoxserver DOT com.
Paolo Bax
The SmartFoxServer Team
homerlex
Posts: 48
Joined: 16 Aug 2012, 15:13

Re: Simple Java Client Examples?

Postby homerlex » 16 Aug 2012, 16:27

Bax wrote:We have a very basic client showing the connection (and maybe login, if I'm not wrong). It could be a starting point.
Just drop us an email at info AT smartfoxserver DOT com.


Thanks - I sent mail. In the mean time I did come up with some code that connects. However, after I do a .connect() call on the SmartFox object a thread is started up that never stops named "New I/O client worker #1-1". Even after I do a .disconnect() the thread remains. Should this thread stop after I issue a disconnect? If not, how am I supposed to programmatically end it?

Perhaps your sample code will shed some light on this but I figured I'd as while I await the sample.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Simple Java Client Examples?

Postby Lapo » 17 Aug 2012, 11:42

That's the Netty I/O thread, I am not entirely sure if this thread is supposed to go away. I would presume no, because the API instantiates a socket connector which is not destroyed at the end of the cycle, since you might want to reconnect again.

I think at least you should remove all listeners from the SmartFox object and the release its reference(s).
Are you doing this?
Lapo
--
gotoAndPlay()
...addicted to flash games
homerlex
Posts: 48
Joined: 16 Aug 2012, 15:13

Re: Simple Java Client Examples?

Postby homerlex » 17 Aug 2012, 12:42

Yes, I am removing the listeners and releasing the reference.

This is basically what I am doing:

Code: Select all

// Set up the connection
sfsClient = new SmartFox();
sfsClient.addEventListener(SFSEvent.CONNECTION, this);
sfsClient.connect("localhost", 9933);


In the dispatch method I handle the CONNECTION event:

Code: Select all

if (event.getType().equalsIgnoreCase(SFSEvent.CONNECTION)) {
            if (event.getArguments().get("success").equals(true)) {
                System.out.println("Connection Successful");
                onDestroy();


The onDestroy code looks like this:

Code: Select all

if (sfsClient != null) {           
            sfsClient.removeAllEventListeners();
            sfsClient.disconnect();
            sfsClient = null;
        }


I found that when I do the sfsClient.disconnect() call control is never returned (I never get to the sfsClient = null; line).

Even if I comment out the disconnect call setting sfsClient = null doesn't help. The only way I can get my app to end is to not call disconnect and do a System.exit(0);
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Simple Java Client Examples?

Postby Lapo » 17 Aug 2012, 13:50

Well, that's pretty strange. I don't have any problems with that, I can call the disconnect() method and the threads continues with the subsequent code. No delays or thread blocking.
Are you sure you are using the latest API and there's no errors both client and server side?
Lapo

--

gotoAndPlay()

...addicted to flash games
homerlex
Posts: 48
Joined: 16 Aug 2012, 15:13

Re: Simple Java Client Examples?

Postby homerlex » 17 Aug 2012, 14:15

Lapo wrote:Are you sure you are using the latest API and there's no errors both client and server side?


Yup - Latest API.

When disconnect is called I see the following in the server log:

Code: Select all

10:05:42,319 INFO  [SocketReader] sessions.DefaultSessionManager     - Session removed: { Id: 1, Type: DEFAULT, Logged:
No, IP: 127.0.0.1:59173 }


... and I see the following logged in my client

Code: Select all

10667 [New I/O client worker #1-1] INFO sfs2x.client.core.EventDispatcher - Dispatching event OnDisconnect to 1 listeners
10668 [New I/O client worker #1-1] INFO sfs2x.client.core.EventDispatcher - Dispatching event disconnect to 1 listeners
homerlex
Posts: 48
Joined: 16 Aug 2012, 15:13

Re: Simple Java Client Examples?

Postby homerlex » 17 Aug 2012, 15:52

I figured out the problem - or at least what triggers it.

For my test I just wanted to to connect and disconnect. I was having the event handler call the method that does the disconnect. That is when bad things happen.

If, instead, I connect and trigger the disconnect by entering something on the command line (instead of in the CONNECTION handler) it all works fine.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Simple Java Client Examples?

Postby Lapo » 17 Aug 2012, 18:44

I tried the same and don't see anything bad happening, calling the disconnect in the thread that executes the event handler works just fine.
Can I see the code you're using?

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
homerlex
Posts: 48
Joined: 16 Aug 2012, 15:13

Re: Simple Java Client Examples?

Postby homerlex » 17 Aug 2012, 19:04

Sure, below is the entire code. When you execute it you will see ""About to disconnect ..." but never get to the next println.

The version of the Java API client came with the latest download of SFS2X (2.0.1) - there is no version number in the API_Java jar but the classes in there were compiled Feb 7, 2012

Code: Select all

package smartclient;

import com.smartfoxserver.v2.exceptions.SFSException;
import sfs2x.client.SmartFox;
import sfs2x.client.core.BaseEvent;
import sfs2x.client.core.IEventListener;
import sfs2x.client.core.SFSEvent;

public class TestClient implements IEventListener {

    SmartFox sfsClient;

    @Override
    public void dispatch(final BaseEvent event) throws SFSException {

        if (event.getType().equalsIgnoreCase(SFSEvent.CONNECTION)) {
            if (event.getArguments().get("success").equals(true)) {
                System.out.println("Connection Successful");
                onDestroy();
            } //otherwise error message is shown
            else {
                System.out.println("ERROR: Connection Not Successful");
            }
        }
    }

    public void start() {
        sfsClient = new SmartFox();
        sfsClient.addEventListener(SFSEvent.CONNECTION, this);
        sfsClient.connect("localhost", 9933);
    }

    protected void onDestroy() {
        if (sfsClient != null) {           
            sfsClient.removeAllEventListeners();
            System.out.println("About to disconnect ...");
            sfsClient.disconnect();
            System.out.println("Yay, disconnected");
            sfsClient = null;
        }
       
    }

    public static void main(String[] args) {
        TestClient client = new TestClient();
        client.start();
    }
}

homerlex
Posts: 48
Joined: 16 Aug 2012, 15:13

Re: Simple Java Client Examples?

Postby homerlex » 17 Aug 2012, 19:09

BTW - I tried the above code in both Java 1.6 and 1.7 and had the same result.
homerlex
Posts: 48
Joined: 16 Aug 2012, 15:13

Re: Simple Java Client Examples?

Postby homerlex » 17 Aug 2012, 19:13

... and for completeness, below is the output I get when I run it:

Code: Select all

128 [New I/O client worker #1-1] INFO sfs2x.client.core.EventDispatcher - Dispatching event OnConnect to 1 listeners
129 [New I/O client worker #1-1] INFO sfs2x.client.core.EventDispatcher - Dispatching event connect to 1 listeners
153 [New I/O client worker #1-1] INFO sfs2x.client.core.EventDispatcher - Dispatching event OnData to 1 listeners
154 [New I/O client worker #1-1] INFO sfs2x.client.core.SFSIOHandler - Handling New Packet of size 80
155 [New I/O client worker #1-1] INFO sfs2x.client.core.SFSIOHandler - Handling Header Size. Length: 79 (small)
155 [New I/O client worker #1-1] INFO sfs2x.client.core.SFSIOHandler - Data size is 77
155 [New I/O client worker #1-1] INFO sfs2x.client.core.SFSIOHandler - Handling Data: 77, previous state: 0/77
156 [New I/O client worker #1-1] INFO sfs2x.client.core.SFSIOHandler - <<< Packet Complete >>>
158 [New I/O client worker #1-1] INFO sfs2x.client.core.EventDispatcher - Dispatching event connection to 1 listeners
Connection Successful
About to disconnect ...
160 [New I/O client worker #1-1] INFO sfs2x.client.core.EventDispatcher - Dispatching event OnDisconnect to 1 listeners
160 [New I/O client worker #1-1] INFO sfs2x.client.core.EventDispatcher - Dispatching event disconnect to 1 listeners
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Simple Java Client Examples?

Postby Lapo » 18 Aug 2012, 07:58

You are using and old version of both the server and API, please make sure to upgrade and retest your example.
You find the updates here:
http://www.smartfoxserver.com/download/sfs2x#p=updates
Lapo

--

gotoAndPlay()

...addicted to flash games
homerlex
Posts: 48
Joined: 16 Aug 2012, 15:13

Re: Simple Java Client Examples?

Postby homerlex » 20 Aug 2012, 12:47

Sorry - I did not realize there was a patch. Anyhow, I've upgraded. Now on server 2.2.0 and the binaries in the client jar were complied on June 16, 2012.

With the latest updates I can still duplicate the behavior with the sample code I posted earlier.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Simple Java Client Examples?

Postby Lapo » 20 Aug 2012, 12:59

I wasn't talking about the Server patch I was talking about the Java API version.
there is no version number in the API_Java jar but the classes in there were compiled Feb 7, 2012

The version number is obtained via the getVersion() method.
Also if you check the page I have linked, below the server patch you will find the latest Java API which was release in May 2012

cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Java / Android API”

Who is online

Users browsing this forum: No registered users and 17 guests