我正在开发一个 Spring Boot 应用程序,使用套接字 io 服务器实现与客户端通信。我的意图是在特定功能中从服务器向客户端发送一个推送通知。我如何使它成为可能。我需要在单独的服务类中触发发送事件。Socket IO 实现类@Configurationpublic class Socket { @Value("${socket.io.ip}") private String hostIP; @Value("${socket.io.port}") private int hostPort; @Bean public void setupSocketServer() { Configuration config = new Configuration(); config.setHostname(hostIP); config.setPort(hostPort); SocketIOServer socketServer = new SocketIOServer(config); socketServer.addEventListener("chat", ChatObject.class, new DataListener<ChatObject>() { @Override public void onData(final SocketIOClient client, ChatObject data, final AckRequest ackRequest) { // check is ack requested by client, // but it's not required check if (ackRequest.isAckRequested()) { // send ack response with data to client ackRequest.sendAckData("client message was delivered to server!", "yeah!"); } // send message back to client with ack callback WITH data ChatObject ackChatObjectData = new ChatObject(data.getUserName(), "message with ack data"); client.sendEvent("chatResponse1", new AckCallback<String>(String.class) { @Override public void onSuccess(String result) { System.out.println("ack from client: " + client.getSessionId() + " data: " + result); } }, ackChatObjectData); } }); socketServer.start(); }}服务等级class userService{ void sendnotification(msg){ // need to call sendEvent function here}}
1 回答
函数式编程
TA贡献1807条经验 获得超9个赞
我通过使用套接字客户端的 UUID 实现了它。使用在将新客户端连接到服务器时保存的 UUID 创建了一个套接字客户端。并在我想要的任何地方重复使用它。
SocketIOClient client = socketServer.getClient(uuid);
添加回答
举报
0/150
提交
取消