mirror of
https://gitee.com/farsunset/cim.git
synced 2025-06-16 23:29:57 +08:00
android sdk更新
适配targetSdkVersion 33
This commit is contained in:
parent
046bfd8ade
commit
68b3ebd5c4
@ -28,15 +28,17 @@ import com.farsunset.cim.group.SessionGroup;
|
||||
import com.farsunset.cim.model.Message;
|
||||
import com.farsunset.cim.util.JSONUtils;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelFutureListener;
|
||||
import io.netty.channel.ChannelOutboundInvoker;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.data.redis.connection.MessageListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* 集群环境下,监控多设备登录情况,控制是否其余终端下线的逻辑
|
||||
@ -55,6 +57,11 @@ public class BindMessageListener implements MessageListener {
|
||||
*/
|
||||
private final Map<String,String[]> conflictMap = new HashMap<>();
|
||||
|
||||
/*
|
||||
* web可能同一个终端 打开多 tab页面,可以同时保持连接
|
||||
*/
|
||||
private final Set<String> keepLiveChannels = new HashSet<>();
|
||||
|
||||
@Resource
|
||||
private SessionGroup sessionGroup;
|
||||
|
||||
@ -64,6 +71,8 @@ public class BindMessageListener implements MessageListener {
|
||||
conflictMap.put(Session.CHANNEL_WINDOWS,new String[]{Session.CHANNEL_WINDOWS,Session.CHANNEL_WEB,Session.CHANNEL_MAC});
|
||||
conflictMap.put(Session.CHANNEL_WEB,new String[]{Session.CHANNEL_WINDOWS,Session.CHANNEL_WEB,Session.CHANNEL_MAC});
|
||||
conflictMap.put(Session.CHANNEL_MAC,new String[]{Session.CHANNEL_WINDOWS,Session.CHANNEL_WEB,Session.CHANNEL_MAC});
|
||||
|
||||
keepLiveChannels.add(Session.CHANNEL_WEB);
|
||||
}
|
||||
|
||||
@EventListener
|
||||
@ -79,34 +88,98 @@ public class BindMessageListener implements MessageListener {
|
||||
this.handle(session);
|
||||
}
|
||||
|
||||
private void handle(Session session) {
|
||||
private void handle(Session session){
|
||||
|
||||
String uid = session.getUid();
|
||||
|
||||
String[] conflictChannels = conflictMap.get(session.getChannel());
|
||||
|
||||
if (ArrayUtils.isEmpty(conflictChannels)){
|
||||
return;
|
||||
}
|
||||
|
||||
Collection<Channel> channelList = sessionGroup.find(uid,conflictChannels);
|
||||
|
||||
channelList.removeIf(channel -> session.getNid().equals(channel.attr(ChannelAttr.ID).get()));
|
||||
channelList.removeIf(new KeepLivePredicate(session));
|
||||
|
||||
/*
|
||||
* 获取到其他在线的终端连接,提示账号再其他终端登录
|
||||
* 同设备仅关闭连接,无需通知客户端
|
||||
*/
|
||||
channelList.forEach(channel -> {
|
||||
channelList.stream().filter(new SameDevicePredicate(session)).forEach(ChannelOutboundInvoker::close);
|
||||
|
||||
if (Objects.equals(session.getDeviceId(),channel.attr(ChannelAttr.DEVICE_ID).get())){
|
||||
channel.close();
|
||||
return;
|
||||
}
|
||||
/*
|
||||
* 不同设备关闭连接, 通知客户端账号在其他设备登录
|
||||
*/
|
||||
channelList.stream().filter(new DifferentDevicePredicate(session)).forEach(new BreakOffMessageConsumer(uid,session.getDeviceName()));
|
||||
|
||||
Message message = new Message();
|
||||
}
|
||||
|
||||
|
||||
private static class BreakOffMessageConsumer implements Consumer<Channel> {
|
||||
|
||||
private final Message message;
|
||||
|
||||
private BreakOffMessageConsumer(String uid,String deviceName) {
|
||||
message = new Message();
|
||||
message.setAction(FORCE_OFFLINE_ACTION);
|
||||
message.setReceiver(uid);
|
||||
message.setSender(SYSTEM_ID);
|
||||
message.setContent(session.getDeviceName());
|
||||
channel.writeAndFlush(message);
|
||||
channel.close();
|
||||
});
|
||||
message.setContent(deviceName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(Channel channel) {
|
||||
channel.writeAndFlush(message).addListener(ChannelFutureListener.CLOSE);
|
||||
}
|
||||
}
|
||||
private static class SameDevicePredicate implements Predicate<Channel> {
|
||||
|
||||
private final String deviceId;
|
||||
|
||||
private SameDevicePredicate(Session session) {
|
||||
this.deviceId = session.getDeviceId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(Channel channel) {
|
||||
return Objects.equals(this.deviceId,channel.attr(ChannelAttr.DEVICE_ID).get());
|
||||
}
|
||||
}
|
||||
|
||||
private static class DifferentDevicePredicate implements Predicate<Channel>{
|
||||
|
||||
private final SameDevicePredicate predicate;
|
||||
|
||||
private DifferentDevicePredicate(Session session) {
|
||||
this.predicate = new SameDevicePredicate(session);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(Channel channel) {
|
||||
return !predicate.test(channel);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private class KeepLivePredicate implements Predicate<Channel>{
|
||||
private final Session session;
|
||||
|
||||
private KeepLivePredicate(Session session) {
|
||||
this.session = session;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(Channel ioChannel) {
|
||||
|
||||
if (Objects.equals(session.getNid(),ioChannel.attr(ChannelAttr.ID).get())){
|
||||
return true;
|
||||
}
|
||||
|
||||
String deviceId = ioChannel.attr(ChannelAttr.DEVICE_ID).toString();
|
||||
|
||||
String channel = ioChannel.attr(ChannelAttr.CHANNEL).toString();
|
||||
|
||||
return keepLiveChannels.contains(channel) && Objects.equals(session.getDeviceId(),deviceId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Binary file not shown.
@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CompilerConfiguration">
|
||||
<bytecodeTargetLevel target="1.8" />
|
||||
</component>
|
||||
</project>
|
20
cim-use-examples/cim-client-android/.idea/gradle.xml
generated
20
cim-use-examples/cim-client-android/.idea/gradle.xml
generated
@ -1,20 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="GradleMigrationSettings" migrationVersion="1" />
|
||||
<component name="GradleSettings">
|
||||
<option name="linkedExternalProjectsSettings">
|
||||
<GradleProjectSettings>
|
||||
<option name="testRunner" value="GRADLE" />
|
||||
<option name="distributionType" value="DEFAULT_WRAPPED" />
|
||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||
<option name="gradleJvm" value="1.8 (2)" />
|
||||
<option name="modules">
|
||||
<set>
|
||||
<option value="$PROJECT_DIR$" />
|
||||
<option value="$PROJECT_DIR$/app" />
|
||||
</set>
|
||||
</option>
|
||||
</GradleProjectSettings>
|
||||
</option>
|
||||
</component>
|
||||
</project>
|
@ -1,25 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="RemoteRepositoriesConfiguration">
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Maven Central repository" />
|
||||
<option name="url" value="https://repo1.maven.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="jboss.community" />
|
||||
<option name="name" value="JBoss Community repository" />
|
||||
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="BintrayJCenter" />
|
||||
<option name="name" value="BintrayJCenter" />
|
||||
<option name="url" value="https://jcenter.bintray.com/" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="Google" />
|
||||
<option name="name" value="Google" />
|
||||
<option name="url" value="https://dl.google.com/dl/android/maven2/" />
|
||||
</remote-repository>
|
||||
</component>
|
||||
</project>
|
@ -1,9 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||
</component>
|
||||
<component name="ProjectType">
|
||||
<option name="id" value="Android" />
|
||||
</component>
|
||||
</project>
|
@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/../.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
Loading…
x
Reference in New Issue
Block a user