I have experience developing in android and using REST services, but now I'd like to implement an online game for android. For example a tic-tac-toe for multiplayer, where a user can invite to other to play a game and each one receive the actions of the other in real time.
Using REST for this kind of apps would require constants surveys to the server for updates and its a bad idea.Isn't it?
My question is what are the typical aproaches for this kind of "permanent" connections. I just need some hints about the technologies and protocols in client and server for start searching and read :D
Thanks
You can use sockets directly but for a game like tic-tac-toe, it shouldn't be THAT chatty, using REST would be ok.
Related
I've had a look around and could not find what I'm looking for, so please feel free to redirect me.
I'm writing a card game that uses a Java server that stores the card information. I want to develop an Android app that connects to this server (via TCP/IP is my initial thought) and issues the commands i.e. pick up cards, play cards etc. (as the cards are stored in arraylists within the server app.)
My knowledge of Android is extremely limited and I cant find any good sources that could possibly help me, or explain how android networking works.
You need tcp server socket communication >
http://developer.android.com/reference/java/net/Socket.html
I would recommend to use a REST service and communicate via JSON. You can use for example Robospice in combination with Jackson2 which also brings you async communication and caching out of the box.
https://github.com/stephanenicolas/robospice/wiki/Starter-guide
Socket communication is really old school ;-)
*I have to make an android application which is basically a chat application.*In this chat app the user can interact with single user as well in a group that means Group chat is also there. I am using php as the back end for database . I have never done this type of work earlier so need some suggestions or guidance.
The options i have seen is as follow :
1. Web-services
2. Socket Programming
3. XMPP using the smack api
Now i am confused in them that which is better to implement chat feature in android. Also i didn't get any sample code for that. One of my team member is asking me to use the socket programming but i didn't get any sample app or anything for socket programming in android. I don't know socket programming this time. How we can connect the our phone to the server and then some other device. Hope you get me what i want to Conway you.
So can any one please guide me and show me the right direction to move on. Also if you can provide me any sample code, that can be really helpful. Any help is appreciated
Another option (besides XMPP, which is certainly a valid approach for chat, since it was designed for that) would be using WebSocket. Using WebSocket would open the possiblity of having a pure browser client as well.
For WebSocket on Android, there is AutobahnAndroid, part of the Autobahn family of WebSocket and WAMP implementations.
WAMP is a RPC and PubSub over WebSocket protocol with multiple implementations. There is also a PHP implementation of WebSocket/WAMP: Ratchet.
Disclaimer: I am author of Autobahn/WAMP and work for Tavendo.
For a basic Socket programming tutorial, you can check HERE and HERE.
However, I suggest using XMPP, as I have mentioned HERE, as it is a protocol designed from the ground up to be used for Messaging.
Which is the best way to connect 2 android phones in the internet with a Java Server?
I'm making an game form android. At this moment i have the single player working. But now i have two use an Java server to play online games.
Which is the best way to communicate with 2 phones ?
Most difficult action is send information of client1 to the Server and Server redirect this information to client 2.
Anyone can help me?
Regards
You can use either SOAP or REST, using Jax-RS (rest) or Java-WS (SOAP).
http://www.oracle.com/technetwork/java/index-jsp-137004.html
http://docs.oracle.com/javaee/6/tutorial/doc/giepu.html
Here is one way, assuming your game has some turn based interface, and you want to do is have a create game/join game or a create game with user style interface.
When the game starts, connect to the server using one of
the above techniques (soap/rest) to let the server know you are
there.
Then have one client say create a new game. This will tell the
server to create the game.
The other client will tell the server to list games. Then on
the client, choose a game to join. Then tell the server of the game
choice, so that it knows to add you.
The phone that started the game, should poll the server to see if
another user joined the game. Eventually some will join. (there are
other ways to handle this, but polling is very easy to do)
Have all players in the game poll to find out if the game started, then
go, once they have all joined.
There sure isn't a best way to do that. There are some techniques.
But what I would do is to create a long, unclosed http request from both of the clients to the server and then when something happens the clients get responses from the server. This is called Comet, but I never implemented that by myself. There sure is an Android implementation (or just a Java implementation).
I want to make a two player turn based game (think of tic tac toe) for android. I am not sure how to implement the communication between the mobile and the server. Should I use sockets or http client ? Can anybody guide.
Issue with http client is I have to regularly poll the server to see if any data has arrived. Is there any other way to push the data from server if I am using http client ?
Have you considered HTTP requests + push notifications? Depending on the particular kind of game this might be the best solution I guess...
You can use App Engine's Channel API
https://developers.google.com/appengine/docs/java/channel/
Even though there is no official Android library you can use this WebView wrapper
https://github.com/burcu/android-channel-api
but I haven't tested it yet.
This is an old question but if you are still looking for a solution you could try the new Google Play Game Services.
I'm programming an Android multi-player game, which basically consist of a server where the clients connect and exchange messages. When the player connects to a server, a player list is return to him/her. A player can then select a user to challenge - of course he must select a player from the player list, which only contains connected users.
When a player1 challenges player2, a message needs to be transmitted from player1 to the server, which in turn must send a message to the player2, notifying him about the challenge. The player2 can then accept/decline the challenge.
I can use the following techniques to make this happen:
Use custom server/client with Java socket programming. The server basically accepts a connection from the client, spawning a new thread for each connected client. The problem with this are:
There needs to be a persistent connection open from client to server wasting battery life of the android phone. This is not really big limitation since the battery isn't consumed that much.
When I'll want to develop another game I'll have to rewrite the client/server code from the scratch - also choosing another port to listen for incoming connections - the whole concept gets rather difficult to maintain.
I'm also worried if this is the way to do it. Spawning another thread for each clients sound quite a lot if thousands clients are connecting at the same time. But I'm guessing the PC games do it like this. Not sure about android.
Use Java REST jersey to build the client-server on top of HTTP. This would be a perfect solution if the server could easily send notifications to clients. There are actually multiple design decisions here:
the client pulls the server for any new data/notifications every few seconds - this is really bad, since we're stuck with non responsiveness, delay, etc.
the client can send a waiting request to server, so the client receives the response only after some data becomes available. This is better, but can still produce a delay when two notifications one after another need to be sent to the user. The first notification is sent instantly, since the client already has a connection open, waiting for data to receive. But we would have to wait for the client to initiate another long http request to receive the second notification. The problem gets bigger as there are multiple notifications that need to be send in a row to a specific client.
the client can initiate a http streaming, where the communication is left open when the request is handled, so the server can also send multiple messages to client whenever it wishes. The problem here is that I don't know how well this works on Android. I've looked at several implementations:
Java jersey + atmosphere: didn't succeed in actually making it work. This seems the most promising, but I don't want to spend too much time on it, since I'm not even sure if it does what I want.
Deacon: seems pretty neat, but after seen the video tutorial on their official web page, I'm not sure that it can do what I need. When a player1 challenges player2, can it send a notification to player2 letting it know about the match request?
I would be glad to know how other multi-player games handle the network communications, if the two players are playing the game over the network.
I'm also open to a totally new suggestion how to achieve what I want. I can pretty much code anything, so don't hesitate to let me know of some more difficult way to achieve the network communication.
Let me also mention that I'll be glad to implement a totally specific method to work in my case, so it can be anything that will do the job done, but I'm also looking at more general way for communication between clients and server. So that I can program an interface/whatever and reuse the code in other android games, android applications.
I hope I presented the problem allright and that I'll receive some valuable answers.
Thank you
You should take a look at XMPP. It's a protocol (originally created for chat programs) that allows sending of xml data between users.
It has a separated client-server relationship, so that you can focus on developing a client application fit for phones, and a different server depending on your needs.
There are loads of information available on the protocol (I should know, I wrote a thesis about using the protocol in game applications), but you can start by looking it up on wikipedia to see if it is what you want.
aSmack is a library for creating android xmpp-clients. It takes some tweaking to set it up and get everything to work, but once you do, it's neat.
EDIT: relating to the answer suggesting using the C2DM:
from the c2dm docs "Sending large numbers of C2DM messages":
Are you sending C2DM messages too frequently? If you need to communicate with your application frequently over a short period of
time, C2DM is probably not the best solution. Instead, consider
implemeting XMPP or your own protocol to exchange messages, and use
C2DM only to send the initial notification.
It sounds like Android Cloud-to-Device-Messaging might be what you need
Push notifications without the app having to keep a connection open
I would vote in favor of some message passing technique - like activeMQ, rabbitMQ, zeroMQ eor something like it. On the server side you may stick with java , or javascript ( like
node.js ) - such solution would provide most performance and minimal latencies.
If latency is not that critical, you may as well use REST calls with JSON