Commit 84190ade by giaogiao

redis管理rtc频道

parent 81e302e0
......@@ -22,6 +22,52 @@ public class RedisUtils {
@Autowired
private StringRedisTemplate redisTemplate;
public StringRedisTemplate redisTemplate() {
return redisTemplate;
}
/**
* 添加Key:value
*
* @param key
* @param value
*/
public void setKey(String key, String value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* 删除Key
*
* @param key
*/
public boolean delKey(String key) {
return redisTemplate.delete(key);
}
/**
* 获取Key
*
* @param key
*/
public String getKey(String key) {
return redisTemplate.opsForValue().get(key);
}
/**
* 模糊查询
*
* @param key
* @return
*/
public Set<String> keys(String key) {
return redisTemplate.keys(key);
}
/**
* 获取hash中field对应的值
*
......
package com.wecloud.rtc;
import java.io.Serializable;
public class RtcRedisKey implements Serializable {
/**
* 维护所有用户当前在线的频道ID
* new Map<String,Long> map
* map.put("clientA",10001)
* map.put("clientB",10001)
* map.put("clientC",10002)
* map.put("clientD",10003)
* <p>
* redis Key:
* user_join_channel = ujc
* rcu:clientA:10001
* rcu:clientB:10001
* rcu:clientC:10002
* rcu:clientD:10003
*/
public static final String USER_JOIN_CHANNEL = "ujc:%s:%s";
/**
* 维护频道中存在的用户
* Map<Long,List<String>> map
* <p>
* new List<String> list
* list.add("clientA")
* list.add("clientB")
* <p>
* map.put(10001,list)
* <p>
* redis Key:
* rtc_channel_users = rcu
* key = rcu:10001:clientA
* key = rcu:10001:clientB
* key = rcu:10002:clientC
* key = rcu:10003:clientD
*/
public static final String RTC_CHANNEL_USERS = "rcu:%s:%s";
}
package com.wecloud.rtc.service;
import java.util.List;
/**
* 管理rtc频道
*/
public interface MangerRtcChannelService {
/**
* 创建一个频道
*/
Long create(String appKey, String clientId, Long rtcChannelId);
/**
* 加入频道
*/
void join(String appKey, String clientId, Long rtcChannelId);
/**
* 退出频道
*/
void remove(String appKey, String clientId, Long rtcChannelId);
/**
* 根据频道ID获取频道内所有client
*/
List<String> getClientListByRtcChannelId(Long rtcChannelId);
/**
* 根据客户端ID获取该客户端加入的频道ID
*/
Long getRtcChannelIdListByClientId(String appKey, String clientId);
/**
* 获取客户端忙线/空闲状态
* @param appKey
* @param clientId
* @return true:忙线,false空闲
*/
boolean getBusyStatus(String appKey, String clientId);
}
package com.wecloud.rtc.service.impl;
import com.wecloud.im.ws.utils.RedisUtils;
import com.wecloud.rtc.RtcRedisKey;
import com.wecloud.rtc.service.MangerRtcChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@Service
public class MangerRtcChannelServiceImpl implements MangerRtcChannelService {
@Autowired
private RedisUtils redisUtils;
@Override
public Long create(String appKey, String clientId, Long rtcChannelId) {
//用户当前在线的频道ID
String userJoinChannelKey = String.format(RtcRedisKey.USER_JOIN_CHANNEL, appKey + clientId, rtcChannelId);
//频道中存在的用户
String rtcChannelUsersKey = String.format(RtcRedisKey.RTC_CHANNEL_USERS, rtcChannelId, appKey + clientId);
redisUtils.setKey(userJoinChannelKey, "");
redisUtils.setKey(rtcChannelUsersKey, "");
return null;
}
@Override
public void join(String appKey, String clientId, Long rtcChannelId) {
//用户当前在线的频道ID
String userJoinChannelKey = String.format(RtcRedisKey.USER_JOIN_CHANNEL, appKey + clientId, rtcChannelId);
//频道中存在的用户
String rtcChannelUsersKey = String.format(RtcRedisKey.RTC_CHANNEL_USERS, rtcChannelId, appKey + clientId);
redisUtils.setKey(userJoinChannelKey, "");
redisUtils.setKey(rtcChannelUsersKey, "");
}
@Override
public void remove(String appKey, String clientId, Long rtcChannelId) {
//用户当前在线的频道ID
String userJoinChannelKey = String.format(RtcRedisKey.USER_JOIN_CHANNEL, appKey + clientId, rtcChannelId);
//频道中存在的用户
String rtcChannelUsersKey = String.format(RtcRedisKey.RTC_CHANNEL_USERS, rtcChannelId, appKey + clientId);
redisUtils.delKey(userJoinChannelKey);
redisUtils.delKey(rtcChannelUsersKey);
}
@Override
public List<String> getClientListByRtcChannelId(Long rtcChannelId) {
String rtcChannelUsersKey = String.format(RtcRedisKey.RTC_CHANNEL_USERS, rtcChannelId, "*");
Set<String> keys = redisUtils.keys(rtcChannelUsersKey);
List<String> clientList = new ArrayList<>();
for (String next:keys){
String s = next.split(":")[2];
clientList.add(s);
}
return clientList;
}
@Override
public Long getRtcChannelIdListByClientId(String appKey, String clientId) {
String userJoinChannelKey = String.format(RtcRedisKey.USER_JOIN_CHANNEL, appKey + clientId, "*");
Set<String> keys = redisUtils.keys(userJoinChannelKey);
String next = keys.iterator().next();
return Long.valueOf(next.split(":")[2]);
}
@Override
public boolean getBusyStatus(String appKey, String clientId) {
//用户当前在线的频道ID
String userJoinChannelKey = String.format(RtcRedisKey.USER_JOIN_CHANNEL, appKey + clientId, "*");
Set<String> keys = redisUtils.keys(userJoinChannelKey);
if (keys == null || keys.isEmpty()) {
return false;
} else {
return true;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment