2D Multiplayer approach

Started by
3 comments, last by Gazu 11 years, 10 months ago
Hey there, I've been trying to establish a client/server architecture for a 2D Multiplayer application for like, 1-2 weeks now.
I've been using ruby (becuse I'm comfortable with it's syntax) and rubygame.

I have some problems now, it's kinda getting too complex to just find things out myself or via google. (still helps here and there, but problems seem too specific to get the right answers, which is funny because these should be common problems for EVERY newbie at networking)

My "current final" goal is just having 2 clients connected to the server, each one of them being able to see the movement of the other player.
The "game" itself isn't anything more than that just yet, just a moveable character on a random-colored background.

What I achieved so far is, getting all the logic behind player movement to the server, the client just sends the key inputs to the server.
I'm sending strings with a header part and the appropiate player's values to the server, which then knows how to change player positions accordingly, then sends those positions to all currently connected clients. Well at least in theory.

Now this is where my confusion starts. The commnication and processing of the data itself works just fine.
The problem is, waiting to receive messages on client and server. Because, as long as my client for example, waits for a message from the server, the game itself freezes of course, because the loop for updating the screen "pauses" there. At the same time, the Server waits for incoming input from the client, but since the client is in a frozen state, there will be none, and the server won't send any data.

So naturally (I think) my first thought was, letting one extra thread handle as well the client's and the server's receiving data, and let the main loop just send the current information all the time.
That way, the main loops won't be blocked by waiting for data.
I am well aware that this is a very basic approach and that there is much, much more behind managing data transfer between clients and server, but with a small application on localhost and without any experience in that matter whatsoever, I thought this should do the job for now.
But I was wrong, as the threads somehow just stop working as soon as I have the first input sent to the server.
Or in other words, as soon as they actually would have something to do.

So with that information, can anyone give me any pointers on how to approach my current goal? I will give out any information thats needed but I didn't want to create a too large wall of text for now, so thank you in advance.
Advertisement
For interactive (not turn-based) games, using a web server or web server framework is almost always the wrong tool for the job.
No matter what, the client needs to be able to receive asynchronous notifications from the network, while handling user input and animation display at the same time. This is common to all interactive games.
If you really want to be using a browser, look into "comet," or "AJAX polling."
enum Bool { True, False, FileNotFound };
I'm not using any browser/webserver/framework, it's all just plain .rb files containing ruby code.
The server I'm speaking about is just a bunch of local ruby code, just as anything else =)
I have the .rb files online at gist.github, but I'd want to refresh them since they are now outdated, and then polish them up a bit before posting it here. It's quite a mess now (many outcommented lines and puts statements for testing)


No matter what, the client needs to be able to receive asynchronous notifications from the network, while handling user input and animation display at the same time.

Yea, that's what I'm trying to figure out how to achieve :)
The browser has many ways of doing asynchronous communication. The most common one is to use XMLHttpRequest with an onReadyStateChange callback.
Also, you might want to look into socket.io, which has some good examples and support for this. Not Ruby-based on the server, though.
enum Bool { True, False, FileNotFound };
Excuse me, obviously I really suck at explaining myself.
The whole thing is not supposed to be web based.
No browsers.

Imagine it like an MMO. Most of them are not played in Browerse (though some are).
Or Like, Diablo. Again, no browsers.
Just a game, featuring multiplayer, be it via LAN or internet.
The only difference is, at the moment is, it's not designed so that one client is the host. Instead, there's a seperate program I wrote, which handles inputs from the clients via UDP sockets: that is the server.

But I will look into what you suggested, maybe I will get some hints about how to approach the logic behind handling the data traffic in my approach, so thank you.

This topic is closed to new replies.

Advertisement