|
@@ -0,0 +1,84 @@
|
|
|
+package com.ym.mec.web.support.socket;
|
|
|
+
|
|
|
+import com.corundumstudio.socketio.SocketIONamespace;
|
|
|
+import com.corundumstudio.socketio.annotation.OnConnect;
|
|
|
+import com.corundumstudio.socketio.annotation.OnDisconnect;
|
|
|
+import com.corundumstudio.socketio.annotation.OnEvent;
|
|
|
+import com.ym.mec.web.support.anno.NamespaceReference;
|
|
|
+import com.ym.mec.web.support.anno.OnNamespace;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.BeansException;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.config.BeanPostProcessor;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.ReflectionUtils;
|
|
|
+
|
|
|
+import java.lang.annotation.Annotation;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class SocketEventScanner implements BeanPostProcessor {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private NamespaceFactoryBean factoryBean;
|
|
|
+ private static final Logger LOGGER = LoggerFactory.getLogger(SocketEventScanner.class);
|
|
|
+
|
|
|
+ private final List<Class<? extends Annotation>> annotations =
|
|
|
+ Arrays.asList(OnConnect.class, OnDisconnect.class, OnEvent.class);
|
|
|
+
|
|
|
+ private Class originalBeanClass;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
|
|
+ OnNamespace onNamespace = bean.getClass().getAnnotation(OnNamespace.class);
|
|
|
+ if (originalBeanClass != null && onNamespace != null) {
|
|
|
+ //前端调用方式 var socket = io.connect('http://localhost:9092/chat1');
|
|
|
+ factoryBean.setOriginalBeanClass(originalBeanClass);
|
|
|
+ SocketIONamespace socketIONamespace = factoryBean.getObject();
|
|
|
+ socketIONamespace.addListeners(bean, originalBeanClass);
|
|
|
+ LOGGER.info("{} bean 手动 listeners added", beanName);
|
|
|
+ //注入属性
|
|
|
+ Field[] declaredFields = originalBeanClass.getDeclaredFields();
|
|
|
+ for (Field declaredField : declaredFields) {
|
|
|
+ if (declaredField.isAnnotationPresent(NamespaceReference.class)) {
|
|
|
+ try {
|
|
|
+ declaredField.setAccessible(true);
|
|
|
+ declaredField.set(bean, socketIONamespace);
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ originalBeanClass = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ return bean;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
|
|
+
|
|
|
+ final AtomicBoolean add = new AtomicBoolean();
|
|
|
+ ReflectionUtils.doWithMethods(bean.getClass(),
|
|
|
+ method -> add.set(true),
|
|
|
+ method -> {
|
|
|
+ //匹配符合的方法
|
|
|
+ for (Class<? extends Annotation> annotationClass : annotations) {
|
|
|
+ if (method.isAnnotationPresent(annotationClass)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ });
|
|
|
+ if (add.get()) {
|
|
|
+ originalBeanClass = bean.getClass();
|
|
|
+ }
|
|
|
+ return bean;
|
|
|
+ }
|
|
|
+}
|