我正在尝试立即创建一个Strophe.js预绑定聊天系统。到目前为止我所做的工作是设置预绑定,将JID,RID,SID附加到Strophe连接,并将SID,RID和JID存储到cookie,以便重新加载页面,重新连接会话。
但我现在仍然面临着问题。第一个是只显示并正确处理发送到admin @ localhost的第一条消息。一旦我发送第二个消息,就没有更多答案了,尽管http-bind仍在工作并且正在等待。
修改 弄清楚第一个问题 返回true;在js内的onMessage2函数结束时丢失(代码已更改) 希望有人可以帮我找到我在第二个问题上遗漏的内容。
我遇到的第二个问题是我想启用类似聊天系统的Facebook。因此,聊天用户可以打开多个标签,并在每个标签/窗口上同时收到他的消息。怎么可能?
到目前为止,这是我的代码:
prebind.php
<?php
session_start();
//SESSION Check
include('XMPP/XmppPrebind.php');
/**
* Comment here for explanation of the options.
*
* Create a new XMPP Object with the required params
*
* @param string $jabberHost Jabber Server Host
* @param string $boshUri Full URI to the http-bind
* @param string $resource Resource identifier
* @param bool $useSsl Use SSL (not working yet, TODO)
* @param bool $debug Enable debug
*/
$username = 'admin';
$password = 'password';
$xmppPrebind = new XmppPrebind('localhost', 'http://example.local:5280/http-bind', 'balcony', false, false);
$xmppPrebind->connect($username, $password);
$xmppPrebind->auth();
$sessionInfo = $xmppPrebind->getSessionInfo(); // array containing sid, rid and jid
echo(json_encode($sessionInfo));
?>
的index.php
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type='text/javascript'
src='http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js'></script>
<script type="text/javascript" src="/jquery.cookie.js"></script>
<script type='text/javascript'
src='/strophe.js'></script>
<script type='text/javascript'
src='/anditworks.js'></script>
<title>Unbenanntes Dokument</title>
</head>
<body>
<div id='log'></div>
</body>
</html>
anditworks.js
// Strophe Connection Manager
var BOSH_SERVICE = 'http://example.local:5280/http-bind';
var connection = null;
function log(msg)
{
$('#log').append('<div></div>').append(document.createTextNode(msg));
}
$(document).ready(function () {
connection = new Strophe.Connection(BOSH_SERVICE);
// Uncomment the following lines to spy on the wire traffic.
//connection.rawInput = function (data) { log('RECV: ' + data); };
//connection.rawOutput = function (data) { log('SEND: ' + data); };
// Uncomment the following line to see all the debug output.
//Strophe.log = function (level, msg) { log('LOG: ' + msg); };
if(!$.cookie('jid')){
$.ajax({
url:"/prebind.php",
type:'POST',
dataType:"json",
success: function(response){
console.log(response);
$.cookie('jid', response.jid, {path:'/'});
$.cookie('rid', response.rid, {path:'/'});
$.cookie('sid', response.sid, {path:'/'});
connection.attach(response.jid, response.sid, response.rid, onConnect);
console.log($.cookie('jid'));
}
});
}else{
connection.attach($.cookie('jid'), $.cookie('sid'), $.cookie('rid'), onConnect);
}
});
function onConnect(status)
{
if (status == Strophe.Status.CONNECTING) {
log('Strophe is connecting.');
} else if (status == Strophe.Status.CONNFAIL) {
log('Strophe failed to connect.');
} else if (status == Strophe.Status.DISCONNECTING) {
log('Strophe is disconnecting.');
} else if (status == Strophe.Status.DISCONNECTED) {
log('Strophe is disconnected.');
$.removeCookie('rid');
$.removeCookie('sid');
$.removeCookie('jid');
$.ajax({
url:"/prebind.php",
type:'POST',
dataType:"json",
success: function(response){
console.log(response);
$.cookie('jid', response.jid,{path:'/'});
$.cookie('rid', response.rid,{path:'/'});
$.cookie('sid', response.sid,{path:'/'});
connection.attach(response.jid, response.sid, response.rid, onConnect);
console.log($.cookie('jid'));
}
});
} else if (status == Strophe.Status.CONNECTED) {
log('Connected');
}else if(status === Strophe.Status.ATTACHED){
connection.flush();
log('ECHOBOT: Send a message to ' + connection.jid +
' to talk to me.');
var pres = $pres().tree();
console.log(pres);
connection.send(pres);
connection.addHandler(onMessage2, null, 'message', null, null, null);
connection.xmlOutput = function (e) {
$.cookie('rid', (parseInt($(e).attr('rid'), 10) +1 ) )
LAST_USED_RID = $(e).attr('rid');
LAST_USED_SID = $(e).attr('sid');
log(' XMLOUTPUT INFO - OUTGOING RID=' + LAST_USED_RID + ' [SID=' + LAST_USED_SID + ']');
};
}
}
function onMessage2(msg) {
//Edited
if (type == "chat" && elems.length > 0) {**
console.log(msg);
var reply = $msg({to: 'testuser@localhost', from: 'admin@localhost', type: 'chat'}).cnode(Strophe.xmlElement('body', 'hello back'));
console.log(reply);
connection.send(reply.tree());
}
return true;
}
任何帮助表示感谢。
提前致谢。