通过套接字进行 PHP-Flash 通信
亲爱的开发者,今天的美好时光!
我不是任何类型的网络编程专家,但碰巧我面临着需要在 php 上开发套接字服务器(无法使用 Java)的 flash 多人浏览器游戏(标准功能,如位置、团队战斗等)。
主要问题是TCP是点对点协议,它完全占用给定的端口。
当然,可以创建某种队列来管理与套接字的连接,但这种解决方案似乎不是最合适的解决方案。
在我看来,使用“公平”端口间隔(例如从 2000 到 2200)是更合适的解决方案,因为一个请求可能需要很长时间才能执行,而且玩家不会乐意排队等待。
但如何实施这种“端口间隔”策略呢? 我看到的解决方案是:
每个端口启动 php-script(嘿嘿,200 个启动的脚本!);
以某种方式将初始进程分叉为新进程(200个已处理?也不好),每个端口一个;
以- 额外的 while 循环,监听所有端口(看起来很糟糕);
- 使用线程或类似的东西(问题是 php 是单线程的,据我所知;pcntl?)。
但不知怎的,我不喜欢其中任何一个,或者至少不知道如何以最好的方式实现它们。
在每个时间单位无延迟地处理来自多个用户的多个请求的最佳现有策略是什么,以及如何在 php 中实现该策略?我们有自己的 Debian 服务器,因此可以使用任何所需的 php 扩展。
任何有关此类系统的开发、规划和实施的建议都将受到高度赞赏。
谢谢你!
编辑1: 顺便说一句,我忘了提及一些额外的细节。
例如,如果我们尝试开发聊天应用程序,我们需要为每个用户提供某种固定(我的意思是持久)连接。例如,我们有 80 个用户在聊天,然后其中一个用户发布了一条消息,该服务器尝试处理并发送给所有其他连接的用户,同时将一个条目放入历史文件或类似的文件中。
在这种情况下,每 10 秒轮询服务器一次来自 80 个用户的新消息是疯狂的,因此持久连接的需求变得显而易见。 但考虑到并非所有请求都会立即处理,我不知道实现此类操作的最佳方法是什么。
Good time of the day, dear developers!
I am not any kind of network programming pro, but it happened that I have faced necessity to develop socket-server on php (no way for using Java) for flash multiplayer browser-game (standard features like locations, team battles, etc).
The main problem is that TCP is point-to-point protocol and it completely occupies given port.
Of course it is possible to create some kind of queue, which will manage connections to the socket, but this solution doesn't seems to be the fittest one.
It seems to me that using interval of "fair" ports (from 2000 to 2200, for example) is more fitting solution, because one request may take a lot of time to execute and players won't be happy to wait in queue.
But how can I implement this "port inteval" strategy?
The solutions that I see are:
launcing php-script per every port (he-he, 2 hundreds of launched scripts!);
somehow forking the initial process to new processes (2 hundreds of processed? Not nice too), one for every port;
- additional while-loop, which listens all the ports (looks very bad);
- using threading or something like that (the problem is that php is single-threaded, as far as I know; pcntl?).
But somehow I don't like any of them, or at least don't know how to implement them in the best possible way.
What is the best existing strategy to handle multiple requests from multiple users per time unit without delays, and how to implement this strategy in php? We have our own Debian-server, so it is possible to use any required php extensions.
Any advice about development, planning and implementation of such kind of systems is highlhy appreciated.
Thank You!
Edit 1:
By the way, I've forgotten to mention some extra details.
For example, if we are trying to develop chat application, we need some sort of fixed (I mean persistent) connections for each user. For example, we have 80 users in chat, and then one of them posted a message, which server tries to handle and send to all other connected users, also putting an entry to the history file or something like that.
In this situation polling server for new messages every 10 seconds from each of 80 users is craziness, so the need in persistent connection gets obvious.
But I don't know what is the best way to implement something like this, considering that not all requests are handled instanlty.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否查看过 http://www.smartfoxserver.com/ 或 http://www. electro-server.com/ ?
have you looked at http://www.smartfoxserver.com/ or http://www.electro-server.com/ ?
Flash 多用户通信的最佳方式是 RTMP。寻找 FMS 或 Red5。它不是用 php 编写的,但我认为这是正确的方法。
Best way for multi user communication for Flash is RTMP. Look for FMS or Red5. It's not in php, but I think this is correct way.