XMPP登录不成功

自己用xmpp出现了Error Domain=GCDAsyncSocketErrorDomain Code=7 "Socket closed by remote peer" UserInfo=0x165c79a0 {NSLocalizedDescription=Socket closed by remote peer 的错误,弄了几天没弄好,找到了下面的文章。

但是在我试验此文章的时候,服务器修好了,终端就不用再改了,因此没试验出下面的方法是否可行,但老外也遇到了我同样的问题,他们就是用以下方法搞的,所以还是先记录下来吧。



转自:http://www.cnblogs.com/zhidao-chen/archive/2013/04/23/3037158.html

为了监听服务器是否有效,增加心跳监听。用XEP-0199协议,在XMPPFrameWork框架下,封装了 XMPPAutoPing 和 XMPPPing两个类都可以使用,因为XMPPAutoPing已经组合进了XMPPPing类,所以XMPPAutoPing使用起来更方便。

首先,导入头文件 

#import "XMPPAutoPing.h"

声明成员变量和属性

@interface XMPPConnectManeger : NSObject<XMPPAutoPingDelegate>

{

     XMPPAutoPing *_xmppAutoPing;

}

@property  (nonatomicretainXMPPAutoPing *xmppAutoPing;

在实现文件中

@synthesize xmppStream = _xmppStream;

@synthesize xmppAutoPing = _xmppAutoPing;

//初始化并启动ping

-(void)autoPingProxyServer:(NSString*)strProxyServer

{

    _xmppAutoPing = [[XMPPAutoPingallocinit];

    [_xmppAutoPingactivate:_xmppStream];

    [_xmppAutoPingaddDelegate:selfdelegateQueue:  dispatch_get_main_queue()];

    _xmppAutoPing.respondsToQueries = YES;

    _xmppAutoPing.pingInterval=2;//ping 间隔时间

    if (nil != strProxyServer)

    {

       _xmppAutoPing.targetJID = [XMPPJID jidWithString: strProxyServer ];//设置ping目标服务器,如果为nil,则监听socketstream当前连接上的那个服务器

    }

}

//卸载监听

 [_xmppAutoPing   deactivate];

  [_xmppAutoPing   removeDelegate:self];

   _xmppAutoPing = nil;

//ping XMPPAutoPingDelegate的委托方法:

- (void)xmppAutoPingDidSendPing:(XMPPAutoPing *)sender

{

    NSLog(@"- (void)xmppAutoPingDidSendPing:(XMPPAutoPing *)sender");

}

- (void)xmppAutoPingDidReceivePong:(XMPPAutoPing *)sender

{

    NSLog(@"- (void)xmppAutoPingDidReceivePong:(XMPPAutoPing *)sender");

}

 

- (void)xmppAutoPingDidTimeout:(XMPPAutoPing *)sender

{

    NSLog(@"- (void)xmppAutoPingDidTimeout:(XMPPAutoPing *)sender");

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个用于开发Java应用程序的开源框架,它简化了Spring应用程序的配置和部署过程。XMPP(Extensible Messaging and Presence Protocol)是一种基于XML的协议,用于实时通信,特别是用于即时聊天和在线状态管理。 如果你想在Spring Boot应用程序中使用XMPP协议,你可以使用Smack库。Smack是一个开源的XMPP客户端库,它提供了一组易于使用的API来处理XMPP连接、通信和扩展。你可以在你的Spring Boot项目中添加Smack依赖,并使用它来建立和管理XMPP连接,发送和接收消息等。 以下是一个简单的示例代码,演示了如何在Spring Boot中使用Smack进行XMPP通信: 首先,添加Smack依赖到你的项目的pom.xml文件中: ```xml <dependency> <groupId>org.igniterealtime.smack</groupId> <artifactId>smack</artifactId> <version>4.4.4</version> </dependency> ``` 然后,创建一个XMPPManager类来处理XMPP连接和通信: ```java import org.jivesoftware.smack.*; import org.jivesoftware.smack.chat.Chat; import org.jivesoftware.smack.chat.ChatManager; import org.jivesoftware.smack.chat.ChatMessageListener; import org.jivesoftware.smack.packet.Message; public class XMPPManager { private static final String XMPP_SERVER = "your_xmpp_server"; private static final int XMPP_PORT = 5222; private static final String XMPP_USERNAME = "your_username"; private static final String XMPP_PASSWORD = "your_password"; private AbstractXMPPConnection connection; public void connect() throws Exception { XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder() .setXmppDomain(XMPP_SERVER) .setHost(XMPP_SERVER) .setPort(XMPP_PORT) .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled) .build(); connection = new XMPPTCPConnection(config); connection.connect(); connection.login(XMPP_USERNAME, XMPP_PASSWORD); ChatManager chatManager = ChatManager.getInstanceFor(connection); chatManager.addChatListener(new ChatManagerListener() { public void chatCreated(Chat chat, boolean createdLocally) { chat.addMessageListener(new ChatMessageListener() { public void processMessage(Chat chat, Message message) { String from = message.getFrom(); String body = message.getBody(); // 处理收到的消息 } }); } }); } public void disconnect() { if (connection != null && connection.isConnected()) { connection.disconnect(); } } public void sendMessage(String to, String messageBody) throws Exception { ChatManager chatManager = ChatManager.getInstanceFor(connection); Chat chat = chatManager.createChat(to); chat.sendMessage(messageBody); } } ``` 在你的应用程序中,你可以使用XMPPManager类来建立连接、发送消息等。 这只是一个简单的示例,你可以根据你的实际需求进行扩展。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值