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); } }