smack +openfire 聊天开发(二)连接,注册用户、发送单聊

/**
     * 连接XMPP服务
     * 
     * @return
     * @throws XmppStringprepException
     */
    private AbstractXMPPConnection getXmpptcpConnection() throws XmppStringprepException {
        XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder().setXmppDomain(openfireServer)
                .setSecurityMode(XMPPTCPConnectionConfiguration.SecurityMode.disabled)
                .setUsernameAndPassword("admin", "admin").setHost(openfireServer).setDebuggerEnabled(false).build();
        AbstractXMPPConnection con = new XMPPTCPConnection(config);
//        SASLAuthentication.blacklistSASLMechanism("SCRAM-SHA-1");
//        SASLAuthentication.blacklistSASLMechanism("DIGEST-MD5");
//        SASLAuthentication.blacklistSASLMechanism("CRAM-MD5");
        xmppConnection = con;
        return con;
    }

注册用户

public void register(String userName,String openid) throws InterruptedException, XmppStringprepException {
		// 检测用户是否注册过
		HashMap<String, Object> map = new HashMap<>();
		map.put("openid", openid);
		List<ChatGroup> list = chatGroupService.selectByMap(map);
		if (list.size() > 0)// 如果已经注册过,不做任何操作
		{
			return;
		}
		AbstractXMPPConnection con = getXmpptcpConnection();
		try {
			// 连接服务
			con.connect();
			if (con.isConnected()) {
				{
					HashMap<String, String> attributes = new HashMap<String, String>();
					attributes.put("name", userName);
					AccountManager.sensitiveOperationOverInsecureConnectionDefault(true);
					Localpart usernameL = Localpart.from(userName);
					AccountManager.getInstance(con).createAccount(usernameL, "123456", attributes);

					
				}
			}

		} catch (SmackException | IOException | XMPPException e) {
			e.printStackTrace();
		} finally {
//			con.disconnect();// 断开连接
		}
	}

 

 

//单聊发送

	/**
	 * 单聊发消息
	 * 
	 * @param receive
	 * @param body
	 * @throws Exception
	 */
	@RequestMapping("/sendMsg")
	public void sendChatMessage(String name, String receive, String body) throws Exception {

		AbstractXMPPConnection connection = ConnectionMap.get(name);
		if (connection == null) {
			if (name.equals("admin")) {
				connection = getXmpptcpConnection("admin", "admin");
			} else {
				connection = getXmpptcpConnection(name, "123456");
			}
			// 拼凑jid
			connection.connect();
			if (!connection.isAuthenticated()) {
				connection.login();
				ConnectionMap.put(name, connection);
			}
		}
		mChatManager = ChatManager.getInstanceFor(connection);
		EntityBareJid jid = JidCreate.entityBareFrom(receive + "@" + openfireServer);
		Chat chat = mChatManager.chatWith(jid);
		Message message = new Message();
		message.setType(Message.Type.chat);
		// 发送消息格式需要处理
		message.setBody(body);
		chat.send(message);
	}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值