Фикс отправления ответов

This commit is contained in:
Gravit 2018-10-01 14:55:09 +07:00
parent 7f952af1b9
commit a1bb6d6e2f
3 changed files with 13 additions and 7 deletions

View file

@ -8,6 +8,7 @@
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import io.netty.util.concurrent.GlobalEventExecutor;
import ru.gravit.launchserver.LaunchServer;
@ -38,10 +39,13 @@ public void channelActive(ChannelHandlerContext ctx) {
@Override
protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception {
// ping and pong frames already handled
ByteBuf buf = frame.content();
ByteBufInputStream input = new ByteBufInputStream(buf);
Reader reader = new InputStreamReader(input, "UTF-8");
JsonResponse response = gson.fromJson(reader,JsonResponse.class);
response.execute(ctx,frame);
if (frame instanceof TextWebSocketFrame) {
String request = ((TextWebSocketFrame) frame).text();
JsonResponse response = gson.fromJson(request, JsonResponse.class);
response.execute(ctx, frame);
} else {
String message = "unsupported frame type: " + frame.getClass().getName();
throw new UnsupportedOperationException(message);
}
}
}

View file

@ -1,6 +1,7 @@
package ru.gravit.launchserver.socket.websocket.json;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import ru.gravit.launchserver.socket.websocket.WebSocketFrameHandler;
import ru.gravit.utils.helper.LogHelper;
@ -20,7 +21,7 @@ public String getType() {
@Override
public void execute(ChannelHandlerContext ctx, WebSocketFrame frame) {
LogHelper.info("Echo: %s",echo);
ctx.channel().writeAndFlush(WebSocketFrameHandler.gson.toJson(new Result(echo)));
ctx.channel().writeAndFlush(new TextWebSocketFrame(WebSocketFrameHandler.gson.toJson(new Result(echo))));
}
public class Result
{

View file

@ -1,6 +1,7 @@
package ru.gravit.launchserver.socket.websocket.json.auth;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import ru.gravit.launcher.profiles.ClientProfile;
import ru.gravit.launcher.serialize.signed.SignedObjectHolder;
@ -67,7 +68,7 @@ public void execute(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exce
server.config.hwidHandler.check(hwid, result.username);
} catch (AuthException | HWIDException e)
{
ctx.channel().writeAndFlush(WebSocketFrameHandler.gson.toJson(new ErrorResult(e.getMessage())));
ctx.channel().writeAndFlush(new TextWebSocketFrame(WebSocketFrameHandler.gson.toJson(new ErrorResult(e.getMessage()))));
}
}
public class ErrorResult