如何使用PHP制作聊天室脚本? [英] How to make a chat room script with PHP?

查看:68
本文介绍了如何使用PHP制作聊天室脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个访客连接到 http://site.com/chat.php

他们每个人都可以编写文本并将其发送到chat.php,并将其立即显示在每个人的浏览器上( http: //site.com/chat.php )

They each can write and send a text message to chat.php and it displays instantly on everyone's browser (http://site.com/chat.php)

我必须使用数据库吗?我的意思是,对于这样的会话聊天室,AJAX或PHP缓冲区功能是否足够?

Do I have to use a database? I mean, is AJAX or PHP buffer capabilities enough for such a chat room on sessions?

不同用户的会话如何彼此共享数据?

How can sessions of different users share data from each other?

任何想法或见解将不胜感激,谢谢!

Any idea or insights will be appreciated, thanks!

感谢您的链接.但是我想要的是将数据推送到客户端浏览器的方法.不断刷新客户端浏览器(是否使用AJAX)是唯一的方法吗?同样,这里面临的挑战是不同的用户(例如2对1一对一)如何共享聊天文本?您如何存储它们?以及如何在两个客户端之间同步文本?最好不要使用数据库.

Thanks for the links. But what I want is the way to push data to a client browser. Is constantly refreshing client browser (AJAX or not) the only way? Also the challenge here is how different users, for example, 2, 1 on 1, share chat texts? How do you store them? And how do you synchronize the texts between the 2 clients? Not using a database preferably.

实际上,彼得D提到的 YShout 做得很好.它似乎并没有保持刷新浏览器的状态.但是我不明白它是如何将新消息推送到现有用户的窗口的.

Edit 2: Actually YShout mentioned by Peter D does this job pretty well. It doesn't seem to keep refresh the browser. But I don't understand how it pushes new messages to existing user's window.

推荐答案

(大约)有3个用于创建聊天应用程序的选项:

there are (roughly) 3 options for creating a chat application:

对前端使用flash/java和套接字,对后端使用支持套接字的编程语言.对于后端,我建议使用Java或python,因为它们具有多线程功能并且具有NIO功能.可以使用PHP来做到这一点(但是php不能真正实现高效的多线程,并且通常不适合这样做).如果您需要高性能,可能不是您想要的,这是一个选择.

use flash/java and sockets for the frontend and a socket-capable programming language for the backend. for the backend, i'd recommend java or python, because they are multithreading and NIO-capable. it's possible to do it with PHP (but php can't really do efficient multithreading and is generally not really suited for this). this is an option if you need high performance, and probably not what you're looking for.

使用ajax并拉动

在这种情况下,所有客户端都会不断(例如,每2秒一次)轮询是否发生了新情况.感觉很奇怪,因为您仅在这些时间间隔内得到答复.此外,这给您的服务器和带宽带来了很大压力.您知道应用程序使用此技术是因为浏览器会不断刷新.这是次优的解决方案.

in this case all clients are constantly (for example ever 2 seconds) polling if something new has happened. it feels strange because you only get responses at those intervals. additionally, it puts quite a strain on your server and bandwidth. you know an application uses this technique because the browser constantly refreshes. this is a suboptimal solution.

使用ajax并推送

这适用于多部分响应,并且在后端具有长期运行的(php-)脚本.并非最佳解决方案,但在大多数情况下,它比拉动要好,并且它可以工作,并且已在多个知名的聊天应用程序中使用.这种技术有时称为 COMET .

我的建议:如果您需要用于生产的聊天应用程序,请安装现有的.对聊天应用程序进行编程并不是那么简单.

my advise: if you need a chat app for production use, install an existing one. programming chat applications is not that easy.

如果您只是想学习它,请从一个简单的ajax/pull应用程序开始,然后尝试使用ajax并进行推送编程.

if you just want to learn it, start with a simple ajax/pull app, then try to program one using ajax and push.

是的,很可能您需要一个数据库,艰难的是,我成功地实现了一个非常简单的ajax/pull解决方案,该解决方案可与文本文件一起使用来娱乐(但我当然不会在生产中使用它!).

and yes, most probably you'll need a database, tough i successfully implemented a very simple ajax/pull solution that works with text files for fun (but i certainly wouldn't use it in production!).

(据我所知,但我很确定)没有服务器端后端(仅使用前端javascript)就无法创建聊天应用程序!

it is (to my knowledge, but i'm pretty sure) not possible to create a chat app without a server-side backend (with just frontend javascript alone)!

如果您想了解数据推送的完成方式,请在此处查看源代码: http: //wehrlos.strain.at/httpreq/client.html .异步multipart是您想要的:)

if you want to know how the data pushing is done, look at the source here: http://wehrlos.strain.at/httpreq/client.html. async multipart is what you want :)

function asSendSyncMulti() {
    var httpReq = new XMLHttpRequest();

    showMessage( 'Sending Sync Multipart ' + (++this.reqCount)  );

    // Sync - wait until data arrives
    httpReq.multipart   = true;     
    httpReq.open( 'GET', 'server.php?multipart=true&c=' + (this.reqCount), false );
    httpReq.onload = showReq;
    httpReq.send( null );
}

function showReq( event ) {
    if ( event.target.readyState == 4 ) {
        showMessage( 'Data arrives: ' + event.target.responseText );
    }
    else {
        alert( 'an error occured: ' + event.target.readyState );
    }

}

showReq每次都被称为 数据到达,而不仅仅是像常规的ajax请求中那样(我不在这里使用jquery或原型,因此代码有点肥胖-这确实很旧了: )).

showReq is called every time data arrives, not just once like in regular ajax-requests (i'm not using jquery or prototype here, so the code's a bit obese - this is really old :)).

这是服务器端部分:

<?php

    $c = $_GET[ 'c' ];

    header('Content-type: multipart/x-mixed-replace;boundary="rn9012"');

    sleep( 1 );

    print "--rn9012\n";
    print "Content-type: application/xml\n\n";
    print "\n";
    print "Multipart: First Part of Request " . $c . "\n";
    print "--rn9012\n";
    flush();

    sleep( 3 );

    print "Content-type: application/xml\n\n";
    print "\n";
    print "Multipart: Second Part of Request " . $c . "\n";
    print "--rn9012--\n";

?>

update2

关于数据库:如果您在后端拥有无共享的架构(如mod_php/cgi),则 definitley 需要某种种类的外部存储,例如数据库或文本文件.但是:您可以通过编写自己的http服务器来依赖内存(可以使用php,但我不建议您将它用于严肃的工作).这并不是很复杂,但是可能超出了您的问题范围^^

update2

regarding the database: if you've got a nothing-shared architecture like mod_php/cgi in the backend, you definitley need some kind of external storage like databases or textfiles. but: you could rely on memory by writing your own http server (possible with php, but i'd not recommend it for serious work). that's not really complicated, but probably a bit out of the scope of your question ^^

我弄错了!搞混了所有的东西,因为很长时间以来我实际上一直在做这样的事情.这是更正:

i made a mistake! got everything mixed up, because it's been a long time i actually did something like that. here are the corrections:

  1. 多部分响应仅适用于mozilla浏览器,因此用途有限. COMET并不意味着多部分响应.

  1. multipart responses only work with mozilla browsers and therefore are of limited use. COMET doesn't mean multipart-response.

COMET表示:传统的单部分响应,但是一直保持(具有无限循环和休眠),直到有可用数据为止.因此浏览器对每个操作都有1个请求/响应(在最坏的情况下),即使每秒钟都没有响应,也不会每x秒发送一个请求.

COMET means: traditional singlepart response, but held (with an infinite loop and sleep) until there is data available. so the browser has 1 request/response for every action (in the worst case), not one request every x seconds, even if nothing response-worthy happens.

这篇关于如何使用PHP制作聊天室脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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