`
baobeituping
  • 浏览: 1079667 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

smack操作openfire的API介绍(1)

 
阅读更多

1.连接openfire服务器

  /**
     * 建立即时通讯连接
     * @return
     */
   
  
    public boolean xMPPConnect() {
        boolean result = false;
        ConnectionConfiguration config = new ConnectionConfiguration(serverIp);

        //serverIp表示的是你openfire服务器地址
        config.setReconnectionAllowed(false);

        con = new XMPPConnection(config);

        try {
         //通过xmpp的链接跟OPENFIRE服务器链接,并且通过login的方法,将用户名和密码传递到服务器中,前提是服务器中必须先有关该登陆人员的ID和密码

            con.connect();
           
            con.login(“xxxx”, "xxxxx", "cti", true);
            logger.info(con.getUser() + " 登录即时通讯服务器成功!");
            //系统登陆了以后,XMPPConnection将用户的ID以"ID@jobcn-da5f2ccee/cti"这种方式保存,所有在取得当前用户的时候,要拆分字符串,ID表示的是你登录的账号,@后面的字符串表示的是你在openfire所这是的域名
            result = true;
        } catch (XMPPException ex) {
            logger.warning(person.getId() + " 登录即时通讯服务器失败!");
            result = false;
        }
        return result;
    }

2.初始化客户端信息

   /**
     * 初始化即时通讯
     */
    public void initXMPP() {
        roster = con.getRoster();
        roster.addRosterListener(this);
        PacketFilter filter = new PacketTypeFilter(Message.class);
        con.addPacketListener(this, filter);
        con.addConnectionListener(this);
        //默认开放的两个组"我的好友","最近联系人",这是openfire指定的,名字固定的就是"我的好友","最近联系人"
        RosterGroup rosterGroup_friends = roster.getGroup("我的好友");
        if (rosterGroup_friends == null) {
            rosterGroup_friends = roster.createGroup("我的好友");
        }
        RosterGroup rosterGroup_currentContract = roster.getGroup("最近联系人");
        if (rosterGroup_currentContract == null) {
            rosterGroup_currentContract = roster.createGroup("最近联系人");
        }
      
       
       
        FileTransferManager fileTransferManager = new FileTransferManager(con);
        fileTransferManager.addFileTransferListener(this);
    }

  

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics