现在的位置: 首页 > 综合 > 正文

gloox:连接服务器和接收消息

2017年12月21日 ⁄ 综合 ⁄ 共 1012字 ⁄ 字号 评论关闭

参考http://camaya.net/gloox/example

创建一个win32控制台程序
为方便起见,在gloox同一solution中创建一个win32控制台程序

完整测试程序

// SimpleClient.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "../src/messagehandler.h"
#include "../src/client.h"
#include "../src/message.h"

using namespace gloox;

class Bot : public MessageHandler
{
public:
	Bot()
	{
		JID jid( "user@host/resource" );
		j = new Client( jid, "pwd" );
		j->registerMessageHandler( this );
		j->connect();
	}
 
	virtual void handleMessage(const Message& stanza, MessageSession* session = 0 )
	{
		//Message msg(stanza.from(), "hello world" );
		//j->send( msg );
	}

private:
	Client* j;
};

int _tmain(int argc, _TCHAR* argv[])
{
	Bot b;
	return 0;
}

说明
1、JID:user是用户名、host是服务器名(登录到Openfire的管理控制台,Host Name的值),至于resource,我是取自Spark:点击“高级”

取下图中“资源”中的值

2、HandleMessage中的内容暂时没有编译过,注释掉了

解决链接错误
上述程序会产生链接错误
1、在VS2008上,只需要设置工程依赖,让这个控制台程序依赖gloox即可
2、在VS2010上,除了引用lib文件之外,还要在EXE的预编译宏里加上GLOOX_VERSION,否则会有些字符串被认为无定义
以上差别原因不明

测试
1、测试连接:准备两个用户A和B,两者互为好友,用Spark先登上,在运行该程序。如果正常,在程序启动30秒后,Spark上的好友图标会变绿。
2、测试收消息:在VS中,handleMessage处加断点,从Spark上发消息给好友,会触发断点。可以从参数Message对象中看到消息体。

抱歉!评论已关闭.