搭建SpringBoot+ Netty + WebSocket 通信协议框架

本文档介绍了如何结合SpringBoot和Netty搭建WebSocket通信协议框架,适用于与机器设备通讯等场景。文中详细列举了从配置类、properties配置、Action、Command注解到Handler接口和帧定义的步骤,并提供了在线测试工具和测试方法。
摘要由CSDN通过智能技术生成

运用场景:与机器设备进行通讯或者其他场景;
pom文件就不上传了,直接上代码,网上都可以找的到 主要是 SpringBoot 和 Netty 的依赖

1.配置类

@Component
@ConfigurationProperties(prefix = "ws")
public class WebSocketConfig {

    private int port;
    private String host;
    private boolean ssl;
    private String cert;
    private String key;

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public boolean getSsl() {
        return ssl;
    }

    public void setSsl(boolean ssl) {
        this.ssl = ssl;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getCert() {
        return cert;
    }

    public void setCert(String cert) {
        this.cert = cert;
    }
}

2.properties 配置文件

ws.ssl=false
ws.port=8082
ws.host=localhost

3.Action

public class Action {

    private Object target;

    private Method method;


    /**
     * @param target
     * @param method
     */
    public Action(Object target, Method method) {
        this.target = target;
        this.method = method;
    }

    public Object call(Object... args) throws InvocationTargetException, IllegalAccessException {
        return method.invoke(target, args);
    }

    public Object getTarget() {
        return target;
    }

    public void setTarget(Object target) {
        this.target = target;
    }

    public Method getMethod() {
        return method;
    }

    public void setMethod(Method method) {
        this.method = method;
    }

    @Override
    public String toString() {
        return "ActionMethod{" +
                "target=" + target +
                ", method=" + method +
                '}';
    }
}

4.定义Command注解

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
&#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值