如何构建 PHP/MySQL 实时聊天?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4602499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:38:58  来源:igfitidea点击:

How can I build a PHP/MySQL live chat?

phpmysql

提问by Strawberry

I want to learn how to make one of these systems from scratch and I'm find a lot of junk links on Google. I really just want a simple tutorial for the most basic PHP and MySQL chat so I can understand the concept before I start messing with jQuery/AJAX.

我想学习如何从头开始制作这些系统之一,我在 Google 上发现了很多垃圾链接。我真的只是想要一个关于最基本的 PHP 和 MySQL 聊天的简单教程,这样我就可以在开始使用 jQuery/AJAX 之前理解这个概念。

回答by

PHP/MySQL chat 101:

PHP/MySQL 聊天 101:

1) user opens a browser
2) user enters addressin brower
3) browser sends HTTPrequest
4) serverrecieves HTTP request
5) server tells PHP interpreter to run PHP script
6) PHP script connectsto MySQL database
7) PHP script retrieves list of messages
8) PHP generates HTTP response made of HTML code with messages and form
9) Server sends HTTP response to browser
10) Browser draws HTML from HTTP response
11) User types new message and submits the form
12) Browser send HTTP POST request
13) ...

1) 用户打开浏览器
2) 用户在浏览器中输入地址
3) 浏览器发送HTTP请求
4)服务器收到 HTTP 请求
5) 服务器告诉 PHP 解释器运行PHP 脚本
6) PHP 脚本连接MySQL 数据库
7) PHP 脚本检索列表消息
8) PHP 生成由带有消息和表单的 HTML 代码组成的
HTTP 响应 9) 服务器向浏览器发送 HTTP 响应
10) 浏览器从 HTTP 响应中提取 HTML
11) 用户输入新消息并提交表单
12) 浏览器发送 HTTP POST 请求
13) ...

回答by Thilina Rubasingha

Found very interesting tutorial here

在这里找到了非常有趣的教程

http://tutorialzine.com/2010/10/ajax-web-chat-php-mysql/

http://tutorialzine.com/2010/10/ajax-web-chat-php-mysql/

回答by TJHeuvel

The live part of your chat is the tricky part, if you are just beginning i would skip that. Start by building a simple guestbook, and then add more features.

您聊天的实时部分是棘手的部分,如果您刚刚开始,我会跳过它。首先构建一个简单的留言簿,然后添加更多功能。

There are many tutorials available on how to build a guestbook, and even some free scripts where you can learn from.

有许多关于如何构建留言簿的教程,甚至还有一些可供您学习的免费脚本。

After you got your guestbook working, you could add features like auto-loading new messages to make it appear as live, using AJAX polling. What you basically do make an AJAX call to the server at a regular interval to get all the messages and display it on your page.

在您的留言簿工作后,您可以使用 AJAX 轮询添加诸如自动加载新消息以使其显示为实时消息等功能。您基本上会定期对服务器进行 AJAX 调用,以获取所有消息并将其显示在您的页面上。

回答by Codemwnci

A very simple starting point

一个非常简单的起点

Have a database table for a Message

有一个消息的数据库表

id | user | timestamp | message

And have a PHP page that sends an AJAX request to read any new messages.

并且有一个 PHP 页面,它发送一个 AJAX 请求来读取任何新消息。

This will involve checking the database to see if there are any messages since the time the request was received. If no messages, then loop, wait and try again in 100ms (or whatever you think is acceptable lag).

这将涉及检查数据库以查看自收到请求以来是否有任何消息。如果没有消息,则循环,等待并在 100 毫秒后重试(或任何您认为可接受的延迟)。

When the Ajax request returns a message (a JSON response would be best), output the user, time and message to the page using JQuery.

当 Ajax 请求返回消息(最好是 JSON 响应)时,使用 JQuery 将用户、时间和消息输出到页面。

回答by JasonY

If you must use php and mySQL for chat, atleast have a seperate table for unread messages. If you poll you will most likely need to check the database for new messages every 100ms or so. If your total message table is 1000 rows checking every 100ms will kill your server (especially if many users are connected). I would structure my mySQL database with a table for only unread messages and move them to a bigger table for old messages once read. That way your not checking a big table all the time.

如果您必须使用 php 和 mySQL 进行聊天,至少有一个单独的表用于未读消息。如果您进行轮询,您很可能需要每 100 毫秒左右检查一次数据库中是否有新消息。如果您的总消息表是 1000 行,每 100 毫秒检查一次将杀死您的服务器(特别是如果连接了许多用户)。我会用一个只包含未读消息的表来构建我的 mySQL 数据库,并将它们移动到一个更大的表中,以便在读取旧消息时使用。这样你就不会一直检查一张大桌子。

Even better is to use a cache database for unread messages like redis (facebook used memcacheD).

更好的是使用缓存数据库来存储未读消息,如 redis(facebook 使用 memcacheD)。

Even even better is to just not use php all together and use an event driven language with callbacks like node.js

更好的是不要一起使用 php,而是使用事件驱动的语言和像 node.js 这样的回调函数