Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The webSocket message class cannot generate a document #850

Open
PGJiang opened this issue Jul 16, 2024 · 5 comments
Open

The webSocket message class cannot generate a document #850

PGJiang opened this issue Jul 16, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@PGJiang
Copy link

PGJiang commented Jul 16, 2024

The webSocket message class cannot generate a document

@PGJiang PGJiang added the enhancement New feature or request label Jul 16, 2024
@shalousun
Copy link
Collaborator

@PGJiang Can you provide some additional code examples?

@PGJiang PGJiang closed this as not planned Won't fix, can't repro, duplicate, stale Jul 22, 2024
@shalousun shalousun reopened this Aug 9, 2024
@skydream-xu
Copy link

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) {

@linwumingshi
Copy link
Collaborator

linwumingshi commented Aug 19, 2024

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

@skydream-xu
Copy link

skydream-xu commented Aug 21, 2024

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 :
com.ly.smart-doc
smart-doc-maven-plugin
3.0.7

@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());

 
    }
}

@linwumingshi
Copy link
Collaborator

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants