[Java]Springboot整合netty-soketio的最简易Demo

前端用soketio的话后端不能直接用netty了,不管怎么样先跑起来再想别的吧

线上测试地址为http://149.248.19.225:9092,不过不确定后期会不会被移除:)

给个index.html



 
  
  
  websocket-java-socketio
  
 

Socket.io Test

Waiting for input

hello world!

Spring-boot里

pom里

     
        
            com.corundumstudio.socketio
            netty-socketio
            1.7.17
        
        
            org.projectlombok
            lombok
            1.18.4
            provided
        

 直接用注释的话就不用改启动类了,所以直接给代码,配置也写代码不搞那么多乱七八糟的了,跑起来就即可

[Java]Springboot整合netty-soketio的最简易Demo_第1张图片

package yiki.mybatis.netty_socketio;

import lombok.Data;

@Data
public class Message {

    private String msg;
}
package yiki.mybatis.netty_socketio;

import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnDisconnect;
import com.corundumstudio.socketio.annotation.OnEvent;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

@Component
public class MessageEventHandler {


    public static ConcurrentMap socketIOClientMap =
            new ConcurrentHashMap<>();

    /**
     * 客户端连接的时候触发
     *
     * @param client
     */
    @OnConnect
    public void onConnect(SocketIOClient client) {
        String mac = client.getHandshakeData().getSingleUrlParam("mac");
        //存储SocketIOClient,用于发送消息
        socketIOClientMap.put(mac, client);
        //回发消息
        client.sendEvent("message", "onConnect back");
        System.out.println("客户端:" + client.getSessionId() + "已连接,mac=" + mac);
    }

    /**
     * 客户端关闭连接时触发
     *
     * @param client
     */
    @OnDisconnect
    public void onDisconnect(SocketIOClient client) {
        System.out.println("客户端:" + client.getSessionId() + "断开连接");
    }

    /**
     * 客户端事件
     *
     * @param client   客户端信息
     * @param request 请求信息
     * @param data     客户端发送数据
     */
    @OnEvent(value = "messageevent")
    public void onEvent(SocketIOClient client, AckRequest request, Message data) {
        //回发消息
        client.sendEvent("messageevent", "我是服务器都安发送的信息");
        //广播消息
        sendBroadcast();
    }

    /**
     * 广播消息
     */
    public void sendBroadcast() {
        for (SocketIOClient client : socketIOClientMap.values()) {
            if (client.isChannelOpen()) {
                client.sendEvent("Broadcast", "当前时间", System.currentTimeMillis());
            }
        }

    }
}
package yiki.mybatis.netty_socketio;

import com.corundumstudio.socketio.SocketIOServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class Netty_SocketIo_Sever implements CommandLineRunner {

    @Autowired
    private SocketIOServer socketIOServer;


    @Override
    public void run(String... args) throws Exception {
        this.socketIOServer.start();

    }
}
package yiki.mybatis.netty_socketio;

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

@Configuration
public class NettySocketioConfig {

    @Bean
    public SocketIOServer socketIOServer(){
        com.corundumstudio.socketio.Configuration config =
                new com.corundumstudio.socketio.Configuration();
        config.setHostname("localhost");//部署线上记得改ip!!!!
        config.setPort(9092);

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

    @Bean
    public SpringAnnotationScanner springAnnotationScanner() {
        return new SpringAnnotationScanner(socketIOServer());
    }

}

 

你可能感兴趣的:(Java,Spring)