来自 Csdn https://blog.csdn.net/nassue_sn
XMPP相关资料:https://blog.csdn.net/nassue_sn/article/category/6635474
Ejabberd相关资料:https://blog.csdn.net/nassue_sn/article/category/6601126
群聊离线消息实现:http://www.cnblogs.com/lovechengcheng/p/4083398.html
简单集群搭建:https://blog.csdn.net/yongche_shi/article/details/52956125
C#连接ejabberd2.1.x —— agsxmpp
注册新账户方法:(伪码)
XmppClientConnection xmppCon;
xmppCon.RegisterAccount =
true; // 设为true代表是新用户注册 xmppCon.OnRegistered += (sender) => {
// 新用户注册成功
};
xmppCon.OnRegisterError += (sender, e) => {
// 老用户重复注册
};
xmppCon.Open();
----------------------------------------- 下方内容因版本不匹配暂时废弃 --------------------------------------------------
PHP通过xmlrpc注册ejabberd用户
http://www.bubuko.com/infodetail-551255.html
正在研发一款社交软件,架构im使用了ejabberd作为xmpp服务器,于是遇到了如何通过php注册xmpp用户的问题。
解决方法有几个:
1.用xmpphp框架发送含<body>元数据的消息到服务器要求处理,这个可参考:http://blog.csdn.net/newjueqi/article/details/7864066
//1、测试文件 adduser.php
include_once './XMPPHP/XMPP.php';
$conn = new XMPPHP_XMPP($host, 5222, $xmppUserName, $xmppPassword, 'xmpphp', 'svn.zhaoduila.net', false);
try {
$conn->connect();
//发送好友请求、发送消息、删除好友 需要以下两行
$conn->processUntil('session_start');
$conn->presence();
//添加用户
// $conn->registerNewUser($xmppUserName, $xmppPassword, $email);
//发送消息
// $conn->message('liuruikang1@svn.zhaoduila.net', 'This is a test message!');
//发送好友请求
// $conn->addRosterContact($jid, $name);
//删除好友
$conn->deleteRosterContact($jid);
$conn->disconnect();
} catch (XMPPHP_Exception $e) {
die($e->getMessage());
}
//2、我是直接把方法追加到 XMPP.php 文件中的
/**
* Register a new user.
*
* @param $entity
* Entity we want information about
*/
public function registerNewUser($username, $password = NULL, $email){
if (!isset($password))
$password = $this->genRandomString(15);
$id = 'reg_' . $this->getID();
$xml = "<iq type='set' id='$id'>
<query xmlns='jabber:iq:register'>
<username>" . $username . "</username>
<password>" . $password . "</password>
<email>" . $mail . "</email>
<name></name>
</query>
</iq>";
$this->addIdHandler($id, 'register_new_user_handler');
$this->send($xml);
}
/**
* Handler for new user registration
*
* @param XML Object $xml
*/
public function register_new_user_handler($xml){
//dpm($xml);
//var_dump($xml);
switch ($xml->attrs['type']) {
case 'error':
$this->event('new_user_registered', 'error');
break;
case 'result':
// dpm($xml->subs);
$query = $xml->sub('query');
$username='';
$password='';
if(!is_array($query->subs)) {
foreach ($query->sub as $key => $value) {
switch ($value->name) {
case 'username':
$username = $value->data;
break;
case 'password':
$password = $value->data;
break;
}
}
}
$this->event('new_user_registered', array('jid' => $username . "@{$this->server}", 'password' => $password));
default:
$this->event('new_user_registered', 'default');
}
}
/**
* Add contact to your roster
*/
public function addRosterContact($jid, $name, $groups = array()){
// return if there is no jid specified
if(!$jid) return;
// set name to the jid if none is specified
if (!$name) { $name = $jid; }
$id = $this->getID();
$xml = "<iq type='set' id='$id'>";
$xml .= "<query xmlns='jabber:iq:roster'>";
$xml .= "<item jid='$jid' name='$name'>";
foreach ($groups as $group) {
$xml .= "<group>$group</group>";
}
$xml .= "</item>";
$xml .= "</query>";
$xml .= "</iq>";
$xml .= "<presence to='" . $jid . "' type='subscribe'/>";
$this->addIdHandler($id, 'add_roster_contact_handler');
$this->send($xml);
}
/**
* @param XML Object $xml
*/
protected function add_roster_contact_handler($xml) {
// do anything you wish here
$this->event('contact_added');
}
/**
*
* @param $jid
* Contact you wish to remove
*/
public function deleteRosterContact($jid) {
$id = $this->getID();
$xml = "<iq type='set' id='$id'>";
$xml .= "<query xmlns='jabber:iq:roster'>";
$xml .= "<item jid='" . $jid . "' subscription='remove' />";
$xml .= "</query>";
$xml .= "</iq>";
$this->addIdHandler($id, 'delete_roster_contact_handler');
$this->send($xml);
}
/**
*
* @param XML Object $xml
*/
protected function delete_roster_contact_handler($xml) {
// do any handling you wish here
$this->event('contact_removed');
}
function genRandomString($length){
$str='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$len=strlen($str)-1;
$randstr='';
for($i=0;$i<$length;$i++){
$num=mt_rand(0,$len);
$randstr .= $str[$num];
}
return $randstr;
}
2.使用php的xmpp库jaxl,其demo代码中包含一个register_user的脚本,通过shell调用:
php example/register_user.php YOUR_DOMAIN
即可生成用户,缺点是性能较差且慢,不建议
3.最优方法是使用ejabberd自带的命令行工具ejabberdctl来直接生成用户。网上的回答基本是通过修改sudo用户组权限来直接在php使用exec语句来执行此命令
$username = ‘tester‘;
$password = ‘testerspassword‘;
$node = ‘myserver.com‘;
exec(‘sudo /usr/sbin/ejabberdctl register ‘.$username.‘ ‘.$node.‘ ‘.$password.‘ 2>&1‘,$output,$status);
if($output == 0)
{
// Success!
}
else
{
// Failure, $output has the details
echo ‘<pre>‘;
foreach($output as $o)
{
echo $o."\n";
}
echo ‘</pre>‘;
}
需要在sudoer文件中添加ejabberd用户权限,相对不安全也比较麻烦,也不推荐。
4.其实ejabberd在最近的版本中已经集成了xmlrpc模块,通过该模块可直接访问4560端口使用ejabberd的一些内部命令。官网介绍地址:https://www.ejabberd.im/ejabberd_xmlrpc
修改ejabberd.yml配置文件:
listen:
-
port: 4560
ip: "::"
module: ejabberd_xmlrpc
access_commands:
xmlrpc_access:
commands: all
options: []
..
commands_admin_access: xmlrpc_access
..
access_rules:
xmlrpc_access:
- allow: xmlrpc_acl
..
api_permissions:
"console commands":
from:
- ejabberd_ctl
who: all
what: "*"
"admin access":
who:
- access:
- allow:
- acl: loopback
- acl: all
- oauth:
- scope: "ejabberd:admin"
- access:
- allow:
- acl: loopback
- acl: all
重启后 telnet HOST地址 4560 看能否接通,即说明xmlrpc已经可以用了:
关于php端的代码在介绍地址中已有提及,以下是php通过ejabberdctl注册一个用户的demo的代码:
$params=array('user'=>'someUser','host'=>'ejabberdHost','password'=>'somPassword');
$request = xmlrpc_encode_request('register', $params, (array('encoding' => 'utf-8')));
$context = stream_context_create(array('http' => array(
'method' => "POST",
'header' => "User-Agent: XMLRPC::Client mod_xmlrpc\r\n" .
"Content-Type: text/xml\r\n" .
"Content-Length: ".strlen($request),
'content' => $request
)));
$file = file_get_contents("http://127.0.0.1:4560", false, $context);
$response = xmlrpc_decode($file);
if (xmlrpc_is_fault($response)) {
trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
} else {
print_r($response);
}
打印后即生成注册成功的结果
Have a nice try! :)
Linux安装php-xmlrpc扩展:
cd php-5.4.16/ext/xmlrpc
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-xmlrpc=shared --with-libxmlrpc=/usr/local
make
make install
添加到php.ini扩展即可
更多参考:
https://github.com/processone/ejabberd/issues/1231
https://github.com/processone/ejabberd/issues/1749
https://github.com/processone/ejabberd/issues/1568
https://bugs.php.net/bug.php?id=19124
https://www.ejabberd.im/ejabberd_xmlrpc/
https://blog.csdn.net/kenkao/article/details/53333595
https://blog.csdn.net/u012930316/article/details/55251903
https://www.cnblogs.com/--00/p/erlang_into_style.html
posted on 2019-02-12 09:34
思月行云 阅读(909)
评论(0) 编辑 收藏 引用 所属分类:
Erlang