环信实时通信文档

为了项目封装实时通信我们用上了环信,以下是实现步聚,第一步导包,第二个配置,第三步,注入到springboot  第四实模块
HuanXinTemplate


以下是操作步聚

第一步导包

<!-- 环信官方 REST-SDK,版本以官网最新为准 -->
<dependency>
<groupId>com.easemob.im</groupId>
<artifactId>im-sdk-core</artifactId>
<version>0.8.56</version>
</dependency>

第二步配置文件

application.yml(或 application-dev.yml)
yaml
复制
huanxin:
  appkey: 1110201018107234(14#demo          # 组织名#应用名
  client-id: YXA6o9n7Llt1scarIdie70Dvgg (28  # 环信 → 应用详情 → Client ID
  client-secret: YXA6cH-XH3snn0GTJ80F6xrvXxfXCQ (57   # 同上 → Client Secret

配置属性类

配置属性类(自动读取 yml)

package com.tanhua.autoconfig.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

@Data
@ConfigurationProperties(prefix = "huanxin")
public class HuanXinProperties {
private String appkey;
private String clientId;
private String clientSecret;
}

自动装配类,注入到框架

package com.tanhua.autoconfig;

import com.tanhua.autoconfig.properties.HuanXinProperties;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(HuanXinProperties.class)
public class HuanXinAutoConfig {

@Bean
public HuanXinTemplate huanXinTemplate(HuanXinProperties properties) {
return new HuanXinTemplate(properties);
}
}

 

模板封装(线程安全,单例)

package com.tanhua.autoconfig.template;

import cn.hutool.core.collection.CollUtil;
import com.easemob.im.server.EMProperties;
import com.easemob.im.server.EMService;
import com.easemob.im.server.model.EMTextMessage;
import com.tanhua.autoconfig.properties.HuanXinProperties;
import lombok.extern.slf4j.Slf4j;

import java.util.Set;

@Slf4j
public class HuanXinTemplate {

private final EMService service;

public HuanXinTemplate(HuanXinProperties prop) {
EMProperties em = EMProperties.builder()
.setAppkey(prop.getAppkey())
.setClientId(prop.getClientId())
.setClientSecret(prop.getClientSecret())
.build();
this.service = new EMService(em);
}

/* ---------- 用户 ---------- */
public boolean createUser(String username, String password) {
try { service.user().create(username, password).block(); return true; }
catch (Exception e) { log.error("创建用户失败: {}", username, e); return false; }
}

 

// 添加联系人(user1 添加 user2)
public Boolean addContact(String user1, String user2) {
    try {
        service.contact()
               .add(user1, user2)
               .block(Duration.ofSeconds(5)); // 最多等 5 秒
        return true;
    } catch (Exception e) {
        log.error("添加联系人失败:{} -> {}", user1, user2, e);
        return false;
    }
}

// 删除联系人(user1 删除 user2)
public Boolean deleteContact(String user1, String user2) {
    try {
        service.contact()
               .remove(user1, user2)
               .block(Duration.ofSeconds(5));
        return true;
    } catch (Exception e) {
        log.error("删除联系人失败:{} -> {}", user1, user2, e);
        return false;
    }
}
// 发送单条文本消息(管理员“admin”发)
public Boolean sendMsg(String username, String content) {
    try {
        // 接收人用户列表
        Set<String> set = CollUtil.newHashSet(username);

        // 组装文本消息
        EMTextMessage message = new EMTextMessage().text(content);

        // 发送:from 固定为管理员账号 "admin"
        service.message()
               .send("admin",   // from
                     "users",   // toType
                     set,       // 接收人集合
                     message,   // 消息体
                     null)      // 扩展字段
               .block();
        return true;
    } catch (Exception e) {
        log.error("发送消息失败, toUser={}, content={}", username, content, e);
        return false;
    }
}

}

 

posted @ 2025-08-09 11:13  谢双元小号  阅读(20)  评论(0)    收藏  举报