mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-22 16:41:46 +03:00
WebSocketService
This commit is contained in:
parent
a1bb6d6e2f
commit
b42d3d6a08
4 changed files with 65 additions and 4 deletions
|
@ -0,0 +1,57 @@
|
|||
package ru.gravit.launchserver.socket.websocket;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
|
||||
import ru.gravit.launchserver.LaunchServer;
|
||||
import ru.gravit.launchserver.socket.websocket.json.JsonResponse;
|
||||
import ru.gravit.utils.helper.LogHelper;
|
||||
|
||||
public class WebSocketService {
|
||||
public WebSocketService(LaunchServer server, Gson gson) {
|
||||
this.server = server;
|
||||
this.gson = gson;
|
||||
}
|
||||
|
||||
private final LaunchServer server;
|
||||
private final Gson gson;
|
||||
|
||||
void process(ChannelHandlerContext ctx, TextWebSocketFrame frame)
|
||||
{
|
||||
String request = frame.text();
|
||||
JsonResponse response = gson.fromJson(request, JsonResponse.class);
|
||||
try {
|
||||
response.execute(this,ctx);
|
||||
} catch (Exception e)
|
||||
{
|
||||
LogHelper.error(e);
|
||||
sendObject(ctx,new ExceptionResult(e));
|
||||
}
|
||||
}
|
||||
public void sendObject(ChannelHandlerContext ctx, Object obj)
|
||||
{
|
||||
ctx.channel().writeAndFlush(new TextWebSocketFrame(gson.toJson(obj)));
|
||||
}
|
||||
public class ErrorResult
|
||||
{
|
||||
public ErrorResult(String error) {
|
||||
this.error = error;
|
||||
this.type = "requestError";
|
||||
}
|
||||
|
||||
public final String error;
|
||||
public final String type;
|
||||
}
|
||||
public class ExceptionResult
|
||||
{
|
||||
public ExceptionResult(Exception e) {
|
||||
this.message = e.getMessage();
|
||||
this.clazz = e.getClass().getName();
|
||||
this.type = "exceptionError";
|
||||
}
|
||||
|
||||
public final String message;
|
||||
public final String clazz;
|
||||
public final String type;
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@
|
|||
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.launchserver.socket.websocket.WebSocketService;
|
||||
import ru.gravit.utils.helper.LogHelper;
|
||||
|
||||
public class EchoResponse implements JsonResponse {
|
||||
|
@ -19,9 +20,10 @@ public String getType() {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void execute(ChannelHandlerContext ctx, WebSocketFrame frame) {
|
||||
public void execute(WebSocketService service,ChannelHandlerContext ctx) {
|
||||
LogHelper.info("Echo: %s",echo);
|
||||
ctx.channel().writeAndFlush(new TextWebSocketFrame(WebSocketFrameHandler.gson.toJson(new Result(echo))));
|
||||
service.sendObject(ctx,new Result(echo));
|
||||
}
|
||||
public class Result
|
||||
{
|
||||
|
|
|
@ -2,8 +2,9 @@
|
|||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
|
||||
import ru.gravit.launchserver.socket.websocket.WebSocketService;
|
||||
|
||||
public interface JsonResponse {
|
||||
String getType();
|
||||
void execute(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception;
|
||||
void execute(WebSocketService service,ChannelHandlerContext ctx) throws Exception;
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
import ru.gravit.launchserver.auth.provider.AuthProvider;
|
||||
import ru.gravit.launchserver.auth.provider.AuthProviderResult;
|
||||
import ru.gravit.launchserver.socket.websocket.WebSocketFrameHandler;
|
||||
import ru.gravit.launchserver.socket.websocket.WebSocketService;
|
||||
import ru.gravit.launchserver.socket.websocket.json.JsonResponse;
|
||||
import ru.gravit.utils.helper.IOHelper;
|
||||
import ru.gravit.utils.helper.VerifyHelper;
|
||||
|
@ -40,7 +41,7 @@ public String getType() {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void execute(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception {
|
||||
public void execute(WebSocketService service, ChannelHandlerContext ctx) throws Exception {
|
||||
try {
|
||||
String ip = IOHelper.getIP(ctx.channel().remoteAddress());
|
||||
if (server.limiter.isLimit(ip)) {
|
||||
|
@ -68,7 +69,7 @@ public void execute(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exce
|
|||
server.config.hwidHandler.check(hwid, result.username);
|
||||
} catch (AuthException | HWIDException e)
|
||||
{
|
||||
ctx.channel().writeAndFlush(new TextWebSocketFrame(WebSocketFrameHandler.gson.toJson(new ErrorResult(e.getMessage()))));
|
||||
service.sendObject(ctx,new ErrorResult(e.getMessage()));
|
||||
}
|
||||
}
|
||||
public class ErrorResult
|
||||
|
|
Loading…
Reference in a new issue