准备用openfire和strophe.js的组合来做项目的消息推送,现在先在这里测试下。
注:就写了一下遇到的一个小错误和页面源码(包含JS),其他的再补充。
1、在CentOS上安装openfire
1.1 下载openfire安装文件
1.2 命令安装
2、整合使用已有用户表
3、Web上面strophe.js的小测试
error-01:strophe.js登陆时,如果jid不设置域名,那么他默认是以匿名的方式登录,所以如果其他地方使用jid(不加@192.168.1.207)方式发送消息的话,该页面是接收不到的。
源代码:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>使用Shophe.js进行消息发送和消息监听 -- For Openfire</title>
<script type="text/javascript" src="jquery-2.1.3.min.js"></script>
<script type='text/javascript' src='strophe.js'></script>
<style type="text/css">
body {background-color: #ccc; padding: 20px;}
</style>
</head>
<body>
<div id='login' style='text-align: left'>
<form name='cred'>
<label for='username'>User表中的u_id:</label>
<input type='text' id='username' value="7@192.168.1.207" /><br/><br/>
<label for='password'>User表中的u_acc:</label>
<input type='password' id='password' value="testuser" /><br/>
<input type='button' id='connect' value='connect' />
</form>
</div>
<hr />
<div style="text-align: left">
<label for='toUsername'>tojid</label>
<input type='text' id='toUsername' value="6@192.168.1.207" /><br/><br/>
<label for='msg'>msg:</label>
<textarea id="msg">Hello world!!!</textarea>
<br/>
<input type='button' id='send' value='Send Message' />
</div>
<hr />
<div id="log"></div>
<h1>Latest content:</h1>
<div id="notifications"></div>
<!-- javascript -->
<script type="text/javascript">
var BOSH_SERVICE = 'http://192.168.1.207:7070/http-bind/';
var connection = null;
/**
* 创建连接的绑定方法
* @param status {number}
*/
function onConnect(status) {
if (status == Strophe.Status.CONNECTING) {
printMsg('Strophe is connecting.');
} else if (status == Strophe.Status.CONNFAIL) {
printMsg('Strophe failed to connect.');
$('#connect').get(0).value = 'connect';
} else if (status == Strophe.Status.DISCONNECTING) {
printMsg('Strophe is disconnecting.');
} else if (status == Strophe.Status.DISCONNECTED) {
printMsg('Strophe is disconnected.');
$('#connect').get(0).value = 'connect';
} else if (status == Strophe.Status.CONNECTED) {
printMsg('Strophe is connected.');
printMsg('ECHOBOT: Send a message to ' + connection.jid + ' to talk to me.');
connection.addHandler(listenMsgCallback, null, 'message', null, null, null);
connection.send($pres().tree());
}
}
/**
* 消息接收监听回调方法
* @param msg {object}
*/
function listenMsgCallback(msg) {
var to = msg.getAttribute('to'),
from = msg.getAttribute('from'),
type = msg.getAttribute('type'),
elems = msg.getElementsByTagName('body');
if (type == 'chat' && elems.length > 0) {
printMsg('ECHOBOT: I got a message from ' + from + ': ' + Strophe.getText(elems[0]));
}
return true;
}
/**
* 浏览器端像服务器发送消息
* @param toId {String}
* @param fromId {String}
* @param msg {String}
*/
function sendMsg(toId, fromId, msg) {
var reply = $msg({to: toId, from:fromId , type: 'chat'}).cnode(Strophe.xmlElement('body', '' ,msg));
connection.send(reply.tree());
printMsg('ECHOBOT: I sent ' + toId + ': ' + msg);
}
/**
* 打印消息到log div容器中显示
* @param msg {String}
*/
function printMsg(msg) {
$('#log').append('<div></div>').append(document.createTextNode(msg));
}
var DEF_USERNAME = "7@192.168.1.207", DEF_PWD = "testuser";
$(function(){
// 创建连接
connection = new Strophe.Connection(BOSH_SERVICE);
// 静默登录
// connection.connect(DEF_USERNAME, DEF_PWD, onConnect);
// 初始化页面时间
$('#connect').on('click', function(){
var $this = $(this);
if ($this.val() == 'connect') {
$this.val('disconnect');
// 使用自己填写的登录帐号
connection.connect($('#username').val(), $('#password').val(), onConnect);
} else {
$this.val('connect');
connection.disconnect();
}
});
$('#send').on('click', function(){
sendMsg($('#toUsername').val(), $('#username').val(), $('#msg').val());
});
});
</script>
</body>
</html>