PHP游戏服务器,多个TCP客户端? [英] PHP game server, multiple TCP clients?

查看:40
本文介绍了PHP游戏服务器,多个TCP客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作基于网络浏览器的多人游戏.我已经确定 websockets 是处理通信的最佳方式,因为它具有实时性.客户端使用 HTML5 画布渲染游戏,使用 websocket 与主机通信.

I'm making a web browser based multiplayer game. I've determined that websockets are the best way to handle communications given its realtime nature. The client uses a HTML5 canvas to render the game and websockets to communicate with the host.

我选择使用 PHP 来托管游戏,因为它似乎是托管提供商的首选.我以前没有用过 PHP,但在 Java 中用 websockets 做过类似的事情,但严重依赖多线程.

I've elected to use PHP for hosting the game as it seems to be preferred by hosting providers. I haven't used PHP before but have done similar things with websockets in Java, but relying heavily on multithreading.

我一直在看一些关于具有多个客户端的 php 套接字的教程;但他们中的大多数人会为每个客户分叉新流程.由于我将有一个不断运行的游戏循环,我认为这不合适.

I've been looking at a few tutorials on php sockets with multiple clients; but most of them do things like fork off new processes for each client. Since I'll have a constantly running game loop I don't think this is suitable.

我想要实现的是一种在每个客户端连接时为其分配端口、侦听新客户端、与当前客户端列表交换数据以及一起运行游戏循环的方法.

What I'm trying to achieve is a means of assigning ports to each client as they connect, listening for new clients, exchanging data with the current list of clients and running the game loop all together.

我需要帮助的地方是:

  • 如何查找端口并将其分配给新客户端,通知该端口的客户端,并在断开连接时进行清理.
  • 如何在不阻塞游戏循环的情况下执行上述操作以及所有其他套接字事务.以部分块的形式接受来自客户端的消息并仅对完整消息进行操作是可以接受的.

谁能给我一些关于如何实现这些目标的技术建议?我认为这一切看起来对 PHP 要求不高,但如果我错了,请纠正我!

Can anyone give me some technical advice on how to achieve these goals? I don't think this all looks like too much to ask of PHP but correct me if I'm wrong!

我理想中想要实现服务器端的一些伪代码.没有任何功能应该阻止:数组客户端;

Some pseudocode of what I'd ideally like to achieve server-side. None of the functions should block: Array clients;

while(gamerunning)
{
    CheckForNewClients();
    GetStatusFromClients();
    DoGameUpdate();
    SendGameStateToClients();
}

[更新]对于任何有兴趣的人,我创建了一个支持 Web 套接字的专用应用程序(特别是使用 Java 和TooTallNates"Web 套接字库)而不是实际的 Web 服务,因为它似乎更有意义,尽管顺便说一句,似乎大多数 Web 浏览器都已将 Web由于安全问题,垃圾箱中的套接字.

[Update] For anyone interested, I created a dedicated application supporting web sockets (specifically using Java, and 'TooTallNates' web socket library) rather than an actual web service as it seemed to make more sense, though incidentally it seems most web browsers have since slung web sockets in the bin due to security issues.

推荐答案

我不建议将 PHP 用于此类应用程序.PHP 不正式支持多线程,并且在不确定的时间段内(如服务器)运行 PHP 脚本并不是真正的广告功能.

I wouldn't suggest using PHP for this type of application. PHP doesn't officially support multithreading and running a PHP script for an undefined period of time (like a server) isn't really an advertised feature.

当然你可以尝试创造历史:)

Of course you could try and make history :)

(如有错误请指正)

这篇关于PHP游戏服务器,多个TCP客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆