本文主要是介绍搭建SpringBoot+ Netty + WebSocket 通信协议框架,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
运用场景:与机器设备进行通讯或者其他场景;
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;}@Overridepublic String toString() {return "ActionMethod{" +"target=" + target +", method=" + method +'}';}
}
4.定义Command注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
&#
这篇关于搭建SpringBoot+ Netty + WebSocket 通信协议框架的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!