简体   繁体   English

在聊天应用程序中向Apache服务器请求

[英]Request to Apache server in chat app

I'm writing a chat application for joomla (apache server) and use this construction to emulate long-polling (server side): 我正在为joomla(Apache服务器)编写一个聊天应用程序,并使用此构造来模拟长轮询(服务器端):

function get_messages($last_id) {
   $time = time();
   while((time() - $time) < 25) {       
      $sql = 'SELECT * FROM #__messages WHERE `id` >'.intval($last_id);
      $db->setQuery($sql);
      $rows = $db->loadAssocList();
      if (count($rows)>0) { 
         echo 'JSON STRING HERE';  
      } else {
     flush();
  } 
   usleep(5000000);       
   }
}

How Can I optimize this part of code. 我如何优化这部分代码。 Should I use infinite looping or should I avoid while construction? 我应该使用无限循环还是在构造时避免使用? P/S: I know Apache is not best choice to write chat app for and node.js is better. P / S:我知道Apache并不是编写聊天应用程序的最佳选择,而node.js更好。

Thanks! 谢谢!

Infinite loops are never a good idea because they hammer your server resources. 无限循环绝不是一个好主意,因为它们会影响您的服务器资源。 You are better off having JS providing the intermittent polling to your get_messages function. 最好让JS为您的get_messages函数提供间歇性轮询。 Use a timeout and embed the script on any page that shows the messages. 使用超时并将脚本嵌入显示消息的任何页面上。

I'm going to answer based on the limited information I've got to help you in the broadest way possible following industry standards. 我将根据有限的信息来回答,这些信息将按照行业标准以最广泛的方式为您提供帮助。 You need to not code in the way you currently are because it is very inefficient and quite frankly dangerous. 您不必按照当前的方式进行编码,因为它效率很低而且非常危险。

Here is the mootools code required to run an intervaled polling (I've used Mootools as you said you're using Joomla, I've assumed you're using 1.6+ as 1.5 is EOL this month): 这是运行间隔轮询所需的mootools代码(我说过您正在使用Joomla时就使用了Mootools,我假设您正在使用1.6+,因为本月的EOL为1.5):

//this sets how often you want to update (in milliseconds).
setInterval('chatPoll()',2000);
//this function essentially just grabs the raw data
//from the specified url and dumps it into the specified div
function chatPoll()
{
   var unixTimestamp Math.round(new Date().getTime() / 1000)
   var req = new Request({
      method: 'get',
      url: $('ajax-alert').get('http://www.yoururltoupdate.com/file.php?last=' + (unixTimestamp-2),
      data: { 'do' : '1' },
      onComplete: function(response) { response.inject($('my-chat-wrapper')); }
   }).send();
}

Your PHP file should look something look like this: 您的PHP文件应如下所示:

get_messages($_GET['last']);
function get_messages($last_id) {

      $sql = 'SELECT * FROM #__messages WHERE `id` >'.intval($last_id);
      $db->setQuery($sql);
      $rows = $db->loadAssocList();
      if (count($rows)>0) { 
         echo json_encode($rows);
      }     
}

I haven't fully tested this code but it should work and if not will definitely help answer your query as to how what you're trying to do should be achieved rather than the way you originally posted. 我尚未对该代码进行完整的测试,但是它应该可以工作,如果不能,则肯定会帮助您回答有关如何实现您的尝试而不是最初发布方式的查询。 If you really wanted to get fancy you could check out node.js as well. 如果您真的想花哨的话,也可以查看一下node.js。 There is also tons of extensions for Joomla which work as chat mediums for support purposes if that's what you were after. Joomla还有大量扩展,如果您需要的话,它们可以用作聊天介质以提供支持。

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

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