上次我必须解决这个问题我使用了这个架构:
Entlarge image
网络服务器提供 JavaScript / jQuery 或 flash 聊天。
聊天开始后,客户端在 1 秒内向服务器询问新消息。
1 秒轮询的替代方案
如果这对您来说太慢了,请查看websockets。
http://martinsikora.com/nodejs-and-websocket-simple-chat-tutorial
http://demo.cheyenne-server.org:8080/chat.html
但是 php 无法提供 Websockets。那里你需要改变 php + apchache agaist node.js 或 java。
普通 HTTP PHP 方法
在 PHP 中,您将连接到 PsyBnc,并为您轮询来自支持者的消息。
PsyBnc 是一个 IRC 机器人。
不直接连接到 XMPP 或 BitlBee 的原因是那些协议不喜欢摆动的连接,断开与 PHP 的连接。因为您无法使会话保持活动状态,所以您需要一些用于经常和短连接的东西。这是 PsyBnc。
我会使用这样的东西:
http://pear.php.net/package/Net_SmartIRC/download
<?php
session_start();
$message = $_GET['message'];
$client_name = $_GET['client_name'];
if (empty($_SESSION['chat_id'])) {
$_SESSION['chat_id'] = md5(time(). mt_rand(0, 999999));
}
if (empty($_SESSION['supporter'])) {
// how do you select the supporter?
// only choose a free?
// We send first message to all supporter and the first who grapped got the chat (where only 3 gues)
}
$irc_host = "127.0.0.1";
$irc_port = 6667; // Port of PsyBnc
$irc_password = "password_from_psy_bnc";
$irc_user = "username_from_psy_bnc";
include_once('Net/SmartIRC.php');
class message_reader
{
private $messages = array();
public function receive_messages(&$irc, &$data)
{
// result is send to #smartirc-test (we don't want to spam #test)
$this->messages[] = array(
'from' => $data->nick,
'message' => $data->message,
);
}
public function get_messages() {
return $this->messages;
}
}
$bot = &new message_reader();
$irc = &new Net_SmartIRC();
$irc->setDebug(SMARTIRC_DEBUG_ALL);
$irc->setUseSockets(TRUE);
$irc->registerActionhandler(SMARTIRC_TYPE_QUERY|SMARTIRC_TYPE_NOTICE, '^' . $_SESSION['chat_id'], $bot, 'receive_messages');
$irc->connect($irc_host, $irc_port);
$irc->login($_SESSION['chat_id'], $client_name, 0, $irc_user, $irc_password);
$irc->join(array('#bitlbee'));
$irc->listen();
$irc->disconnect();
// Send new Message to supporter
if (!empty($message)) {
$irc->message(SMARTIRC_TYPE_QUERY, $_SESSION['supporter'], $message);
}
echo json_encode(array('messages' => $bot->get_messages()));
将支持即时通讯工具连接到 PHP
我们已经与 PsyBnc 建立了 IRC 连接,现在我们需要将消息从 IRC 发送到 ICQ、XMPP、GOOGLE TALK、MSN、YAHOO、AOI...
这是一个很好的解决方案,名为BitlBee。
BitlBee 提供了一个 IRC 服务器,可以在几乎所有即时消息协议之间传输消息。通过给这些帐户起别名。例如,您的系统只需要 1 个在 google talk、icq 的服务器帐户……以及您的支持者到这些帐户的好友列表。现在 BitleBee 将提供您的 boddylist 作为 irc 聊天。