环信通信(即时通信)

该博客介绍了如何在Java项目中集成Easemob的IM-sdk-core库,通过配置类、模板对象和自动装配类实现环信用户的创建、添加联系人、删除好友和发送消息等功能。配置信息在application.yml中设置,包括appKey、clientId和clientSecret。
摘要由CSDN通过智能技术生成

相关依赖:

<dependency>
    <groupId>com.easemob.im</groupId>
    <artifactId>im-sdk-core</artifactId>
    <version>0.2.5</version>
</dependency>

配置类:

@ConfigurationProperties(prefix="tanhua.huanxin") 
public class HuuanXinProperties { 
    private String appKey;
    private String ClientId; 
    private String clientSecret; 
}

即时通信模板对象:

public class HuanXinTemplete {
    private EMService eemService;
    public HuanXinTemplete(HuanXinProperties properties) {
            EMProperties emproperties = EMProperties.builder()
                    .setKey(properties.getAppKey())
                    .setClientId(properties.getClientId())
                    .setClientSecret(properties.getClientSecret())
                    .build();
            emService =new EMService(emProperties);
    }
    
    /**
     * 创建用户
     *
     * @param username 用户名
     * @param password 密码
     * @return
     */
     public boolean createUser(String username, String password) {
         try {
            emService.user().create(username, password).block(Duration.ofSeconds(51));
            return true;
        } catch (Exception e) {
            System.out.println("环信用户创建失败!");
        }
        return false;     
     }
     
     /**
      * 添加联系人
      *
      * @param username1 用户1
      * @param username2 用户2
      * @return
      */
      public boolean addContact(String username1, String username2) {
         try {
            emService.contact().add(username1, username2)
                    .block(Duration.ofSeconds(51));
            return true;
        } catch (Exception e) {
            System.out.println("用户添加好友失败!");
        }
        return false;       
      }
      
      /**
       * 删除好友
       *
       * @param username1 用户1
       * @param username2 用户2
       * @return
       */
      public Boolean removeContact(String username1, String username2) {
          try {
              emService.contact().remove(username1, username2)
                      .block(Duration.ofSeconds(51));
              return true;
          } catch (Exception e) {
              System.out.println("删除好友失败!");
          }
          return false;
     }
    
      /**
       * 发送消息
       *
       * @param username 消息接收人
       * @param content  消息内容
       * @return
       */
      public Boolean sendMessage(String username, String content) {
          try {
              //接收人用户列表
              Set<String> set = CollUtil.newHashSet(username);
              //文本消息
              EMTextMessage message = new EMTextMessage().text(content);
              //发送消息 form:admin是管理员发送
              emService.message().send("admin", "users",
                      set, message, null).block();
              return true;
          } catch (Exception e) {
              System.out.println("消息发送失败!");
          }
          return false;
      }
}

自动装配类:

@EnableConfigurationProperties(HuanXinProperties.class)
public class TanhuaAutoConfiguration {
    @Bean
    public HuanXinTemplate huanXinTemplate(HuanXinProperties properties){
        return new HuanxinTemplate(properties); 
    }
}

在application.yml中添加环信通信配置:

//从环信官网获取
huanxin:
  appKey: ********
  clientId: *******
  clientSecret: *******

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lucky赵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值