28 changed files with 398 additions and 67 deletions
@ -1,20 +0,0 @@
|
||||
package net.sopod.soim.client.handler; |
||||
|
||||
import com.google.inject.Singleton; |
||||
import net.sopod.soim.client.cmd.handler.NonArgsHandler; |
||||
|
||||
/** |
||||
* ExitHandler |
||||
* |
||||
* @author tmy |
||||
* @date 2022-04-25 11:53 |
||||
*/ |
||||
@Singleton |
||||
public class ExitHandler extends NonArgsHandler { |
||||
|
||||
@Override |
||||
public void handle() { |
||||
System.exit(1); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,30 @@
|
||||
package net.sopod.soim.client.handler.cmd; |
||||
|
||||
import com.google.inject.Inject; |
||||
import com.google.inject.Singleton; |
||||
import net.sopod.soim.client.cmd.CmdStarter; |
||||
import net.sopod.soim.client.cmd.handler.NonArgsHandler; |
||||
import net.sopod.soim.client.session.SoImSession; |
||||
|
||||
/** |
||||
* ExitHandler |
||||
* |
||||
* @author tmy |
||||
* @date 2022-04-25 11:53 |
||||
*/ |
||||
@Singleton |
||||
public class ExitHandler extends NonArgsHandler { |
||||
|
||||
@Inject |
||||
private SoImSession soImSession; |
||||
|
||||
@Inject |
||||
private CmdStarter cmdStarter; |
||||
|
||||
@Override |
||||
public void handle() { |
||||
cmdStarter.close(); |
||||
soImSession.close(); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,26 @@
|
||||
package net.sopod.soim.client.handler.msg; |
||||
|
||||
import com.google.inject.Inject; |
||||
import com.google.inject.Singleton; |
||||
import net.sopod.soim.client.session.MessageHandler; |
||||
import net.sopod.soim.client.session.SoImSession; |
||||
import net.sopod.soim.data.msg.auth.Auth; |
||||
|
||||
/** |
||||
* ResTokenAuthHandler |
||||
* |
||||
* @author tmy |
||||
* @date 2022-04-27 9:46 |
||||
*/ |
||||
@Singleton |
||||
public class ResTokenAuthHandler implements MessageHandler<Auth.ResTokenAuth> { |
||||
|
||||
@Inject |
||||
private SoImSession soImSession; |
||||
|
||||
@Override |
||||
public void handleMsg(Auth.ResTokenAuth msg) { |
||||
soImSession.authResult(msg.getSuccess(), msg.getMessage()); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,53 @@
|
||||
package net.sopod.soim.client.session; |
||||
|
||||
import com.google.inject.Inject; |
||||
import com.google.inject.Singleton; |
||||
import com.google.protobuf.MessageLite; |
||||
import io.netty.channel.ChannelHandler; |
||||
import io.netty.channel.ChannelHandlerContext; |
||||
import io.netty.channel.SimpleChannelInboundHandler; |
||||
import net.sopod.soim.common.util.Reflects; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
|
||||
/** |
||||
* MessageDispatcher |
||||
* |
||||
* @author tmy |
||||
* @date 2022-04-27 9:48 |
||||
*/ |
||||
@Singleton |
||||
@ChannelHandler.Sharable |
||||
public class MessageDispatcher extends SimpleChannelInboundHandler<MessageLite> { |
||||
|
||||
private Map<Class<?>, MessageHandler> msgHandlers; |
||||
|
||||
@Inject |
||||
public MessageDispatcher(Set<MessageHandler> handlers) { |
||||
this.registryHandlers(handlers); |
||||
} |
||||
|
||||
@Override |
||||
protected void channelRead0(ChannelHandlerContext channelHandlerContext, MessageLite msg) throws Exception { |
||||
MessageHandler messageHandler = msgHandlers.get(msg.getClass()); |
||||
messageHandler.handleMsg(msg); |
||||
} |
||||
|
||||
private void registryHandlers(Set<MessageHandler> handlers) { |
||||
for (MessageHandler<?> handler : handlers) { |
||||
List<String> msgTypes = Reflects.getSuperInterfaceGenericTypes(handler.getClass()); |
||||
Map<Class<?>, MessageHandler> msgHandlers = new HashMap<>(); |
||||
try { |
||||
Class<?> msgType = msgTypes.size() == 0 ? Object.class : Class.forName(msgTypes.get(0)); |
||||
msgHandlers.put(msgType, handler); |
||||
} catch (ClassNotFoundException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
this.msgHandlers = msgHandlers; |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,13 @@
|
||||
package net.sopod.soim.client.session; |
||||
|
||||
/** |
||||
* MessageHandler |
||||
* |
||||
* @author tmy |
||||
* @date 2022-04-27 9:50 |
||||
*/ |
||||
public interface MessageHandler<T> { |
||||
|
||||
void handleMsg(T msg); |
||||
|
||||
} |
||||
@ -0,0 +1,21 @@
|
||||
package net.sopod.soim.common.constant; |
||||
|
||||
/** |
||||
* AppConstant |
||||
* |
||||
* @author tmy |
||||
* @date 2022-04-27 15:04 |
||||
*/ |
||||
public interface AppConstant { |
||||
|
||||
String APP_REGISTRY_GROUP = "so-im"; |
||||
|
||||
/** im-entry 注册中心名称 */ |
||||
String APP_IM_ENTRY_REGISTRY_NAME = "im-entry-instance"; |
||||
|
||||
String APP_IM_ENTRY_NAME = "im-entry"; |
||||
|
||||
String APP_IM_HTTP_ENTRY_NAME = "im-http-entry"; |
||||
String APP_IM_DAS_USER_NAME = "im-das-user"; |
||||
|
||||
} |
||||
@ -0,0 +1,48 @@
|
||||
package net.sopod.soim.entry.registry; |
||||
|
||||
import com.alibaba.nacos.api.NacosFactory; |
||||
import com.alibaba.nacos.api.exception.NacosException; |
||||
import com.alibaba.nacos.api.naming.NamingService; |
||||
import net.sopod.soim.common.constant.AppConstant; |
||||
import net.sopod.soim.entry.config.EntryServerConfig; |
||||
import org.apache.dubbo.registry.client.ServiceDiscoveryRegistry; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
import javax.annotation.Resource; |
||||
import java.util.Properties; |
||||
|
||||
/** |
||||
* RegistryService |
||||
* |
||||
* @author tmy |
||||
* @date 2022-04-27 16:38 |
||||
*/ |
||||
@Service |
||||
public class RegistryService { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(RegistryService.class); |
||||
|
||||
@Resource |
||||
private EntryServerConfig entryServerConfig; |
||||
|
||||
//@Resource
|
||||
private ServiceDiscoveryRegistry serviceDiscoveryRegistry; |
||||
|
||||
private NamingService namingService; |
||||
|
||||
public void registryImEntry() throws NacosException { |
||||
Properties properties = new Properties(); |
||||
properties.put("serverAddr", entryServerConfig.getNacosAddr()); |
||||
this.namingService = NacosFactory.createNamingService(properties); |
||||
|
||||
// 注册当前服务
|
||||
this.namingService.registerInstance(AppConstant.APP_IM_ENTRY_REGISTRY_NAME, |
||||
entryServerConfig.getIp(), |
||||
entryServerConfig.getPort() |
||||
); |
||||
logger.info("{} registry at {}:{}", AppConstant.APP_IM_ENTRY_REGISTRY_NAME, entryServerConfig.getIp(), entryServerConfig.getPort()); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue