How to implement a chat room using Jquery/PHP?

前端 未结 10 1752
抹茶落季
抹茶落季 2020-12-02 07:12

I\'m looking to implement a chat room using PHP/Javascript (Jquery) with both group chat and private chat features.

The problem is how to continually update the inte

10条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-02 07:53

    Chat with PHP/AJAX/JSON

    I used this book/tutorial to write my chat application:

    AJAX and PHP: Building Responsive Web Applications: Chapter 5: AJAX chat and JSON.

    It shows how to write a complete chat script from scratch.


    Comet based chat

    You can also use Comet with PHP.

    From: zeitoun:

    Comet enables web servers to send data to the client without having any need for the client to request it. Therefor, this technique will produce more responsive applications than classic AJAX. In classic AJAX applications, web browser (client) cannot be notified in real time that the server data model has changed. The user must create a request (for example by clicking on a link) or a periodic AJAX request must happen in order to get new data fro the server.

    I'll show you two ways to implement Comet with PHP. For example:

    1. based on hidden "; } else if (navigator.appVersion.indexOf("KHTML") != -1) { // for KHTML browsers comet.connection = document.createElement('iframe'); comet.connection.setAttribute('id', 'comet_iframe'); comet.connection.setAttribute('src', './backend.php'); with (comet.connection.style) { position = "absolute"; left = top = "-100px"; height = width = "1px"; visibility = "hidden"; } document.body.appendChild(comet.connection); } else { // For other browser (Firefox...) comet.connection = document.createElement('iframe'); comet.connection.setAttribute('id', 'comet_iframe'); with (comet.connection.style) { left = top = "-100px"; height = width = "1px"; visibility = "hidden"; display = 'none'; } comet.iframediv = document.createElement('iframe'); comet.iframediv.setAttribute('src', './backend.php'); comet.connection.appendChild(comet.iframediv); document.body.appendChild(comet.connection); } }, // this function will be called from backend.php printServerTime: function (time) { $('content').innerHTML = time; }, onUnload: function() { if (comet.connection) { comet.connection = false; // release the iframe to prevent problems with IE when reloading the page } } } Event.observe(window, "load", comet.initialize); Event.observe(window, "unload", comet.onUnload);

      Method 2: AJAX non-returning request

      You need the same as in method 1 + a file for dataexchange (data.txt)

      Now, backend.php will do 2 things:

      1. Write into "data.txt" when new messages are sent
      2. Do an infinite loop as long as "data.txt" file is unchanged
      
      

      The frontend script (index.html) creates the

      tags hat will contains the chat messages comming from "data.txt" file, and finally it create a "comet" javascript object that will call the backend script in order to watch for new chat messages.

      The comet object will send AJAX requests each time a new message has been received and each time a new message is posted. The persistent connection is only used to watch for new messages. A timestamp url parameter is used to identify the last requested message, so that the server will return only when the "data.txt" timestamp is newer that the client timestamp.

      
      
      
        Comet demo
        
        
      
      
      
      


      Alternatively

      You can also have a look at other chat applications to see how they did it:

      • http://hot-things.net/?q=blite - BlaB! Lite is an AJAX based and best viewed with any browser chat system that supports MySQL, SQLite & PostgreSQL databases.

      • Gmail/Facebook Style jQuery Chat - This jQuery chat module enables you to seamlessly integrate Gmail/Facebook style chat into your existing website.

      • Writing a JavaScript/PHP Chat Server - A tutorial

      • CometChat - CometChat runs on standard shared servers. Only PHP + mySQL required.

提交回复
热议问题