XMPPHP GTalk状态 [英] XMPPHP GTalk Status

查看:84
本文介绍了XMPPHP GTalk状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 XMPPHP 来获取在线状态,但似乎无法获得任何具有$conn中的我的状态.这是我的代码段:

require_once('XMPPHP/XMPP.php');

$conn = new XMPPHP_XMPP('talk.google.com', 5222, 'xxxx@gmail.com', 'xxxxx', 'xmpphp', 'gmail.com', $printlog = false, $loglevel = XMPPHP_Log::LEVEL_INFO);

$conn->connect();
$conn->processUntil('session_start');
$conn->presence($status='Controller available.');
var_dump($conn); // this gives me a long output but nothing about status. ex: http://pastebin.com/yfs1V5Jb

我还尝试过getRoster()来查看朋友信息的列表(尽管我只对我的感兴趣),但没有运气.

有什么建议可以使我的工作正常吗?谢谢.

解决方案

在过去的两天里,我一直在努力解决此问题,最终找到了使事情正常进行的黑客方法.我在这里进行记录,因为这是我在寻找答案时最常出现的堆栈溢出问题.

$ conn-> presence()方法不仅将您的状态信息发送到服务器,而且还会将状态信息发送给服务器.它还从服务器收集每个联系人的状态信息.根本的问题是,在发送$ conn-> presence()命令时,必须给脚本一些时间来接收和处理来自服务器的信息.示例脚本全部使用$ conn-> processUntil('presence')进行此操作,但是由于某些原因,我没有将内容暂停足够长的时间来获取所有花名册信息.

要解决这个问题,我最终只是使用$ conn-> processTime(2),迫使事情等待2秒钟才能继续.就我的目的而言,这已经足够好了,但显然是一个hack.因此,以您的代码为例:

require_once('XMPPHP/XMPP.php');

$conn = new XMPPHP_XMPP('talk.google.com', 5222, 'xxxx@gmail.com', 'xxxxx', 'xmpphp', 'gmail.com', $printlog = true, $loglevel = XMPPHP_Log::LEVEL_VERBOSE);

$conn->connect();
$conn->processUntil('session_start');
$conn->presence($status='Controller available.');
$conn->processTime(2);

// now see the results
$roster = $conn->roster->getRoster();
print_r($roster); // you should now see roster array with presence info for each contact

要更具体地回答您的问题,您可以使用以下代码代替立即查看结果"下的代码:

$my_jid = 'user@domain.tld'; // put your jid here
$status = $conn->roster->getPresence($my_jid);
echo $status['show'];

这将显示您提供的jid的在线状态.

请注意,在此示例中,我还更改了构造函数以显示最详细的日志.这是帮助我完成这项工作的关键.

一个更好的解决方案显然是在框架中添加$ conn-> processUntil('roster')命令或类似的命令.但是由于该框架已经有5年没有更新了,所以这种情况不太可能发生.

希望这将为我节省尝试解决该问题所花费的时间.干杯.

I’m trying to get my online status using XMPPHP and I can’t seem to get anything that has my status from the $conn. Here is a snippet of my code:

require_once('XMPPHP/XMPP.php');

$conn = new XMPPHP_XMPP('talk.google.com', 5222, 'xxxx@gmail.com', 'xxxxx', 'xmpphp', 'gmail.com', $printlog = false, $loglevel = XMPPHP_Log::LEVEL_INFO);

$conn->connect();
$conn->processUntil('session_start');
$conn->presence($status='Controller available.');
var_dump($conn); // this gives me a long output but nothing about status. ex: http://pastebin.com/yfs1V5Jb

I also tried getRoster() to see a list of my friend’s info (although I’m only interested in mine) but no luck.

Any suggestions how I can get this to work? Thanks.

解决方案

I've been grappling with this issue for the last 2 days, and finally figured out a hack to get things to work. I'm documenting it here, because this was the stack overflow question that appeared most often for me while searching for answers.

The $conn->presence() method not only sends your presence info to the server; it also collects presence info for every contact from the server. The fundamental problem is that when you send the $conn->presence() command, you have to give the script time to receive and process this information from the server. The example scripts all use $conn->processUntil('presence') to do this, but for some reason for me that didn't pause things long enough to get all the roster information.

To get around this, I finally just used $conn->processTime(2), forcing things to wait 2 seconds before proceeding. This is good enough for my purposes, but is clearly a hack. So using your code as an example:

require_once('XMPPHP/XMPP.php');

$conn = new XMPPHP_XMPP('talk.google.com', 5222, 'xxxx@gmail.com', 'xxxxx', 'xmpphp', 'gmail.com', $printlog = true, $loglevel = XMPPHP_Log::LEVEL_VERBOSE);

$conn->connect();
$conn->processUntil('session_start');
$conn->presence($status='Controller available.');
$conn->processTime(2);

// now see the results
$roster = $conn->roster->getRoster();
print_r($roster); // you should now see roster array with presence info for each contact

To answer your question more specifically, you could use the following in place of the code under "now see the results":

$my_jid = 'user@domain.tld'; // put your jid here
$status = $conn->roster->getPresence($my_jid);
echo $status['show'];

That will display the online status for the jid you provide.

Note that in this example I also changed the constructor to display the most verbose log possible. This was key to helping me work through this.

A better solution would obviously be to add a $conn->processUntil('roster') command to the framework, or something like that. But since the framework hasn't been updated in 5 years, that's unlikely to happen.

Hopefully this will save someone the hours I lost trying to solve it. Cheers.

这篇关于XMPPHP GTalk状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆