简体   繁体   English

使用PHP / MySQL网站作为服务器创建桌面聊天应用程序

[英]Creating a desktop chat application with a PHP/MySQL web site acting as the server

Most desktop chat applications probably use a specific software running on a distant dedicated box. 大多数桌面聊天应用程序可能使用在远程专用盒子上运行的特定软件。 However, is there a way a chat application could be backed by a simple PHP/MySQL web site? 但是,有没有一种方法可以通过简单的PHP / MySQL网站支持聊天应用程序? If so, what general advice would you give in terms of logging in and sending/receiving text? 如果是这样,您将在登录和发送/接收文本方面给出什么一般建议?

There are two problems with the architecture (PHP/MySQL) you propose. 您提出的体系结构(PHP / MySQL)存在两个问题。

1) Chat is two-way traffic. 1)聊天是双向流量。 This means that you need some way to push a message to your users. 这意味着您需要某种方式将消息推送给您的用户。 One option is to have your clients poll constantly for new messages (generating a lot of traffic). 一种选择是让您的客户不断轮询新消息(产生大量流量)。 Another way is to wait with answering a HTTP request until a message exists. 另一种方法是等待回答HTTP请求,直到消息存在。 This is dependent on the timeouts in the network (timeout from proxy server, timeout from HTTP server). 这取决于网络中的超时(代理服务器超时,HTTP服务器超时)。 But it would work. 但这会起作用。

2) You have to be able to communicate messages between PHP instances. 2)您必须能够在PHP实例之间传递消息。 When you load a HTTP page, a single PHP process is launched which transforms the PHP code into HTML. 加载HTTP页面时,将启动一个PHP流程,该流程会将PHP代码转换为HTML。 You are proposing to use MySQL as a common data store between those processes. 您建议将MySQL用作这些进程之间的公用数据存储。 This means that you would need to have your PHP code constantly poll the database. 这意味着您将需要让您的PHP代码不断轮询数据库。 They also need to mark which messages have been transferred to the client and which haven't. 他们还需要标记哪些消息已经传输到客户端,哪些没有。 Maybe messages are lost along the way, there is no way to be sure. 也许消息一路上丢失了,无法确定。

Because of these two problems, chat programs are better left to a specific architecture. 由于这两个问题,聊天程序最好留给特定的体系结构。

I myself have programmed a simple chat application using PHP and SQL databases. 我本人已使用PHP和SQL数据库编写了一个简单的聊天应用程序。 You can check it out here . 您可以在这里查看

The way I did it was kept a database of messages. 我做的方式是保存消息数据库。 The user stays on one webpage. 用户停留在一个网页上。 When the user types a message and presses enter, an AJAX POST request is made to a PHP script. 当用户键入消息并按Enter时,将向PHP脚本发出AJAX POST请求。 This script then inserts a row into the database with the message data etc. Then, every 1 second, the newest 8 messages are requested (via AJAX), and then put into a div on the page. 然后,此脚本将行与消息数据等一起插入数据库。然后,每1秒钟(通过AJAX)请求最新的8条消息,然后将其放入页面的div中。 All this happens every minute. 这一切每分钟发生一次。

There are a number of problems with this design. 这种设计存在许多问题。

The most obvious is bandwidth consumption. 最明显的是带宽消耗。 Regardless of whether there are new messages, they are requested every second anyway. 无论是否有新消息,无论如何都要每秒请求它们。 I myself thought of a new concept. 我自己想到了一个新概念。 Every second, instead of requesting all messages, it requests the message count. 每秒而不是请求所有消息,而是请求消息计数。 It then compares the local count to the server count. 然后,它将本地计数与服务器计数进行比较。 If they are different, it requests the new messages. 如果它们不同,它将请求新消息。 You could even go further by finding the difference between the message count's, and then requesting each new message in a for loop, and then inject them into the DOM. 您甚至可以通过找到消息计数之间的差异,然后在for循环中请求每条新消息,然后将它们注入DOM来走得更远。

Here's how I would go about doing it: 这是我要做的事情:

Create a mySQL database to act as a message history. 创建一个mySQL数据库以充当消息历史记录。 But use another means of relaying messages. 但是,请使用其他中继消息的方法。 Such as memcache. 如内存缓存。

Since we're a little way off web sockets, a messenger system requires a browser to poll or long-poll / comet to get any new messages. 由于我们离Web套接字还有点距离,因此Messenger系统需要浏览器进行轮询或长时间轮询/彗星才能获取任何新消息。 You don't want to be polling mySQL for this. 您不想为此轮询mySQL。 Instead - new messages go into a memcache entry which is polled by recipients. 取而代之的是-新邮件进入由收件人轮询的内存缓存条目。

If you then wanted to save these messages, you could periodically save them to mySQL. 如果随后要保存这些消息,则可以定期将它们保存到mySQL。

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

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