How do I make the server to send data to clients?

Started by
3 comments, last by hplus0603 12 years, 10 months ago
Problem:
My game contains a room where everyone who has started the game can chat - this is the menu for joining and creating rooms in multiplayer. This is why I can't make that chat peer to peer - so I made all clients to send chat data to a server and then check if there are any new messages. The problem is that the never-ending requests for chat data to the server seem like a DoS attack and the host of the server blocks my access temporarily.

What I need:
I have decided to make the chat push and not pull based - the clients will only send their chat messages and listen for incoming data, and the server will send all messages to all connected clients on regular interval. In this way it won't block players who imitate a DoS attack. But I'm fairly new in php and here come the questions.

1. Someone told me that proc_* and sock_* are unsafe and therefore are forbidden on a shared hosting(which I use). Then how do I make the server to send data to clients? Which functions should I use?
2. How can the server understand if the internet connection of a player has accidentally stopped and he couldn't tell the server he stopped playing?
3. Is it better to stop using php for chat and use IRC instead? I'm using a shared hosting and I have probably no more than 3% or 4% of a server's bandwidth.
Advertisement
How frequently were you polling for messages? If you poll every few seconds you shouldn't look like a DOS attack.

Offloading the chat to IRC could be a good solution.

But I'm fairly new in php and here come the questions.


3. Is it better to stop using php for chat and use IRC instead? I'm using a shared hosting and I have probably no more than 3% or 4% of a server's bandwidth.


PHP Is *not* a good language for "long poll" type situations. (Google for "long poll" and "comet" to see this mechanism described in more detail)
Each waiting child process doesn't see any of the other children, so you'd have to go through something like a shared database, which would be terrible for efficiency.
If your choice is "PHP or nothing" then you either have to put in an IRC plug-in on your web site, or live with the terrible performance you'll get out of PHP.
enum Bool { True, False, FileNotFound };

How frequently were you polling for messages?
As soon as the game recieved some data from the server, it requested another one. I dont know if the chat getting updated every 5-6 seconds would be nice for the players.

PHP Is *not* a good language for "long poll" type situations. (Google for "long poll" and "comet" to see this mechanism described in more detail)[/quote] That's why I'm looking to implement a push-type chat.

If your choice is "PHP or nothing" then you either have to put in an IRC plug-in on your web site, or live with the terrible performance you'll get out of PHP.[/quote] Are you saying I can't make a good irc client built in the game with PHP? I'm fine with whatever technology satisfies my needs.

If your choice is "PHP or nothing" then you either have to put in an IRC plug-in on your web site, or live with the terrible performance you'll get out of PHP.
Are you saying I can't make a good irc client built in the game with PHP? I'm fine with whatever technology satisfies my needs.
[/quote]

I am saying that if you use PHP the way everybody uses PHP, as a per-request scripting language within Apache or nginx, then no, you cannot make a useful IRC client with PHP.

For a persistent server, you need a persistent process. HTTP, Apache, and PHP are not persistent processes in this regard, because there is one "process" per request, and it dies as soon as the request is gone. Typical servers that allow long-running processes are servers that are not web based -- anything from something custom in C++, Java or C#, to something you write on top of node.js.

For a persistent, reactive response on the client, you either need a continuous connection -- with a game client, this is simple. With a web browser, you have to use WebSockets, Flash with sockets, or a custom plug-in of some sort, or you will have to double-buffer a form of long polling using XMLHttpRequest.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement