I am writing a small game in android that basically is a turn-based boardgame, and I am having some questions about my multiplayer design, if it’s good of bad practice.
The required speed of the client-server communication is slow at best, much like wordfeud, so I have used AsyncTask to do what I need, examples of tasks are: LoginTask, RegisterUserTask, StartGameTask, PlayMoveTask.
To initialize I can write:
VerifyUserTask verify = new VerifyUserTask();
verify.execute(username, password);
On the serverside I have a php script that takes the parameters sent in, and returns a result.
Back to the android again I might need to wait for a reply using
Boolean verified = verify.get(10, TimeUnit.SECONDS);
The verifybit is rarely used, it’s only when I need to verify that the user really is a valid user with a valid password.
I know this works, but is it an ok solution? Considering it’s lightweight, fast enough, and I can log errors and responses.
Thanks in advance…
Try to avoid using
AsyncTaskfor everything. It will bring you more problems than solutions.Create an Android
Serviceto handle all the backend logic and find a way to comunicate with your activities.I would recommend you learning about
SyncAdapters. Here is a blog post with a nice introduction.