Android multiplayer network game

I am programming an Android multiplayer game that basically consists of a server on which clients connect and exchange messages. When a player connects to the server, the list of players returns to him. Then the player can select the user to call - of course, he must select the player from the list of players, which contains only connected users.

When player1 challenges player2, the message must be sent from player1 to the server, which in turn should send a message to player2, notifying him of the call. Player2 can accept / reject the call.

I can use the following methods to make this happen:

  • Use a custom server / client with Java socket programming. The server basically accepts a connection to the client, creating a new stream for each connected client. The problem with this:

    • There must be a permanent connection, open from the client to the server, losing the battery life of the Android phone. This is not a very big limitation, since the battery is not consumed so much.
    • When I want to develop another game, I will have to rewrite the client / server code from scratch - also choosing a different port to listen for incoming connections - the whole concept becomes quite difficult to maintain.
    • I am also worried if this is the way to do it. Ignoring another topic for each client sounds pretty much if thousands of clients connect at the same time. But I guess that computer games do that. Not sure about android.
  • Use Java REST jerseys to build a client server on top of HTTP. This would be ideal if the server could easily send notifications to clients. There are several design solutions here:

    • the client pulls out the server for any new data / notifications every few seconds - this is really bad, because we are stuck with non-response, delay, etc.
    • the client can send a wait request to the server, so the client receives a response only after some data becomes available. This is better, but it can still cause a delay when the user needs to send two notifications one by one. The first notification is sent instantly, as the client has already opened a connection, waiting for data to be received. But we will have to wait until the client initiates another long HTTP request to receive a second notification. The problem grows because there are several notifications that need to be sent in a row to a specific client.
    • the client can initiate a streaming HTTP message when the connection remains open while processing the request, so the server can also send several messages to the client when it wants. The problem here is that I don't know how well this works on Android. I reviewed several implementations:
      • Java jersey + atmosphere: failed to actually get it to work. This seems the most promising, but I don’t want to spend too much time on him, since I’m not even sure that he is doing what I want.
      • Deacon: It seems pretty neat, but after watching the video tutorial on their official web page, I'm not sure that he can do what I need. When player1 challenges player2, can he send a notification to player2 letting him know about the match request?
  • I would be happy to know how other multiplayer games handle network communications if two players play the game over the network.

  • I am also opening a completely new proposal on how to achieve what I want. I can encode a lot of things, so feel free to tell me about a more complex way to achieve network communication.

Let me also mention that I will be happy to implement a very specific method for working in my case, so that may be all that will make this work, but I also consider a more general way of communication between clients and server. So that I can program the interface / independently and reuse the code in other Android games, Android applications.

Hope I submitted a problem and I will get some valuable answers.

thanks

+6
source share
3 answers

You should take a look at XMPP. This is a protocol (originally created for chat programs) that allows you to send XML data between users.
It has a separate client-server relationship, so you can focus on developing a client application suitable for phones and on another server depending on your needs.

There is a lot of information in the protocol (I should know, I wrote a thesis about using the protocol in gaming applications), but you can start by looking at wikipedia to make sure that this is what you want.

aSmack is a library for creating android xmpp clients. It takes some change to tweak it and make it work, but once you do, it will be neat.

EDIT: Related to the answer suggesting to use C2DM:
from c2dm docs "Sending a large number of C2DM messages" :

Are you sending C2DM messages too often? If you need to frequently chat with your application for a short period of time, C2DM is probably not the best solution. Instead, consider using XMPP or a proprietary protocol for messaging, and using C2DM only to send the initial notification.

+2
source

Sounds like Android Cloud-to-Device-Messaging may be what you need.

Push notifications without an application that must support an open connection

0
source

I would vote for some kind of messaging technique - for example, activeMQ, rabbitMQ, zeroMQ, something like this. On the server side, you can stick with java or javascript (e.g. node.js) - such a solution will provide maximum performance and minimum latency.

If the latency is not critical, you can also use REST calls with JSON

0
source

Source: https://habr.com/ru/post/903690/


All Articles