网上一搜一大陀ejabberd安装配置的,扩展开发的资料少之又少,写个笔记记录一下。

xmpp protocal: http://xmpp.org/xmpp-protocols/xmpp-extensions/

安装与配置:

http://www.process-one.net/docs/ejabberd/guide_en.html

默认配置文件在 /etc/ejabberd/ejabberd.cfg

1

git clone git://github.com/processone/ejabberd.git ejabberd
cd ejabberd
git checkout -b 2.1.x origin/2.1.x

2

sudo apt-cache search libyaml

sudo apt-get install libyaml-dev

3. cd ejabberd/src

./configure

make

报错:erl_driver.h: No such file or directoryERLANG_CFLAGS=

vi Makefile发现ERLANG_CFLAGS和ERLANG_LIBS所指目录不对,居然使用了我编译erlang时的源文件而没有使用/usr/local/...

解决方法也很简单,把编译erlang用的源文件(otp-src-R16B01)删除,再次./configure即可。因为这样configure的时候就不会找到

4.

sudo make install

5.

The files and directories created are, by default:

/etc/ejabberd/
Configuration directory:
ejabberd.yml
ejabberd configuration file
ejabberdctl.cfg
Configuration file of the administration script
inetrc
Network DNS configuration file
/lib/ejabberd/
ebin/
Erlang binary files (*.beam)
include/
Erlang header files (*.hrl)
priv/
Additional files required at runtime
bin/
Executable programs
lib/
Binary system libraries (*.so)
msgs/
Translation files (*.msgs)
/sbin/ejabberdctl
Administration script (see section 4.1)
/share/doc/ejabberd/
Documentation of ejabberd
/var/lib/ejabberd/
Spool directory:
.erlang.cookie
Erlang cookie file (see section 5.3)
acl.DCD, ...
Mnesia database spool files (*.DCD, *.DCL, *.DAT)
/var/log/ejabberd/
Log directory (see section 7.1):
ejabberd.log
ejabberd service log
erlang.log
Erlang/OTP system log

6.

ejabberdctl start

ejabberdctl status
The node ejabberd@localhost is started with status: started
ejabberd is running in that node ejabberdctl stop

接下来需要使用客户端进行简单的测试,网上搜了一下,选择了smack。

首先写一个注册功能:

  1. public boolean register(String username, String password) {
  2. try {
  3. ConnectionConfiguration config = new ConnectionConfiguration(
  4. "192.168.17.102", 5222);
  5. connection = new XMPPConnection(config);
  6. connection.connect();
  7. System.out.println("connect ok, user,pass:" + username + " "
  8. + password);
  9. AccountManager am = connection.getAccountManager();
  10. am.createAccount(username, password);
  11. connection.disconnect();
  12. } catch (Exception ex) {
  13. ex.printStackTrace();
  14. return false;
  15. }

发现connection ok但是无法注册。检查配置文件,vi /etc/ejabberd/ejabberd.cfg

  1. {mod_register, [
  2. %%
  3. %% Protect In-Band account registrations with CAPTCHA.
  4. %%
  5. {captcha_protected, true},
  6. %%
  7. %% Set the minimum informational entropy for passwords.
  8. %%
  9. %%{password_strength, 32},
  10. %%
  11. %% After successful registration, the user receives
  12. %% a message with this subject and body.
  13. %%
  14. {welcome_message, {"Welcome!",
  15. "Hi.\nWelcome to this XMPP server."}},
  16. %%
  17. %% When a user registers, send a notification to
  18. %% these XMPP accounts.
  19. %%
  20. %%{registration_watchers, ["admin1@example.org"]},
  21. %%
  22. %% Only clients in the server machine can register accounts
  23. %%
  24. {ip_access, [{allow, "127.0.0.0/8"},
  25. {deny, "0.0.0.0/0"}]},
  26. %%
  27. %% Local c2s or remote s2s users cannot register accounts
  28. %%
  29. %%{access_from, deny},
  30. {access, register}
  31. ]},

将{ip_access, [..., {deny, "0.0.0.0/0"}]}改为allow,再次运行注册代码,发现依然失败。然后改为使用如下代码,注册成功:

  1. public void register2(String username, String password, String email,
  2. String fullName) {
  3. try {
  4. ConnectionConfiguration config = new ConnectionConfiguration(
  5. "192.168.17.102", 5222);
  6. connection = new XMPPConnection(config);
  7. connection.connect();
  8. Registration reg = new Registration();
  9. reg.setType(IQ.Type.SET);
  10. reg.setTo(connection.getServiceName());
  11. // attributes.put("username", username);
  12. // attributes.put("password", password);
  13. // reg.setAttributes(attributes);
  14. Map<String, String> attr = new HashMap<String, String>();
  15. attr.put("username", username);
  16. attr.put("password", password);
  17. attr.put("email", email);
  18. attr.put("name", fullName);
  19. reg.setAttributes(attr);
  20. PacketFilter filter = new AndFilter(new PacketIDFilter(
  21. reg.getPacketID()), new PacketTypeFilter(IQ.class));
  22. PacketCollector collector = connection
  23. .createPacketCollector(filter);
  24. connection.sendPacket(reg);
  25. IQ result = (IQ) collector.nextResult(SmackConfiguration
  26. .getPacketReplyTimeout());
  27. // Stop queuing results
  28. collector.cancel();// 停止请求results(是否成功的结果)
  29. if (result == null) {
  30. System.out
  31. .println("xmppMainRegiter”, “No response from server.");
  32. } else if (result.getType() == IQ.Type.ERROR) {
  33. if (result.getError().toString()
  34. .equalsIgnoreCase("conflict(409)")) {
  35. System.out.println("xmppMainRegiter" + " IQ.Type.ERROR: "
  36. + result.getError().toString());
  37. System.out.println("conflict");
  38. } else {
  39. System.out.println("xmppMainRegiter" + " IQ.Type.ERROR: "
  40. + result.getError().toString());
  41. System.out.println("注册失败");
  42. }
  43. } else if (result.getType() == IQ.Type.RESULT) {
  44. System.out.println("注册成功");
  45. }
  46. } catch (Exception ex) {
  47. ex.printStackTrace();
  48. }
  49. }

关于ejabberd各模块的测试,因为mod_*均为gen_*, 可以使用rpc:call/4进行测试某些功能。但是erlang节点的互通需要-setcookie,

vi /var/lib/ejabberd/.erlang.cookie

可以查看ejabberd节点的cookie值,这里查到为FBSFDTMHVUJTXLNXFUE

启动erlang shell, erl -sname test -setcookie FBSFDTMHVUJTXLNXFUE

> rpc:call(ejabberd@localhost, ejabberd_captcha, is_feature_available, [], 10).
  > true

成功。

ejabberd的参考资料少之又少,刚才研究了下如何获取验证码。

首先要在/etc/ejabberd/ejabberd.cfg中开启验证。

mod_register:

  1. {captcha_protected, true}
  1. %%%.   =======
  2. %%%'   CAPTCHA
  3. %%
  4. %% Full path to a script that generates the image.
  5. %%
  6. {captcha_cmd, "/lib/ejabberd/priv/bin/captcha.sh"}.
  7. %%
  8. %% Host for the URL and port where ejabberd listens for CAPTCHA requests.
  9. %%
  10. {captcha_host, "192.168.17.102:5280"}.
  11. %%
  12. %% Limit CAPTCHA calls per minute for JID/IP to avoid DoS.
  13. %%
  14. {captcha_limit, 5}.

然后看了下mod_register和ejabberd_captcha的代码,把注册请求的IQ.Type.SET改为IQ.Type.GET即可获取验证码信息,得到xml响应如下:

  1. <iq id="hLfY6-0" from="192.168.17.102" type="result">
  2. <query xmlns="jabber:iq:register">
  3. <instructions>You need a client that supports x:data and CAPTCHA to register</instructions>
  4. <x xmlns="jabber:x:data" type="form">
  5. <instructions>Choose a username and password to register with this server</instructions>
  6. <field var="FORM_TYPE" type="hidden">
  7. <value>urn:xmpp:captcha</value>
  8. </field>
  9. <field label="User" var="username" type="text-single">
  10. <required/>
  11. </field>
  12. <field label="Password" var="password" type="text-private">
  13. <required/>
  14. </field>
  15. <field type="fixed">
  16. <value>If you don't see the CAPTCHA image here, visit the web page.</value>
  17. </field>
  18. <field var="captchahidden" type="hidden">
  19. <value>workaround-for-psi</value>
  20. </field>
  21. <field label="CAPTCHA web page" var="url" type="text-single">
  22. <value>http://192.168.17.102:5280/captcha/2824232941/image</value>
  23. </field>
  24. <field var="from" type="hidden">
  25. <value>192.168.17.102</value>
  26. </field>
  27. <field var="challenge" type="hidden">
  28. <value>2824232941</value>
  29. </field>
  30. <field var="sid" type="hidden">
  31. <value>hLfY6-0</value>
  32. </field>
  33. <field label="Enter the text you see" var="ocr">
  34. <required/>
  35. </field>
  36. </x>
  37. <data xmlns="urn:xmpp:bob"/>
  38. </query>
  39. </iq>

验证码图片为

  1. http://192.168.17.102:5280/captcha/2824232941/image

Over!

控制台输出:

vi /sbin/ejabberdctl

  1. start ()
  2. {
  3. checknodenameusage
  4. [ "$?" -eq 0 ] && echo "\nERROR: The node '$ERLANG_NODE' is already running." && return 1
  5. $EXEC_CMD "$ERL \
  6. $NAME $ERLANG_NODE \
  7. -noinput -detached \
  8. -pa $EJABBERD_EBIN_PATH \
  9. -mnesia dir \"\\\"$SPOOLDIR\\\"\" \
  10. $KERNEL_OPTS \
  11. -s ejabberd \
  12. -sasl sasl_error_logger \\{file,\\\"$SASL_LOG_PATH\\\"\\} \
  13. $ERLANG_OPTS $ARGS \"$@\""
  14. }

去掉-noinput -detached

ejabberd服务端开发笔记的更多相关文章

  1. Swift3.0服务端开发(一) 完整示例概述及Perfect环境搭建与配置(服务端+iOS端)

    本篇博客算是一个开头,接下来会持续更新使用Swift3.0开发服务端相关的博客.当然,我们使用目前使用Swift开发服务端较为成熟的框架Perfect来实现.Perfect框架是加拿大一个创业团队开发 ...

  2. Swift3.0服务端开发(五) 记事本的开发(iOS端+服务端)

    前边以及陆陆续续的介绍了使用Swift3.0开发的服务端应用程序的Perfect框架.本篇博客就做一个阶段性的总结,做一个完整的实例,其实这个实例在<Swift3.0服务端开发(一)>这篇 ...

  3. 在线教学、视频会议 Webus Fox(2) 服务端开发手册

    上次在<在线教学.视频会议软件 Webus Fox(1)文本.语音.视频聊天及电子白板基本用法>里介绍了软件的基本用法.本文主要介绍服务器端如何配置.开发. 1. 配置 1.1 IIS配置 ...

  4. Android 服务端开发之开发环境配置

    Android 服务端开发之开发环境配置 这里是在Eclipse的基础上安装PhpEclipse插件方法,PHPEclipse是Eclipse的 一个用于开发PHP的插件.当然也可以采用Java开发a ...

  5. Swift3.0服务端开发(三) Mustache页面模板与日志记录

    本篇博客主要介绍如果在Perfect工程中引入和使用Mustache页面模板与日志记录系统.Mustache页面模板类似于PHP中的smarty模板引擎或者Java中的JSTL标签.当然Mustach ...

  6. 如何有效快速提高Java服务端开发人员的技术水平?

    我相信很多工作了3-5年的开发人员都会经常问自己几个问题: 1.为什么总是感觉技术没有质的提高? 2.如何能够有效和快速的提高自身的技术水平? 3.如何进入到一个牛逼的大公司,认识牛逼的人? 这篇文章 ...

  7. WCF服务端开发和客户端引用小结

    1.服务端开发 1.1 WCF服务创建方式 创建一个WCF服务,总是会创建一个服务接口和一个服务接口实现.通常根据服务宿主的不同,有两种创建方式. (1)创建WCF应用程序 通过创建WCF服务应用程序 ...

  8. socket服务端开发之测试使用threading和gevent框架

    socket服务端开发之测试使用threading和gevent框架 话题是测试下多线程和gevent在socket服务端的小包表现能力,测试的方法不太严谨,也没有用event loop + pool ...

  9. 个人公众号服务端开发Demo

    公众号出来很久了,也可以个人申请.知道公众号的服务端开发其实很简单,接口调用封装,数据存取,不外如是. 人一旦懒了,真的是 “无可救药” 了...现简单描述晚到的公众号HelloWorld 思路 公众 ...

随机推荐

  1. SharePoint\O365 "See also"功能 and site feature 激活\禁用小节

    博客地址:http://blog.csdn.net/FoxDave 最近因为问题发现了SharePoint的"See also"功能,该功能是由SharePoint自动的Feat ...

  2. Android平板电脑开发— — —碎片

    碎片是一种可以嵌入在活动中的UI片段,它能让程序更加合理与充分地使用大屏幕的空间,碎片通常都是在平板电脑开发中才会使用 简单实例 左碎片布局 <?xml version="1.0&qu ...

  3. JS中数据类型及原生对象简介

    js是一种专门设计用来给网页增加交互性的编程语言,它的技术体系包含了一下几个方面: 1.JavaScript核心语言定义:包括数据类型,变量,常量,运算符,语句等. 2.原生对象和内置对象 3.浏览器 ...

  4. marquee|各种文字滚动代码(适用公告)

    marquee|各种文字滚动代码(适用公告)  

  5. B/S和C/S测试的区别

        B/S(Brower/Server)以访问方式为主,包含客户端浏览器.web应用服务器.数据库服务器的软件系统.一般的B/S结构,都是多层架构的,有界面层.业务逻辑层.数据层.由于这种结构不需 ...

  6. SQL 变量

    1.变量可以暂时储存数据 --定义变量:  declare @xxx int --变量赋值:  set @xxx=1  select @xxx=3 --变量的使用:  print @xxx 2.--全 ...

  7. Android 中pid与uid的作用与区别

    PID:为Process Identifier, PID就是各进程的身份标识. 程序一运行系统就会自动分配给进程一个独一无二的PID.进程中止后PID被系统回收,可能会被继续分配给新运行的程序,但是在 ...

  8. select、poll、poll的比较(转)

    原文地址:http://www.cnblogs.com/xuxm2007/archive/2011/08/15/2139809.html select.poll.epoll的比较 linux提供了se ...

  9. Crypto++ 动态链接编译与实例测试

    测试用例的来源<Crypto++入门学习笔记(DES.AES.RSA.SHA-256)> 解决在初始化加密器对象时触发异常的问题: CryptoPP::AESEncryption aesE ...

  10. NSString字符串

    要把 “2011-11-29” 改写成 “2011/11/29”一开始想用ios的时间格式,后来用NSString的方法搞定. [string stringByReplacingOccurrences ...