[FEATURE][EXPERIMENTAL] onOpen реализован

This commit is contained in:
Gravit 2019-04-29 13:38:35 +07:00
parent 1cc08204e5
commit 323cfe763a
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
3 changed files with 12 additions and 7 deletions

View file

@ -84,6 +84,7 @@ public ChannelFuture send(String text)
} }
abstract void onMessage(String message) throws Exception; abstract void onMessage(String message) throws Exception;
abstract void onDisconnect() throws Exception; abstract void onDisconnect() throws Exception;
abstract void onOpen() throws Exception;
public void close() throws InterruptedException { public void close() throws InterruptedException {
//System.out.println("WebSocket Client sending close"); //System.out.println("WebSocket Client sending close");

View file

@ -63,6 +63,14 @@ void onDisconnect() {
if(onCloseCallback != null) onCloseCallback.onClose(0,"unsupported param", !isClosed); if(onCloseCallback != null) onCloseCallback.onClose(0,"unsupported param", !isClosed);
} }
@Override
void onOpen() throws Exception {
synchronized (onConnect)
{
onConnect.notifyAll();
}
}
@FunctionalInterface @FunctionalInterface
public interface OnCloseCallback public interface OnCloseCallback
{ {

View file

@ -23,11 +23,6 @@ public class WebSocketClientHandler extends SimpleChannelInboundHandler<Object>
private final WebSocketClientHandshaker handshaker; private final WebSocketClientHandshaker handshaker;
private final ClientJSONPoint clientJSONPoint; private final ClientJSONPoint clientJSONPoint;
private ChannelPromise handshakeFuture; private ChannelPromise handshakeFuture;
interface OnMessageCallback
{
void onMessage(String text);
}
public OnMessageCallback onMessageCallback;
public WebSocketClientHandler(final WebSocketClientHandshaker handshaker, ClientJSONPoint clientJSONPoint) { public WebSocketClientHandler(final WebSocketClientHandshaker handshaker, ClientJSONPoint clientJSONPoint) {
this.handshaker = handshaker; this.handshaker = handshaker;
@ -46,6 +41,7 @@ public void handlerAdded(final ChannelHandlerContext ctx) throws Exception {
@Override @Override
public void channelActive(final ChannelHandlerContext ctx) throws Exception { public void channelActive(final ChannelHandlerContext ctx) throws Exception {
handshaker.handshake(ctx.channel()); handshaker.handshake(ctx.channel());
clientJSONPoint.onOpen();
} }
@Override @Override
@ -66,7 +62,7 @@ protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Except
if (msg instanceof FullHttpResponse) { if (msg instanceof FullHttpResponse) {
final FullHttpResponse response = (FullHttpResponse) msg; final FullHttpResponse response = (FullHttpResponse) msg;
throw new Exception("Unexpected FullHttpResponse (getStatus=" + response.getStatus() + ", content=" throw new Exception("Unexpected FullHttpResponse (getStatus=" + response.status() + ", content="
+ response.content().toString(CharsetUtil.UTF_8) + ')'); + response.content().toString(CharsetUtil.UTF_8) + ')');
} }
@ -89,7 +85,7 @@ else if (frame instanceof BinaryWebSocketFrame) {
@Override @Override
public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) throws Exception { public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) throws Exception {
cause.printStackTrace(); LogHelper.error(cause);
if (!handshakeFuture.isDone()) { if (!handshakeFuture.isDone()) {
handshakeFuture.setFailure(cause); handshakeFuture.setFailure(cause);