我正在开发一个Spring Boot应用程序,并通过套接字io服务器实现与客户端进行通信。我的意图是在特定功能中从服务器向客户端发送一个推送通知。我如何使其成为可能。我需要在单独的服务类中触发send事件。
Socket IO实现类
@Configuration
public 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
}
}
答案 0 :(得分:0)
我通过使用套接字客户端的UUID实现了它。使用UUID创建了套接字客户端,该套接字客户端在将新客户端连接到服务器时被保存。并在需要的地方重复使用。
SocketIOClient client = socketServer.getClient(uuid);