[FEATURE] Возможность отправлять сообщения всем клиентам

This commit is contained in:
Gravit 2019-05-03 20:49:00 +07:00
parent 47e33b0588
commit b807dd871a
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
2 changed files with 21 additions and 0 deletions

View file

@ -1,8 +1,11 @@
package ru.gravit.launchserver.command.basic;
import ru.gravit.launcher.events.PingEvent;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.command.Command;
import ru.gravit.launchserver.websocket.NettyServerSocketHandler;
import ru.gravit.launchserver.websocket.WebSocketFrameHandler;
import ru.gravit.launchserver.websocket.WebSocketService;
import ru.gravit.utils.helper.CommonHelper;
public class TestCommand extends Command {
@ -33,5 +36,9 @@ public void invoke(String... args) throws Exception {
if (args[0].equals("stop")) {
handler.close();
}
if (args[0].equals("eventAll"))
{
WebSocketFrameHandler.service.sendObjectAll(new PingEvent());
}
}
}

View file

@ -125,6 +125,20 @@ public void sendObject(ChannelHandlerContext ctx, Object obj, Type type) {
ctx.channel().writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, type)));
}
public void sendObjectAll(Object obj) {
for(Channel ch : channels)
{
ch.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, ResultInterface.class)));
}
}
public void sendObjectAll(Object obj, Type type) {
for(Channel ch : channels)
{
ch.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, type)));
}
}
public void sendObjectAndClose(ChannelHandlerContext ctx, Object obj) {
ctx.channel().writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, ResultInterface.class))).addListener(ChannelFutureListener.CLOSE);
}