您的位置:首页 > 运维架构 > Nginx

利用strophe.js +nginx 在openfire服务器注册用户

2015-02-06 10:25 253 查看
strophe是xmpp协议的javascript脚本解释库。

nginx用来解决跨域的问题。

要实现在openfire注册其实很简单,如果通过走后台实现(smack),只需要在前端利用ajax调用,如果使用smack,需要引入的jar包:

smack-bosh-4.0.4.jar

smack-core-4.0.4.jar

smack-core-dns-4.0.4.jar

smack-debug-4.0.4.jar

smack-experimental-4.0.4.jar

smack-extensions-4.0.4.jar

smack-jingle-4.0.4.jar

smack-legacy-4.0.4.jar

smack-resolver-dnsjava-4.0.4.jar

smack-resolver-javax-4.0.4.jar

smack-tcp-4.0.4.jar

kxml2.jar

注册方法实现:

public int register(String username,String password,String email){
xmppConnection = getConnection();
//可以直接改登陆用户的信息(如果是username的值必须和该用户的用户名相同)
Registration r = new Registration();
Map<String, String> attributes = new HashMap<String, String>();
attributes.put("username", username);
attributes.put("password", password);
attributes.put("email", email);
//				attributes.put("name", "name@192.168.1.100");
//添加用户,要设置type类型为set
r.setType(IQ.Type.SET);
r.setAttributes(attributes);
//过滤器,用来过滤由服务器返回的信息(即得到注册信息的内容)
PacketFilter packetFilter = new AndFilter(new PacketIDFilter(r.getPacketID()), new PacketTypeFilter(IQ.class));
PacketCollector collector = xmppConnection.createPacketCollector(packetFilter);
System.out.println(r);
try {
xmppConnection.sendPacket(r);
IQ result = (IQ) collector.nextResult();
if(result == null) {
System.out.println("服务器没有返回任何信息");
disconnect();
return 0;
} else if(result.getType().toString().equals("result")){
System.out.println("注册成功");
disconnect();
return 1;
}else if(result.getType().toString().equals("error")){
if(result.getError().toString().equalsIgnoreCase("conflict")){
System.out.println("用户名称已存在");
disconnect();
return 2;
} else {
disconnect();
System.out.println("注册失败");
return 3;
}
}else{
disconnect();
return 4;
}
} catch (NotConnectedException e) {
e.printStackTrace();
disconnect();
return 5;
}
}


但是如果需要实现前端strophe来注册,需要处理xmpp的一系列流验证,网上资料很少,最后在国外的论坛上找到一个strophe注册插件,其中有一个错误,修改之后可以用:

已经修改的注册插件下载地址附上:http://download.csdn.net/detail/huling2361/8428015
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: