使用PHP的Ajax Instant Messenger

时间:2012-04-05 08:10:04

标签: php ajax jquery

尝试为我的网站创建一个AJAX IM ... 当行插入mysql DB时需要加载页面的一部分...任何人都可以帮助我...提前感谢

3 个答案:

答案 0 :(得分:1)

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
var  waittime=2000;
var intUpdate = null;


function verifDB(){

$.ajax({
   type: "POST",
   url: "verifdb.php",
   success: function(msg){
    alert(msg),; 
   }
 });

intUpdate = setTimeout("verifDB()", waittime);
}
verifDB();
</script>
每隔2000毫秒查询一次verifdb.php文件以检查数据库 你可以把你的文件放在requette verifdb.php中 你将在变量msg

中得到答案

答案 1 :(得分:1)

客户端

对于客户端的同步请求,您可以使用JQuery或纯Javascript XMLHTTPRequest

服务器端

我知道你已经指定了PHP,但我建议你检查google channels的工作方式,并在PHP中进行类似的实现。

由于检查有多个用户检查数据库的更新,我建议你使用memcache。

类似的东西:

$script_called_time = time();
while($memcache->get('last_message') < $script_called_time){
    usleep(100);
}

$result = $database->query("SELECT * FROM `messages` WHERE `date` > " . $script_called_time . "'");
...

这样就可以建立连接,并且当有任何...

时,用户将收到响应

答案 2 :(得分:0)

(function() {
  var chat = {
    messageToSend: "",
    messageResponses: [
      "I Love You",
      "I Wants to Kiss You.",
      'Hug Me!"',
      "Lets Sleep Together",
      "Lets go for a date",
      "Will you be physical with me?"
    ],
    init: function() {
      this.cacheDOM();
      this.bindEvents();
      this.render();
    },
    cacheDOM: function() {
      this.$chatHistory = $(".chat-history");
      this.$button = $("button");
      this.$textarea = $("#message-to-send");
      this.$chatHistoryList = this.$chatHistory.find("ul");
    },
    bindEvents: function() {
      this.$button.on("click", this.addMessage.bind(this));
      this.$textarea.on("keyup", this.addMessageEnter.bind(this));
    },
    render: function() {
      this.scrollToBottom();
      if (this.messageToSend.trim() !== "") {
        var template = Handlebars.compile($("#message-template").html());
        var context = {
          messageOutput: this.messageToSend,
          time: this.getCurrentTime()
        };

        this.$chatHistoryList.append(template(context));
        this.scrollToBottom();
        this.$textarea.val("");

        // responses
        var templateResponse = Handlebars.compile(
          $("#message-response-template").html()
        );
        var contextResponse = {
          response: this.getRandomItem(this.messageResponses),
          time: this.getCurrentTime()
        };

        setTimeout(
          function() {
            this.$chatHistoryList.append(templateResponse(contextResponse));
            this.scrollToBottom();
          }.bind(this),
          1500
        );
      }
    },

    addMessage: function() {
      this.messageToSend = this.$textarea.val();
      this.render();
    },
    addMessageEnter: function(event) {
      // enter was pressed
      if (event.keyCode === 13) {
        this.addMessage();
      }
    },
    scrollToBottom: function() {
      this.$chatHistory.scrollTop(this.$chatHistory[0].scrollHeight);
    },
    getCurrentTime: function() {
      return new Date()
        .toLocaleTimeString()
        .replace(/([\d]+:[\d]{2})(:[\d]{2})(.*)/, "$1$3");
    },
    getRandomItem: function(arr) {
      return arr[Math.floor(Math.random() * arr.length)];
    }
  };

  chat.init();

  var searchFilter = {
    options: { valueNames: ["name"] },
    init: function() {
      var userList = new List("people-list", this.options);
      var noItems = $('<li id="no-items-found">No items found</li>');

      userList.on("updated", function(list) {
        if (list.matchingItems.length === 0) {
          $(list.list).append(noItems);
        } else {
          noItems.detach();
        }
      });
    }
  };

  searchFilter.init();
})();

Messenger Using Jquery And PHP 如果您需要有关此答案的任何帮助,请随时通过pachauriashokkumar [at] gmail [dot] com与我联系。如果您需要使用CSS JS和HTML的完整代码,请给我发送电子邮件,我会通过电子邮件将代码发送给您

需要外部文件

  1. https://code.jquery.com/jquery-3.4.1.js
  2. https://cdn.jsdelivr.net/npm/handlebars@latest/dist/handlebars.js
  3. https://raw.githubusercontent.com/javve/list.js/v1.5.0/dist/list.min.js

使用JQuery和PHP演示程序的Messenger Here也是PenCode上这篇文章的作者,可以通过电子邮件pachauriashokkumar [at] gmail [dot] com进行澄清