SpringBoot 集成Netty-SocketIO

 

om.xml

		
			com.corundumstudio.socketio
			netty-socketio
			1.7.7
		

application.properties

socketio.host=localhost
socketio.port=5380

 NettySocketioConfig.java


import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.SpringAnnotationScanner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class NettySocketioConfig {
    @Value("${socketio.host}")
    private String host;
    @Value("${socketio.port}")
    private Integer port;

    //netty-socketio服务器
    @Bean
    public SocketIOServer socketIOServer() {
        com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();
        config.setHostname(host);
        config.setPort(port);

        SocketIOServer server = new SocketIOServer(config);
        return server;
    }

    //用于扫描netty-socketio的注解,比如 @OnConnect、@OnEvent
    @Bean
    public SpringAnnotationScanner springAnnotationScanner() {
        return new SpringAnnotationScanner(socketIOServer());
    }
}

LoginHandler.java

import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnDisconnect;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class LoginHandler {
    private static Map clientMap = new ConcurrentHashMap<>();
//客户端连上socket服务器时执行此事件
    @OnConnect
    public void onConnect(SocketIOClient client) {
        String userId = client.getHandshakeData().getSingleUrlParam("userId");
        if (userId != null) {
            clientMap.put(userId, client);
        }
    }
//客户端断开socket服务器时执行此事件

    @OnDisconnect
    public void onDisconnect(SocketIOClient client) {
        String userId = client.getHandshakeData().getSingleUrlParam("userId");
        if (userId != null) {
            clientMap.remove(userId);
            client.disconnect();
        }
    }
//服务器向客户端发送事件
    public void pushMessage(String userId) {
        if (!StringUtils.isEmpty(userId)) {
            SocketIOClient client = clientMap.get(userId);
            if (client != null) {
                client.sendEvent("pushMsg", "hello-Rorschache");
            }
        }
    }
}

DemoApplication.java

@SpringBootApplication
public class DemoApplication  implements CommandLineRunner {
	@Autowired
	private SocketIOServer socketIOServer;
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

	@Override
	public void run(String... args) throws Exception {
		socketIOServer.start();//启动
	}
}

SocketController.java

import com.zsw.test.demo.service.LoginHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SocketController {

    @GetMapping("push")
    public void push() {
        LoginHandler loginHandler = new LoginHandler();
        loginHandler.pushMessage("7729");
    }
}

前端测试html




    
    NETTY SOCKET.IO DEMO
    
    
    
    



    

运行截图

SpringBoot 集成Netty-SocketIO_第1张图片

你可能感兴趣的:(springboot)