spring boot集成socketIO

spring boot集成netty-socketio

java用socket给用户分组,然后给分组发送消息,或者给单个人发送消息

第一步:加载netty-socketio包

<dependency>
	<groupId>com.corundumstudio.socketio</groupId>
	<artifactId>netty-socketio</artifactId>
	<version>1.7.11</version>
</dependency>

在项目pom文件中引入依赖

 

第二步:修改启动文件

/**
	 * socket
	 */
	@Bean
	public SocketIOServer socketIOServer() {
		com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();
		config.setPort(port);
 
		final SocketIOServer server = new SocketIOServer(config);
		return server;
	}
 
	@Bean
	public SpringAnnotationScanner springAnnotationScanner(SocketIOServer socketServer) {
		return new SpringAnnotationScanner(socketServer);
	}

在启动文件中添加socket启动

 

第三步:事件接收与发送消息

import com.corundumstudio.socketio.SocketIOServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
 
 
@Component
@Order(value=1)
public class MyCommandLineRunner implements CommandLineRunner {
    private final SocketIOServer server;
 
 
    @Autowired
    public MyCommandLineRunner(SocketIOServer server) {
        this.server = server;
    }
 
    @Override
    public void run(String... args) throws Exception {
        server.start();
        System.out.println("socket.io启动成功!");
    }
}

 


public class MessageInfo {
	String MsgContent;
	 
    public String getMsgContent() {
        return MsgContent;
    }
 
    public void setMsgContent(String msgContent) {
        MsgContent = msgContent;
    }

}

 

import java.util.ArrayList;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnDisconnect;
import com.corundumstudio.socketio.annotation.OnEvent;
 
@Component
public class MessageEventHandler {
    public static SocketIOServer socketIoServer;
    static ArrayList<UUID> listClient = new ArrayList<>();
    static final int limitSeconds = 60;
 
	@Autowired
    public MessageEventHandler(SocketIOServer server) {
        this.socketIoServer = server;
    }
 
    @OnConnect
    public void onConnect(SocketIOClient client) {
        listClient.add(client.getSessionId());
        
        System.out.println("客户端:" + client.getSessionId() + "已连接");
    }
 
    @OnDisconnect
    public void onDisconnect(SocketIOClient client) {
        System.out.println("客户端:" + client.getSessionId() + "断开连接");
    }
 
    @OnEvent(value = "join")
    public void joinEvent(SocketIOClient client, AckRequest request, String roomId) {
        System.out.println("发来ssss消息:" + roomId);
        client.joinRoom(roomId);
    }
    
 
    @OnEvent(value = "messageevent")
    public void onEvent(SocketIOClient client, AckRequest request, MessageInfo data) {
        System.out.println("发来消息:" + data.getMsgContent());
        socketIoServer.getClient(client.getSessionId()).sendEvent("messageevent", "back data");
    }
 
    /**
     * 给单人发信息
     */
    public static void sendMessageToPeople(UUID clientId, String messageContent){
    	socketIoServer.getClient(clientId).sendEvent("messageevent", messageContent);
    }
    
    /**
     * 给连接池中所有人发信息
     */
    public static void sendMessageToListClient(String messageContent) {   //这里就是向客户端推消息了
        for (UUID clientId : listClient) {
            if (socketIoServer.getClient(clientId) == null) continue;
            socketIoServer.getClient(clientId).sendEvent("messageevent", messageContent);
        }
    }
    
    /**
     * 给指定房间发送消息
     */
    public static void sendMessageToRoom(String roomNumber, String messageContent) {
    	socketIoServer.getRoomOperations(roomNumber).sendEvent("messageevent", messageContent);
    }
}

 

转载于:https://my.oschina.net/u/1858920/blog/2988442

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值