java用socket给用户分组,而后给分组发送消息,或者给单我的发送消息java
<dependency> <groupId>com.corundumstudio.socketio</groupId> <artifactId>netty-socketio</artifactId> <version>1.7.11</version> </dependency>
在项目pom文件中引入依赖spring
/** * 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启动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); } }