php 中的服务器端编程设计问题

发布于 2024-10-09 02:41:46 字数 533 浏览 0 评论 0原文

我正在尝试编写一个类似于 Go 的多人游戏,并且想知道人们如何在服务器端的玩家之间进行通信?

由于 php(或任何其他语言)中没有任何内容可以执行此操作,因此我可以将每个动作写入数据库并为其他玩家读取它。但这似乎很浪费,原因如下——

  • 我不需要存储各个玩家的动作。 (除非你让我相信这是绝对必要的)
  • 数据库将被大量写入和读取(我可以将很多资源用于其他用途)
  • 现在可以将内容存储在内存中。我希望快速对此进行原型设计。 (我知道,如果我将东西存储在内存中,如果机器立即死机,我就会失去所有用户的整个游戏)。

那么,即使它是一个不同的用例,浏览器内聊天应用程序会做什么呢?我有一个明确的情况要写入数据库,因为它是一款可能持续长达 10-15 分钟的游戏,但对于聊天来说绝对不需要。

还有其他我应该注意的软件吗?我可以使用 XMPP 服务器(尚未研究过这一点),但它可以与常规标准 HTTP post/get 一起使用吗?

给我一些网址,我将从那里进行研究。感谢您的帮助。

  • 帕夫

I am trying to write a multiplayer game similar to say Go and was wondering how people do the communication across players on the server side?

Since there is nothing in php (or any other language) to do this I could write every move to a DB and read it for the other player(s). But this seems so wasteful for the following reasons -

  • I don't need to store the moves of various players. (unless you convince me that this is absolutely necessary)
  • The DB will be write heavy as well as read heavy ( a lot of resources I can use for something else)
  • Its ok to store things in memory for now. I am looking to quickly prototype this. (I understand that if I store things in memory I lose the whole game for all users if the machine instant dies).

So even though its a different use case what do in-browser chat applications do? There is a definite case for me to write to a DB since its a game that could last upto 10-15 mins but for a chat its definitely not needed.

Any other software I should be aware of? Can I use an XMPP server (have not looked into this yet) but does it work with regular standard HTTP post/get?

Point me some URLs and I will do the research from there. Thanks for your help.

  • Pav

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

等风来 2024-10-16 02:41:46

我非常推荐 XMPP,因为它可以为您解决大部分问题。大多数 XMPP 服务器确实支持通过 HTTP 连接(通过类似 COMET 的技术,称为 BOSH),至少有两个纯 Javascript 库允许直接从浏览器执行此操作:Strope.jsJSJaC

通常,由于路由时间短,XMPP 服务器仅将消息保留在内存中,因此不会' t 将它们写入数据库。大多数服务器都支持插件,因此以后可以将系统更改为透明地记录移动或将消息存储到数据库。

Strope.js 的一位作者写了一本书,其中有一章介绍编写简单的两人游戏(实际上是 Tic-Tac-Toe),因此您可能需要 如果您沿着这条路线走,请检查一下

I'd advocate XMPP pretty much because it solves most of the issues for you. Most XMPP servers do support connecting via HTTP (via a COMET-like technique known as BOSH), there are at least two pure-Javascript libraries that allow that directly from the browser: Strophe.js and JSJaC

Typically because of the short routing times an XMPP server keeps messages in memory only, so doesn't write them to a database. Most servers support plugins so that changing the system to e.g. record moves or store messages to a database transparently would be possible later on.

One of the Strophe.js authors wrote a book that has a chapter on writing a simple two-player game (actually Tic-Tac-Toe), so you might want to check that out if you go down this route.

佼人 2024-10-16 02:41:46

我曾经使用 Memcached 来做这种事情。它非常快并且不存储所有动作(只是覆盖玩家的键)。它会让你的事情变得美好而轻松。

PECL 中有两个 PHP Memcached 库(MemecacheMemcached)。两者都做得很好。

I've used Memcached to do this kind of thing. It's very fast and doesn't store all of the moves (just overwrite the players' keys). It'll make things nice and easy for you.

There are two PHP Memcached libraries in PECL (Memecache and Memcached). Both do the job just fine.

天生の放荡 2024-10-16 02:41:46

许多聊天都使用平面文件。平面文件可能也是您的正确选择。 Memcached 的一些非正统用途也可能如此。游戏本质上是短暂的。因此,对 PHP 进行与 Session 相同的操作可能是构建系统模式的好方法。

祝你好运!

Many chats use flat-files. Flat files might be the right choice for you too. As might some unorthodox uses of Memcached. Games are transitory in nature. So doing the same things PHP does for Session might be a good way to pattern your system.

Good luck!

风追烟花雨 2024-10-16 02:41:46

我从未听说过该游戏,但例如像 Ogame 这样的网页游戏使用数据库来存储动作和内容。其他人使用平面文件,但这对我来说似乎不太好,因为我听说了很多有关安全问题的信息。可以肯定的是,设计良好的数据库应用程序比任何其他工具都要安全得多。真正的问题是另一个:php 不是为游戏而生的,而且可能永远也不会。

I have never heard about that game, but for example a web game like Ogame uses database to store movements and stuff. Others use flat file but that seems not great to me since I heard a lot about safety issues. It's sure that a well designed database application is way much safer than any other tools. The real problem is another: php is not made for game and probably never will.

岁月蹉跎了容颜 2024-10-16 02:41:46

如果 HTML 5 已启动并正在运行,您可能需要使用套接字连接到您的服务器(目前您已经可以使用 Flash 执行此操作)。基本上,您将在服务器上始终运行一个守护程序脚本,该脚本将侦听某个套接字并保留已连接用户的列表,然后当消息从其中一个已连接用户发送到套接字服务器时,套接字服务器将解析该消息,并将其转发给正确的连接用户。这样,内存中就不需要存储太大的内容。它会接收并立即发回消息。

客户端基本上会创建到套接字服务器的持久连接,并有一个处理程序来接收服务器守护程序发回的消息。服务器守护进程可以通过命令行运行 PHP(这样它就始终运行并且不会超时)。您可能需要为服务器创建一个 init shell 脚本,以便在启动时运行 PHP 守护程序脚本。您还需要从 PHP 脚本访问套接字(大多数共享服务器不允许),这意味着您可能需要 VPS(虚拟专用服务器)。

这是 HTML5 将为我们带来的目标,但目前最好的方法是使用 Flash(或者使用效率较低的方法之一,该方法不是真正的异步)。这是一个很大的主题,所以我不能讲太多细节,但我至少可以为你指出正确的方向。互联网上可能有这样的例子。

If HTML 5 were up and running, you would want to use a socket connection to your server for this (You can currently do this with Flash already). Basically, you would have a daemon script running on your server at all times, that would listen on a certain socket, and keep a list of connected users, then when a message is sent to the socket server from one of the connected users, the socket server would parse the message, and forward it to the correct connected users. This way nothing too large needs to be stored in memory. It would receive and immediately send back out the messages.

The client would basically create a persistent connection to the socket server, and have a handler to receive messages sent back by the server daemon. Server daemons can be PHP run via command line (so that it is always running and does not time out). You would want to make an init shell script for your server that runs the PHP daemon script on boot up. You would also need access to sockets from your PHP script (which is not allowed on most shared servers), meaning you would probably need a VPS (virtual private server).

This is the sort of thing that HTML5 will bring us towards, but your best but for now is using Flash instead (or use one of the less efficient methods that is not truly asynchronous). This is a large subject to get in to so I cannot go into too much detail, but I can at least point you in the right direction. There are probably examples of this somewhere on the internet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文