Skip to content

Commit

Permalink
feat: ws sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuangcp committed Jul 12, 2024
1 parent 80e02dc commit 5f90931
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 342 deletions.
12 changes: 12 additions & 0 deletions gui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<ws-starter.version>1.0.5-SNAPSHOT</ws-starter.version>
</properties>


Expand All @@ -33,6 +34,17 @@
<artifactId>commons-lang3</artifactId>
</dependency>

<!-- <dependency>-->
<!-- <groupId>com.github.kuangcp</groupId>-->
<!-- <artifactId>netty-ws</artifactId>-->
<!-- <version>${ws-starter.version}</version>-->
<!-- </dependency>-->
<dependency>
<groupId>com.github.kuangcp</groupId>
<artifactId>netty-ws-core</artifactId>
<version>${ws-starter.version}</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
Expand Down
15 changes: 13 additions & 2 deletions gui/src/main/java/com/github/kuangcp/tank/MainTankGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.github.kuangcp.tank.frame.MainFrame;
import com.github.kuangcp.tank.resource.ResourceMgr;
import com.github.kuangcp.tank.util.executor.MonitorExecutor;
import com.github.kuangcp.tank.ws.WsServer;
import com.github.kuangcp.tank.ws.WsBizHandler;
import com.github.kuangcp.websocket.WsServer;
import com.github.kuangcp.websocket.WsServerConfig;
import lombok.extern.slf4j.Slf4j;

import java.awt.*;
Expand All @@ -22,11 +24,20 @@ public static void main(String[] args) {
ResourceMgr.loadResource();
log.info("finish load resources");

new Thread(() -> new WsServer().init()).start();
startWsServer();

EventQueue.invokeLater(new MainFrame());
} catch (Exception e) {
log.error("", e);
}
}

private static void startWsServer() {
final WsServerConfig config = new WsServerConfig();
final WsServer wsServer = new WsServer(config, new WsBizHandler(config));
final Thread wsThread = new Thread(wsServer::start);
wsThread.setDaemon(true);
wsThread.setName("ws-server");
wsThread.start();
}
}
2 changes: 1 addition & 1 deletion gui/src/main/java/com/github/kuangcp/tank/ws/Readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# 远程通信

> [Source](https://github.com/Kuangcp/JavaBase/tree/master/netty/src/main/java/netty/websocket)
> [改进组件 netty-ws](https://github.com/Kuangcp/netty-ws-starter)
TODO

Expand Down
43 changes: 43 additions & 0 deletions gui/src/main/java/com/github/kuangcp/tank/ws/WsBizHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.github.kuangcp.tank.ws;

import com.github.kuangcp.tank.mgr.PlayStageMgr;
import com.github.kuangcp.tank.util.HoldingKeyStateMgr;
import com.github.kuangcp.tank.util.Json;
import com.github.kuangcp.tank.ws.msg.MsgPack;
import com.github.kuangcp.websocket.WsServerConfig;
import com.github.kuangcp.websocket.handler.AbstractBizHandler;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;

/**
* @author <a href="https://github.com/kuangcp">Kuangcp</a> on 2024-07-13 00:40
*/
@Slf4j
@ChannelHandler.Sharable
public class WsBizHandler extends AbstractBizHandler {

public WsBizHandler(WsServerConfig config) {
super(config);
}

@Override
public void connectSuccess(Long userId) {
log.info("CONN userId={}", userId);
}

@Override
protected void textWebSocketFrameHandler(ChannelHandlerContext ctx, TextWebSocketFrame frame) {
final String text = frame.text();
final MsgPack pack = Json.fromJson(text, MsgPack.class);

HoldingKeyStateMgr.instance.handleJoy(pack.getDirect());
if (BooleanUtils.isTrue(pack.getShot())) {
PlayStageMgr.instance.hero.shotEnemy();
}

ctx.channel().writeAndFlush("OK");
}
}

This file was deleted.

This file was deleted.

10 changes: 0 additions & 10 deletions gui/src/main/java/com/github/kuangcp/tank/ws/WsConst.java

This file was deleted.

Loading

0 comments on commit 5f90931

Please sign in to comment.