Human-readable method names for web socket API (Java)
See original GitHub issueDescribe the feature
What about renaming the method names handle()
in the websocket handler endpoints CloseHandler
, MessageHandkler
, ConnectHandler
, and so on?
The target is to make Java class-based implementations more readable instead of having multiple handle()
methods on the same class.
Additional context
Current
private static class FooWebsocket implements CloseHandler, MessageHandler, ConnectHandler, ErrorHandler, BinaryMessageHandler {
@Override
public void handle(WsSession session) throws Exception {}
@Override
public void handle(WsSession session, String msg) throws Exception {}
@Override
public void handle(WsSession session, int statusCode, String reason) throws Exception {}
@Override
public void handle(WsSession session, Byte[] msg, int offset, int length) throws Exception {}
@Override
public void handle(WsSession session, Throwable throwable) throws Exception {}
}
Idea 1
private static class FooWebsocket implements CloseHandler, MessageHandler, ConnectHandler, ErrorHandler, BinaryMessageHandler {
@Override
public void onConnect(WsSession session) throws Exception {}
@Override
public void onMessage(WsSession session, String msg) throws Exception {}
@Override
public void onClose(WsSession session, int statusCode, String reason) throws Exception {}
@Override
public void onBinaryMessage(WsSession session, Byte[] msg, int offset, int length) throws Exception {}
@Override
public void onError(WsSession session, Throwable throwable) throws Exception {}
}
Idea 2
/**
* Usage: `app.ws("/my/websocket", Websocket.of(new MyWobsocketHandlers()))`
*
* `class MyWebsicketHandlers implements WsHandlers { ... }`
*/
private static final class Websocket implements Consumer<WsHandler> {
private final WsHandlers delegate;
private Websocket(WsHandlers delegate) {
this.delegate = delegate;
}
@Override
public void accept(WsHandler t) {
t.onConnect(delegate::onConnect);
t.onMessage(delegate::onMessage);
/* .. */
}
public static interface WsHandlers {
public abstract void onConnect(WsSession session) throws Exception;
public abstract void onMessage(WsSession session, String msg);
/* .. */
}
public static Websocket of(WsHandlers handlers) {
return new BaseWsHandler(handlers);
}
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
A Guide to the Java API for WebSocket - Baeldung
WebSocket provides an alternative to the limitation of efficient communication between the server and the web browser by providing ...
Read more >Creating AsyncAPI for WebSocket API - Step by Step
First, provide some basic information that every good AsyncAPI file should have: What AsyncAPI version do you use? What is the name of...
Read more >WebSockets With Spring, Part 1: HTTP and WebSocket
The API provides the close method to close the connection. The method has an optional status code and an optional human-readable reason. Once ......
Read more >JSR 356, Java API for WebSocket - Oracle
JSR 356, Java API for WebSocket, specifies the API that Java developers can use when they want to integrate WebSockets into their applications—both...
Read more >WebSockets - Send & Receive Messages - Tutorialspoint
It is a lightweight format for transferring human-readable data between the computers. The structure of JSON consists of key-value pairs. Example. { name:...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@dherges Actually I like your as well as @tipsy approaches. If you still wish to have class that overrides methods it needs, you could quickly create helper class in your project:
I see! You could also do:
Is a new class really necessary? 🤔