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

Websocket Annotation Support #45

Open
Bastitron opened this issue Jun 29, 2024 · 2 comments
Open

Websocket Annotation Support #45

Bastitron opened this issue Jun 29, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@Bastitron
Copy link

Would be nice to have annotations for websocket handling. It would make sense to make them work just like the other annotations:

@Endpoints("/api")
static final class ExampleEndpoints {

    private final ExampleService exampleService;

    // pass dependencies required to handle requests
    public ExampleEndpoints(ExampleService exampleService) {
        this.exampleService = exampleService;
    }

    @WsConnect("/ws")
    void onConnect(Context ctx) {
        // ...
    }
    
    @WsError("/ws")
    void onError(Context ctx) {
        // ...
    }
    
    @WsClose("/ws")
    void onClose(Context ctx) {
        // ...
    }
    
    @WsMessage("/ws")
    void onMessage(Context ctx) {
        // ...
    }
    
    @WsBinaryMessage("/ws")
    void onBinaryMessage(Context ctx) {
        // ...
    }
}
@dzikoysk dzikoysk added the enhancement New feature or request label Jun 29, 2024
@dzikoysk
Copy link
Member

dzikoysk commented Jul 11, 2024

I think it makes sense to support WebSockets, but I also think it'd make more sense if we'd keep these listeners a bit more connected to each other. Maybe something like:

interface WebsocketHandler {
  fun onConnect(ctx: WsContext) {}
  fun onError(ctx: WsContext) {}
  fun onClose(ctx: WsContext) {}
  fun onMessage(ctx: WsContext) {}
}

// -- example impl:

@Endpoints("/api")
class ExampleEndpoints(val exampleService: ExampleService) {

    @Get("/get/{id}")
    fun findById(Context ctx) {
        // ...
    }

    @Ws("/connect")
    fun connect(): WebsocketHandler {
        return ExampleWs()
    }

    inner class ExampleWs : WebsocketHandler {
        private var internalState = "monke"

        override fun onConnect(ctx: WsContext) {
            this.internalState = exampleService.doSomething()
        }
    }
}

@Bastitron
Copy link
Author

Good point, I would enjoy this kind of implementation.

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

2 participants