简体   繁体   English

服务器到客户端的PHP连接

[英]server to client connection with PHP

So my question is: What would be the best way, to make a server-client connection via PHP? 所以我的问题是:通过PHP建立服务器-客户端连接的最佳方法是什么? My first approach (what I'm using right now) is to do it with AJAX. 我的第一种方法(我现在正在使用的方法)是使用AJAX。 (with jQuery) (使用jQuery)

To be a bit more specific: I have a lobby, and a game mode, and my top priorities are the chat, and the in-game users. 更具体一点:我有一个大厅和一个游戏模式,我的首要任务是聊天和游戏中的用户。

What I'm doing right now: I have a PHP file, that updates a record in the database for every user in-game, and another file that cheks this regularly. 我现在正在做什么:我有一个PHP文件,该文件为游戏中的每个用户更新数据库中的一条记录,而另一个文件则定期对此进行检查。 (let's say in every 5 seconds) The problem with this, that there're a lot of mysql calls, lots of ajax call, and sometimes when the ajax can't load in time, the record won't update in time, ergo -> the user gets disconnected. (比方说,每隔5秒一次)出现的问题是,有很多mysql调用,许多ajax调用,有时当ajax无法及时加载时,记录不会及时更新,因此->用户断开连接。

Note that I need this for my dissertation so I have time to do my homework about this even if you give me some links. 请注意,在我的论文中我需要这样做,因此即使您给我一些链接,我也有时间对此做功课。

Any answers and tips are welcome! 欢迎任何答案和提示! Thanks in advance. 提前致谢。

You could start by setting a time limit on the AJAX call, and handling any error by double-checking, rather than just disconnecting the user. 您可以通过设置AJAX调用的时间限制开始,并通过仔细检查来处理任何错误,而不仅仅是断开用户连接。

One call every five seconds isn't that much, really. 实际上,每五秒钟打一次电话并不算多。

You can look at Comet , which uses "long polling" to keep the connection open, allowing for communication without polling. 您可以查看Comet ,它使用“长时间轮询”来保持连接打开,从而允许进行通信而无需轮询。

The issue is that this requires more resources on the server since it may have to keep a large number of connections open simultaneously. 问题在于这需要服务器上更多的资源,因为它可能必须同时保持大量连接的打开。 If you're on a hosted server with multiple server, managing state can become a headache as you either need to stick the user to a single IP/server or manage the state across servers. 如果您在具有多台服务器的托管服务器上,则状态管理可能会变得令人头疼,因为您需要将用户限制在单个IP /服务器上,或者需要跨服务器管理状态。 You're trading off one set of resources for another. 您正在用一套资源换另一套资源。

One this you may want to consider (aside from Coment) is keeping to DB memory-resident with REDIS or MEMCACHED to greatly improve DB performance. 您可能要考虑的一项问题(除了Coment之外)是将数据库内存保留在REDISMEMCACHED上,以大大提高数据库性能。

An alternative is to use shared memory - PHP: Semaphore, Shared Memory and IPC . 一种替代方法是使用共享内存-PHP:Semaphore,Shared Memory和IPC
A potential problem could be limitations on shared memory imposed by the system. 潜在的问题可能是系统对共享内存的限制。 Also, access to the shared resources might get complicated since you have to make sure any they are guarded against simultaneous read/write. 另外,由于必须确保防止共享资源同时读取/写入,因此访问共享资源可能会变得复杂。 However, this should be faster then using a database. 但是,这应该比使用数据库更快。 The data can be flushed to a DB once every minute or half a minute, etc. 数据可以每分钟或每半分钟刷新一次到数据库。

If the game mode requires a lot of requests/responses per second, then creating a separate program to handle game logic would be a better solution, instead of using PHP. 如果游戏模式每秒需要大量请求/响应,那么创建一个单独的程序来处理游戏逻辑将是一个更好的解决方案,而不是使用PHP。
Another option is to make the game stateful (if that's possible). 另一种选择是使游戏具有状态(如果可能)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM