Open PGJiang opened 3 months ago
@PGJiang Can you provide some additional code examples?
I have the same issue ,my code as follow: @ServerEndpoint("/documents/did/{did}/bid/{bid}") @RestController public class DocumentWebSocketController {
@OnMessage
public void receive(String message, Session session, @PathParam("did") int documentId,
@PathParam("bid") String branchBid) {
I have the same issue ,my code as follow: @serverendpoint("/documents/did/{did}/bid/{bid}") @RestController public class DocumentWebSocketController {
@OnMessage public void receive(String message, Session session, @PathParam("did") int documentId, @PathParam("bid") String branchBid) {
@skydream-xu
In WebSocket programming, it's common to handle URL parameters within the @OnOpen
method, where the WebSocket session is established, rather than in the @OnMessage
method. Here’s an example of how you might do this:
@ServerEndpoint("/documents/did/{did}/bid/{bid}")
@RestController
public class DocumentWebSocketController {
private int documentId;
private String branchBid;
/**
* Handles the opening of a WebSocket connection.
*
* @param session the WebSocket session
* @param documentId the ID of the document
* @param branchBid the ID of the branch
*/
@OnOpen
public void onOpen(Session session, @PathParam("did") int documentId,
@PathParam("bid") String branchBid) {
this.documentId = documentId;
this.branchBid = branchBid;
}
@OnMessage
public void receive(String message, Session session) {
// You can now use documentId and branchBid here
// Example usage:
System.out.println("Received message: " + message + " for Document ID: " + documentId + " and Branch ID: " + branchBid);
}
@OnClose
public void onClose(Session session) {
// Handle close event
}
}
By defining the parameters in the @OnOpen
method, you can store them as instance variables and use them across the WebSocket session, including in the @OnMessage
method. This approach helps keep the @OnMessage
method focused on processing messages rather than extracting path parameters.
You can refer to this example: @OnOpen
Thank you for your reply! My code is meet the requirement which you discribe, but it can't gen the api document. My code is follow: maven plugin :
<artifactId>smart-doc-maven-plugin</artifactId>
<version>3.0.7</version>
@Slf4j
@ServerEndpoint("/documents/did/{did}/bid/{bid}/mid/{mid}")
@RestController
public class DocumentWebSocketController {
private static final int HEARTBEAT_TIME = 30;
private static CommandFactory commandFactory;
private static ObjectMapper objectMapper;
@Autowired
public void init(CommandFactory commandFactory, ObjectMapper objectMapper) {
this.commandFactory = commandFactory;
this.objectMapper = objectMapper;
}
@OnMessage
public void receive(String message, Session session, @PathParam("did") int documentId,
@PathParam("bid") String branchBid) {
log.info("[websocket] 收到消息:id={},message={}", session.getId(), message);
}
@OnMessage
public void heartBeat(PongMessage heartBeat, Session session, @PathParam("did") int documentId,
@PathParam("bid") String branchBid) {
}
// 连接打开
@OnOpen
public void onOpen(Session session, @PathParam("did") int documentId, @PathParam("bid") String branchBid) {
log.info("[websocket] 新的连接:id={}, did={}, bid={}", session.getId(), documentId, branchBid);
}
// 连接关闭
@OnClose
public void onClose(Session session, CloseReason closeReason, @PathParam("did") int documentId, @PathParam("bid") String branchBid) {
log.info("[websocket] 连接断开:id={}, did={}, bid={},reason={}", session.getId(), documentId, branchBid, closeReason);
}
// 连接异常
@OnError
public void onError(Session session, Throwable throwable, @PathParam("did") int documentId, @PathParam("bid") String branchBid) throws IOException {
log.info("[websocket] 连接异常:id={},throwable={}, did={}, bid={}", session.getId(), documentId, branchBid, throwable.getMessage());
}
}
@skydream-xu I used your sample code to run, and the result was that the websocket document could be generated. For follow-up discussions, please go to discussions #898
The webSocket message class cannot generate a document