Openfire + Strophe.js小测

准备用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>


转载于:https://my.oschina.net/u/1172699/blog/377737

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值