События

This commit is contained in:
Gravit 2018-10-01 17:01:10 +07:00
parent bf061bfaf2
commit b60bdb72fa
2 changed files with 24 additions and 10 deletions

View file

@ -1,26 +1,20 @@
package ru.gravit.launchserver.socket.websocket;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
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.AttributeKey;
import io.netty.util.AttributeMap;
import io.netty.util.concurrent.GlobalEventExecutor;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.socket.Client;
import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.LogHelper;
public class WebSocketFrameHandler extends SimpleChannelInboundHandler<WebSocketFrame> {
public static final ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
public static LaunchServer server;
public static GsonBuilder builder = new GsonBuilder();
public static WebSocketService service = new WebSocketService(LaunchServer.server,builder);
public static WebSocketService service = new WebSocketService(new DefaultChannelGroup(GlobalEventExecutor.INSTANCE), LaunchServer.server,builder);
private Client client;
static {
service.registerResponses();
@ -29,7 +23,7 @@ public class WebSocketFrameHandler extends SimpleChannelInboundHandler<WebSocket
public void channelActive(ChannelHandlerContext ctx) {
LogHelper.debug("New client %s", IOHelper.getIP(ctx.channel().remoteAddress()));
client = new Client(0);
channels.add(ctx.channel());
service.registerClient(ctx.channel());
}
@Override

View file

@ -2,7 +2,9 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.group.ChannelGroup;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.socket.Client;
@ -15,9 +17,12 @@
import java.util.HashMap;
public class WebSocketService {
public WebSocketService(LaunchServer server, GsonBuilder gson) {
public final ChannelGroup channels;
public WebSocketService(ChannelGroup channels, LaunchServer server, GsonBuilder gson) {
this.channels = channels;
this.server = server;
this.gsonBuiler = gson;
this.
gsonBuiler.registerTypeAdapter(JsonResponseInterface.class,new JsonResponseAdapter(this));
this.gson = gsonBuiler.create();
}
@ -47,6 +52,10 @@ public void registerResponse(String key,Class responseInterfaceClass)
{
responses.put(key,responseInterfaceClass);
}
public void registerClient(Channel channel)
{
channels.add(channel);
}
public void registerResponses()
{
registerResponse("echo", EchoResponse.class);
@ -56,6 +65,10 @@ public void sendObject(ChannelHandlerContext ctx, Object obj)
{
ctx.channel().writeAndFlush(new TextWebSocketFrame(gson.toJson(obj)));
}
public void sendEvent(EventResult obj)
{
channels.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj)));
}
public static class ErrorResult
{
public ErrorResult(String error) {
@ -76,7 +89,14 @@ public SuccessResult(String requesttype) {
public final String requesttype;
public final String type;
}
public class ExceptionResult
public static class EventResult
{
public EventResult() {
this.type = "event";
}
public final String type;
}
public static class ExceptionResult
{
public ExceptionResult(Exception e) {
this.message = e.getMessage();