Commit 347ad0b6 by giaogiao

优化webrtc缓存 : 删除ChannelInfo; 获取RtcChannelInfo

parent 104ec405
...@@ -44,7 +44,7 @@ public class NettyChannelInitializer extends ChannelInitializer<SocketChannel> { ...@@ -44,7 +44,7 @@ public class NettyChannelInitializer extends ChannelInitializer<SocketChannel> {
* allIdleTime—状态为IdleState的IdleStateEvent。在一定时间内不读不写会触发ALL_IDLE。指定0禁用。 * allIdleTime—状态为IdleState的IdleStateEvent。在一定时间内不读不写会触发ALL_IDLE。指定0禁用。
* unit—readerIdleTime、writeIdleTime和allIdleTime的时间单位 * unit—readerIdleTime、writeIdleTime和allIdleTime的时间单位
*/ */
pipeline.addLast(new IdleStateHandler(15, 0, 0, TimeUnit.SECONDS)); pipeline.addLast(new IdleStateHandler(5, 0, 0, TimeUnit.SECONDS));
} }
} }
...@@ -310,22 +310,22 @@ public class MangerChannelServiceImpl implements MangerChannelService { ...@@ -310,22 +310,22 @@ public class MangerChannelServiceImpl implements MangerChannelService {
NioSocketChannel nioSocketChannel = get(toAppKey, toClientId); NioSocketChannel nioSocketChannel = get(toAppKey, toClientId);
if (null == nioSocketChannel) { if (null == nioSocketChannel) {
// userCache.offline(toAppKey + toClientId); // userCache.offline(toAppKey + toClientId);
if (log.isDebugEnabled()) { // if (log.isDebugEnabled()) {
log.info("writeData连接为空:" + toAppKey + toClientId + "," + msg); log.info("writeData连接为空:" + toAppKey + toClientId + "," + msg);
} // }
return false; return false;
} }
// 判断连接是否断开 // 判断连接是否断开
if (nioSocketChannel.isShutdown()) { if (nioSocketChannel.isShutdown()) {
if (log.isDebugEnabled()) { // if (log.isDebugEnabled()) {
log.info("writeData连接断开:" + toAppKey + toClientId + "," + msg + ",\nchannelId:" + nioSocketChannel.id().asLongText()); log.info("writeData连接断开:" + toAppKey + toClientId + "," + msg + ",\nchannelId:" + nioSocketChannel.id().asLongText());
} // }
return false; return false;
} }
if (log.isDebugEnabled()) { // if (log.isDebugEnabled()) {
log.info("writeData:" + toAppKey + "," + toClientId + "," + msg + ",\nchannelId:" + nioSocketChannel.id().asLongText()); log.info("writeData:" + toAppKey + "," + toClientId + "," + msg + ",\nchannelId:" + nioSocketChannel.id().asLongText());
} // }
ChannelFuture channelFuture = nioSocketChannel.writeAndFlush(new TextWebSocketFrame(msg)); ChannelFuture channelFuture = nioSocketChannel.writeAndFlush(new TextWebSocketFrame(msg));
channelFuture.addListener( channelFuture.addListener(
......
...@@ -14,7 +14,7 @@ import com.wecloud.im.ws.model.request.ReceiveModel; ...@@ -14,7 +14,7 @@ import com.wecloud.im.ws.model.request.ReceiveModel;
import com.wecloud.im.ws.sender.SystemPush; import com.wecloud.im.ws.sender.SystemPush;
import com.wecloud.im.ws.service.WriteDataService; import com.wecloud.im.ws.service.WriteDataService;
import com.wecloud.im.ws.strategy.ImCmdAbstract; import com.wecloud.im.ws.strategy.ImCmdAbstract;
import com.wecloud.rtc.RtcSubCmd; import com.wecloud.rtc.entity.RtcSubCmd;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
......
package com.wecloud.rtc; package com.wecloud.rtc.entity;
import java.io.Serializable; import java.io.Serializable;
......
...@@ -14,6 +14,9 @@ public class RtcChannelInfo implements Serializable { ...@@ -14,6 +14,9 @@ public class RtcChannelInfo implements Serializable {
@ApiModelProperty("当前房主") @ApiModelProperty("当前房主")
private String owner; private String owner;
private String appKey;
// private String appId;
@ApiModelProperty("创建时间") @ApiModelProperty("创建时间")
private Long createTimestamp; private Long createTimestamp;
} }
package com.wecloud.rtc.service; package com.wecloud.rtc.service;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.wecloud.rtc.entity.redis.RtcChannelInfo;
import java.util.List; import java.util.List;
...@@ -9,7 +10,32 @@ import java.util.List; ...@@ -9,7 +10,32 @@ import java.util.List;
*/ */
public interface MangerRtcCacheService { public interface MangerRtcCacheService {
boolean isEmpty(String appKey, String clientId, Long rtcChannelId);
/**
* 移除频道信息
*
* @param rtcChannelId
* @return
*/
boolean delChannelInfo(Long rtcChannelId);
/**
* 频道中客户端是否为空
*
* @param rtcChannelId
* @return
*/
boolean channelIsEmpty(Long rtcChannelId);
/**
* 获取频道信息
*
* @param rtcChannelId
* @return
* @throws JsonProcessingException
*/
RtcChannelInfo getRtcChannelInfo(Long rtcChannelId) throws JsonProcessingException;
/** /**
* 创建一个频道 * 创建一个频道
......
...@@ -23,21 +23,46 @@ public class MangerRtcCacheServiceImpl implements MangerRtcCacheService { ...@@ -23,21 +23,46 @@ public class MangerRtcCacheServiceImpl implements MangerRtcCacheService {
@Override @Override
public boolean isEmpty(String appKey, String clientId, Long rtcChannelId) { public boolean delChannelInfo(Long rtcChannelId) {
String channelKey = String.format(RtcRedisKey.RTC_CHANNEL_INFO, rtcChannelId);
redisUtils.delKey(channelKey);
return true;
}
@Override
public boolean channelIsEmpty(Long rtcChannelId) {
List<String> clientListByRtcChannelId = getClientListByRtcChannelId(rtcChannelId); List<String> clientListByRtcChannelId = getClientListByRtcChannelId(rtcChannelId);
// 移除自己 // // 移除自己
clientListByRtcChannelId.remove(appKey + clientId); // clientListByRtcChannelId.remove(appKey + clientId);
return clientListByRtcChannelId.isEmpty(); return clientListByRtcChannelId.isEmpty();
} }
@Override @Override
public RtcChannelInfo getRtcChannelInfo(Long rtcChannelId) throws JsonProcessingException {
// 频道信息
String channelKey = String.format(RtcRedisKey.RTC_CHANNEL_INFO, rtcChannelId);
String value = redisUtils.getKey(channelKey);
if (StringUtils.isBlank(value)) {
return null;
}
JsonMapper jsonMapper = new JsonMapper();
return jsonMapper.readValue(value, RtcChannelInfo.class);
}
@Override
public void create(String appKey, String clientId, Long rtcChannelId) throws JsonProcessingException { public void create(String appKey, String clientId, Long rtcChannelId) throws JsonProcessingException {
// --- 频道信息 // --- 频道信息
RtcChannelInfo rtcChannelInfo = new RtcChannelInfo(); RtcChannelInfo rtcChannelInfo = new RtcChannelInfo();
rtcChannelInfo.setAppKey(appKey);
// rtcChannelInfo.setAppId("");
//当前房主 //当前房主
rtcChannelInfo.setOwner(appKey + clientId); rtcChannelInfo.setOwner(clientId);
//创建时间 //创建时间
rtcChannelInfo.setCreateTimestamp(new Date().getTime()); rtcChannelInfo.setCreateTimestamp(new Date().getTime());
......
...@@ -124,7 +124,7 @@ public class RtcServiceImpl implements RtcService { ...@@ -124,7 +124,7 @@ public class RtcServiceImpl implements RtcService {
//获取频道内所有client //获取频道内所有client
List<String> clientListByRtcChannelId = mangerRtcCacheService.getClientListByRtcChannelId(joinRtcChannelParam.getChannelId()); List<String> clientListByRtcChannelId = mangerRtcCacheService.getClientListByRtcChannelId(joinRtcChannelParam.getChannelId());
// 移除自己 // 移除自己
clientListByRtcChannelId.remove(imApplication.getAppKey() + client.getClientId()); clientListByRtcChannelId.remove(client.getClientId());
for (String toClientId : clientListByRtcChannelId) { for (String toClientId : clientListByRtcChannelId) {
...@@ -149,7 +149,7 @@ public class RtcServiceImpl implements RtcService { ...@@ -149,7 +149,7 @@ public class RtcServiceImpl implements RtcService {
//获取频道内所有client //获取频道内所有client
List<String> clientListByRtcChannelId = mangerRtcCacheService.getClientListByRtcChannelId(rejectRtcChannelParam.getChannelId()); List<String> clientListByRtcChannelId = mangerRtcCacheService.getClientListByRtcChannelId(rejectRtcChannelParam.getChannelId());
// 移除自己 // 移除自己
clientListByRtcChannelId.remove(imApplication.getAppKey() + client.getClientId()); clientListByRtcChannelId.remove(client.getClientId());
for (String toClientId : clientListByRtcChannelId) { for (String toClientId : clientListByRtcChannelId) {
...@@ -184,7 +184,7 @@ public class RtcServiceImpl implements RtcService { ...@@ -184,7 +184,7 @@ public class RtcServiceImpl implements RtcService {
//获取频道内所有client //获取频道内所有client
List<String> clientListByRtcChannelId = mangerRtcCacheService.getClientListByRtcChannelId(leaveRtcChannelParam.getChannelId()); List<String> clientListByRtcChannelId = mangerRtcCacheService.getClientListByRtcChannelId(leaveRtcChannelParam.getChannelId());
// 移除自己 // 移除自己
clientListByRtcChannelId.remove(imApplication.getAppKey() + client.getClientId()); clientListByRtcChannelId.remove(client.getClientId());
for (String toClientId : clientListByRtcChannelId) { for (String toClientId : clientListByRtcChannelId) {
...@@ -198,8 +198,10 @@ public class RtcServiceImpl implements RtcService { ...@@ -198,8 +198,10 @@ public class RtcServiceImpl implements RtcService {
} }
// 判断频道内是否无其他人了 // 判断频道内是否无其他人了
// if (clientListByRtcChannelId == null) { if (mangerRtcCacheService.channelIsEmpty(leaveRtcChannelParam.getChannelId())) {
// } // 移除频道信息
mangerRtcCacheService.delChannelInfo(leaveRtcChannelParam.getChannelId());
}
} }
@Override @Override
...@@ -224,7 +226,7 @@ public class RtcServiceImpl implements RtcService { ...@@ -224,7 +226,7 @@ public class RtcServiceImpl implements RtcService {
//获取频道内所有client //获取频道内所有client
List<String> clientListByRtcChannelId = mangerRtcCacheService.getClientListByRtcChannelId(sdpForwardParam.getChannelId()); List<String> clientListByRtcChannelId = mangerRtcCacheService.getClientListByRtcChannelId(sdpForwardParam.getChannelId());
// 移除自己 // 移除自己
clientListByRtcChannelId.remove(imApplication.getAppKey() + client.getClientId()); clientListByRtcChannelId.remove(client.getClientId());
for (String toClientId : clientListByRtcChannelId) { for (String toClientId : clientListByRtcChannelId) {
...@@ -265,7 +267,7 @@ public class RtcServiceImpl implements RtcService { ...@@ -265,7 +267,7 @@ public class RtcServiceImpl implements RtcService {
//获取频道内所有client //获取频道内所有client
List<String> clientListByRtcChannelId = mangerRtcCacheService.getClientListByRtcChannelId(candidateForwardParam.getChannelId()); List<String> clientListByRtcChannelId = mangerRtcCacheService.getClientListByRtcChannelId(candidateForwardParam.getChannelId());
// 移除自己 // 移除自己
clientListByRtcChannelId.remove(imApplication.getAppKey() + client.getClientId()); clientListByRtcChannelId.remove(client.getClientId());
for (String toClientId : clientListByRtcChannelId) { for (String toClientId : clientListByRtcChannelId) {
......
...@@ -6,11 +6,9 @@ String appSecret = "a5e619003868258e0f7c5b5821ea00fb6b2302faf2ab3737"; ...@@ -6,11 +6,9 @@ String appSecret = "a5e619003868258e0f7c5b5821ea00fb6b2302faf2ab3737";
-- --
clientA1 clientA1
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJ3ZWIiLCJjbGllbnRJZCI6ImNsaWVudEExIiwiaXNzIjoid2VjbG91ZF9pbSIsImFwcEtleSI6IlFOdFAzRWp0THcyNmVrdDAiLCJleHAiOjE2ODQwMDA2NjIsImlhdCI6MTYzNDAyMTY1NCwianRpIjoiYzBiMDExOWNmYzE5NDk1YjgzYWU5YjQ3ZmFlZmM5ZTMifQ.2d_oQT-KwYmSOVZ7zXiuBTB8zRA4H8UgP2m_cMerGHE
-- --
aaaaa2 aaaaa2
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJ3ZWIiLCJjbGllbnRJZCI6ImFhYWFhMiIsImlzcyI6IndlY2xvdWRfaW0iLCJhcHBLZXkiOiJRTnRQM0VqdEx3MjZla3QwIiwiZXhwIjoxNjg0MDAwNzAwLCJpYXQiOjE2MzQwMjE2OTIsImp0aSI6IjFhY2RhNWEzNGI3NjQwZTA4MDBlMWNiMTRhNTBmMWI5In0.m18ZspfoKDx_RjrBJ07o5CP1nSaLAMEwKmSUvh94ilc
-- --
...@@ -256,4 +254,20 @@ ws ...@@ -256,4 +254,20 @@ ws
若房间内只剩一个人 ,下发只剩一个人的通知 并要求仅剩的客户端上传offer的SDP 与candidate, 使原发起方成为"房主"来等待接收方来应答"answer" 若房间内只剩一个人 ,下发只剩一个人的通知 并要求仅剩的客户端上传offer的SDP 与candidate, 使原发起方成为"房主"来等待接收方来应答"answer"
原发起方来重连时, 调用join接口,则返回另一端的offer的SDP,同时上传自己的answer SDP 原发起方来重连时, 调用join接口,则返回另一端的offer的SDP,同时上传自己的answer SDP
\ No newline at end of file
##webrtc测试时https的问题
https://blog.csdn.net/sxc1989/article/details/81291320
在chorme中需要在https的情况下才能使用webrtc,在没有正式证书情况下, 可采用以下几种方式。
mac
用命令行启动chrome
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --unsafely-treat-insecure-origin-as-secure="http://192.168.1.250:5500" --user-data-dir=/Users/giaogiao/Downloads
win
右击chrome游览器图标,选择“属性”,在目标后面加入如下内容:
--unsafely-treat-insecure-origin-as-secure="http://ip:port" --user-data-dir="C:/temp"
1
其中ip、port为服务端地址,–user-data-dir提供一个本地路径即可。
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