-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
adds WebSocketListener at WebSocketService #787
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @zacscoding
It would make sense to have something like this at the lower level within WebSocketClient
.
In there perhaps we could have a List<WebSocketListener>
that is traversed and notified of events from within WebSocketClient
.
Then modify WebSocketService
to include a addWebSocketListener()
method that will add the argument to the webSocketClient
member.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can see checkstyle errors by running ./gradlew check
locally.
I want to handle websocket events such as onClose event.
I think a cleaner approach is to overload setWebSocketListener()
to allow it to take functions as parameters which are invoked in the handlers. Something like:
private void setWebSocketListener(Consumer<String> onMessage, Consumer<Exception> onError, Runnable onClose) {
webSocketClient.setListener(new WebSocketListener() {
@Override
public void onMessage(String message) throws IOException {
onWebSocketMessage(message);
onMessage.accept(message);
}
@Override
public void onError(Exception e) {
log.error("Received error from a WebSocket connection", e);
onError.accept(e);
}
@Override
public void onClose() {
onWebSocketClose();
onClose.run();
}
});
}
I think that it will be better if WebSocketClient have multiple listeners and WebSocketSerivce`s listener
is added.
AFAIK WebSocketClient
can only have 1 listener.
To get this functionality we need to maintain a list (probably a map) of WebSocketClient
in WebSocketService
how about this ?
I don`t know how to maintain multiple WebSocketClient in WebSocketService TT |
Your proposed solution re: consumers would be less intrusive on the tests, so i am ok with it. |
added functional listeners at WebSocketService. |
@@ -106,21 +111,30 @@ private void connectToWebSocket() throws InterruptedException, ConnectException | |||
} | |||
} | |||
|
|||
private void setWebSocketListener() { | |||
private void setWebSocketListener(Consumer<String> onMessage, Consumer<Throwable> onError, Runnable onClose) { | |||
webSocketClient.setListener(new WebSocketListener() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zacscoding , use of Runnable
can be confusing as it would imply the implementation is invoked by another thread. Could you instead use Consumer<Void>
please?
@iikirilov , thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If use Consumer, then have to called like onClose.accept(null);
is ok ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed to consumer :)
@zacscoding can you please resolve the travis-ci checks, as per Ivaylo's suggestion? |
appreciate ur kindness 👍 |
@snazha-blkio I updated source with checkstyle. is any problem? |
Could you please add some unit tests for your changes? We can then look at getting this into 5.0 |
@snazha-blkio added some testing code ! |
Thanks @zacscoding , this will go out in 5.0 |
@snazha-blkio is there any reason why this pull request cannot be released on 4.* versions? There is no breaking changes in public interface. We really need this feature. Thank you. |
@olimsaidov yes we can do this |
What does this PR do?
added WebSocketListener at WebSocketService
Where should the reviewer start?
WebSocketService, WebSocketServiceTest
Why is it needed?
I want to handle websocket events such as onClose event.
I think that it will be better if WebSocketClient have multiple listeners and WebSocketSerivce`s listener
is added.