前段时间为了做聊天的功能差不多是崩溃了,就光处理下这些聊天的逻辑头都大了后来查资料发现可以用一些开源的聊天服务器来做,后期只需整合一下数据就OK了。基于多方面的考虑就选取了openfire做为聊天服务器,同时呢使用spark和如意通来进行开发测试
openfire和spark以及smack的下载地址是:http://www.igniterealtime.org/downloads/index.jsp
如意通的下载地址是:http://rm.rooyeetone.com/down.html
1.openfire聊天服务器的安装
这个基本上就像安装普通软件那样,最主要的还是openfire的配置,此服务器需要可能会使用到数据库(非必须),openfire也是支持主流的数据库服务器的如oracle,SQLServer,mysql等。只需要配置一下就OK了,如果是除mysql数据库的话还要把数据库驱动jar包放在openfire安装目录的lib文件夹里。这些准备工作做好之后就可以开始配置openfire了。这个我给大家找了个连接,自己仿照着做就好了。连接如下:
http://blog.csdn.net/shiyanhui66/article/details/6929729
这里需要注意一下域的配置,这个域以后将是账号的组成部分
配置成功后可以使用下载好的如意通或者spark测试一下能不能聊天,spark只需提供服务器,账号,密码就可以用了,如意通则需要更改一下配置,加上聊天服务器的端口号
聊天测试成功后就可以进行开发了,开发的话可以使用smack,strophejs等进行开发,这样问题也随之而来了,例如项目用的是tomcat但是聊天服务器却在其他地方,这样就会产生跨域的问题了。怎么解决这个问题呢,后来也是通过网上搜答案知道可以用nginx来做反向代理,先来看下nginx的作用吧:
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名
这个服务器地启停很简单的,下面是简单的使用
(1)启动nginx服务器
要停止的话就使用命令:nginx -s quit , 很简单吧懒惰的话可以去任务管理器里干掉进程就OK了
下面就是nginx的配置了,为了解决跨域的问题我们就必须配置一下代理,nginx此时还承担着连接openfire服务器的重任,因此我们要在nginx根目录下conf文件夹下找到nginx.conf,打开后配置一下代理以及openfire的绑定,文件配置如下
在server下面更改配置,内容如下
location / { proxy_pass http://127.0.0.1:8080/; } location /http-bind { proxy_pass http://127.0.0.1:7070/http-bind/; proxy_buffering off; proxy_redirect off; proxy_read_timeout 120; proxy_connect_timeout 120; }
var BOSH_SERVICE = 'http://127.0.0.1/http-bind/'; var connection = null; function log(msg) { $('#log').append('<div></div>').append(document.createTextNode(msg)); } function onConnect(status) { if (status == Strophe.Status.CONNECTING) { log('Strophe is connecting.'); } else if (status == Strophe.Status.CONNFAIL) { log('Strophe failed to connect.'); $('#connect').get(0).value = 'connect'; } else if (status == Strophe.Status.DISCONNECTING) { log('Strophe is disconnecting.'); } else if (status == Strophe.Status.DISCONNECTED) { log('Strophe is disconnected.'); $('#connect').get(0).value = 'connect'; } else if (status == Strophe.Status.CONNECTED) { log('Strophe is connected.'); log('ECHOBOT: Send a message to ' + connection.jid + ' to talk to me.'); connection.addHandler(onMessage, null, 'message', null, null, null); connection.send($pres().tree()); } } function onMessage(msg) { var to = msg.getAttribute('to'); var from = msg.getAttribute('from'); var type = msg.getAttribute('type'); var elems = msg.getElementsByTagName('body'); if (type == "chat" && elems.length > 0) { var body = elems[0]; log('ECHOBOT: I got a message from ' + from + ': ' + Strophe.getText(body)); var reply = $msg({to: from, from: to, type: 'chat'}) .cnode(Strophe.copyElement(body)); connection.send(reply.tree()); log('ECHOBOT: I sent ' + from + ': ' + Strophe.getText(body)); } // we must return true to keep the handler alive. // returning false would remove it after it finishes. return true; } $(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); }; $('#connect').bind('click', function () { var button = $('#connect').get(0); if (button.value == 'connect') { button.value = 'disconnect'; connection.connect($('#jid').get(0).value, $('#pass').get(0).value, onConnect); } else { button.value = 'connect'; connection.disconnect(); } }); });
但是实际的开发我们可能还要整合现有数据,mysql的话整合起来比较简单,oracle主要就是连接字符串难写一点。openfire也给我们提供了集成现有数据库的文档,一步一步按照文档做下去根本就行不通,这个我们就需要去修改一下openfire的ofproperty数据表
这个是部分的修改,还有下面这个部分
这里需要说明的是,虽然官方的集成文档虽然写的有问题但是还是建议大家看一下,集成的时候需要具备的就是熟悉现有的数据表结构。第一个和第二个红框以及倒数第二个是整合我们的现有数据写的sql,其余部分则是相应的修改,此时我们将不使用默认的provider了而是改成我们所需要的。整合的时候如果出问题了就看看数据库是不是少了什么配置。方便起见我吧这些sql弄出来了,大家照着修改下就可以整合成功了。
insert into OFPROPERTY (NAME, PROPVALUE) values ('xmpp.domain', 'will'); insert into OFPROPERTY (NAME, PROPVALUE) values ('xmpp.auth.anonymous', 'false'); insert into OFPROPERTY (NAME, PROPVALUE) values ('admin.authorizedJIDs', 'chenruiyin2@will,admin@will'); insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcUserProvider.loadUserSQL', 'SELECT name,email FROM member WHERE member_name=?'); insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcUserProvider.allUsersSQL', 'SELECT member_name FROM member'); insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcUserProvider.searchSQL', 'SELECT member_name FROM member WHERE'); insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcUserProvider.nameField', 'name'); insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcAuthProvider.passwordSQL', 'SELECT sparkpassword FROM member WHERE member_name=?'); insert into OFPROPERTY (NAME, PROPVALUE) values ('provider.admin.className', 'org.jivesoftware.openfire.admin.DefaultAdminProvider'); insert into OFPROPERTY (NAME, PROPVALUE) values ('provider.group.className', 'org.jivesoftware.openfire.auth.JDBCGroupProvider'); insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcUserProvider.userCountSQL', 'SELECT COUNT(*) FROM member'); insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcUserProvider.emailField', 'email'); insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcAuthProvider.passwordType', 'plain'); insert into OFPROPERTY (NAME, PROPVALUE) values ('register.inband', 'true'); insert into OFPROPERTY (NAME, PROPVALUE) values ('passwordKey', 'ITLXuR7vnS853ic'); insert into OFPROPERTY (NAME, PROPVALUE) values ('xmpp.socket.ssl.active', 'true'); insert into OFPROPERTY (NAME, PROPVALUE) values ('provider.lockout.className', 'org.jivesoftware.openfire.lockout.DefaultLockOutProvider'); insert into OFPROPERTY (NAME, PROPVALUE) values ('provider.auth.className', 'org.jivesoftware.openfire.auth.JDBCAuthProvider'); insert into OFPROPERTY (NAME, PROPVALUE) values ('provider.vcard.className', 'org.jivesoftware.openfire.vcard.DefaultVCardProvider'); insert into OFPROPERTY (NAME, PROPVALUE) values ('xmpp.session.conflict-limit', '0'); insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcGroupProvider.groupCountSQ', 'SELECT count(*) From v_group'); insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcGroupProvider.allGroupsSQL', 'Select Name FROM v_group'); insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcGroupProvider.userGroupsSQL', 'SELECT groupName FROM group_menber Where menber_id=?'); insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcGroupProvider.descriptionSQL', 'Select Explain From v_group WHERE groupName=?'); insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcGroupProvider.loadMembersSQL', 'SELECT menber_id FROM group_menber Where menber_id!=''402881ec3af924b1013af926748d0001'''); insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcGroupProvider.loadAdminsSQL', 'SELECT menber_id FROM group_menber Where menber_id=''402881ec3af924b1013af926748d0001'''); insert into OFPROPERTY (NAME, PROPVALUE) values ('register.password', 'true'); insert into OFPROPERTY (NAME, PROPVALUE) values ('provider.user.className', 'org.jivesoftware.openfire.user.JDBCUserProvider'); insert into OFPROPERTY (NAME, PROPVALUE) values ('provider.securityAudit.className', 'org.jivesoftware.openfire.security.DefaultSecurityAuditProvider'); insert into OFPROPERTY (NAME, PROPVALUE) values ('locale.timeZone', 'GMT'); insert into OFPROPERTY (NAME, PROPVALUE) values ('update.lastCheck', '1354241336597'); insert into OFPROPERTY (NAME, PROPVALUE) values ('xmpp.httpbind.scriptSyntax.enabled', 'true'); insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcProvider.driver', 'oracle.jdbc.driver.OracleDriver'); insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcUserProvider.usernameField', 'member_name'); insert into OFPROPERTY (NAME, PROPVALUE) values ('httpbind.enabled', 'true'); insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcProvider.connectionString', 'jdbc:oracle:thin:hylogistics2/hylogistics2@192.168.1.210:1521:ORCL');
到这里的话整合就成功了,以上的修改也可以登录到openfire控制台进行操作,具体的细节大家自己去摸索。安装或整合出问题可以给我写评论我会给大家回复。