前言
关于vertx介绍这里就不多说了,官网说的很详细,https://vertx.io/
服务端搭建
创建springboot项目,引入依赖
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>4.2.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
创建Verticle,用Verticle创建更易于管理,
关于verticle介绍可以看这里https://vertx.io/docs/vertx-core/java/#_verticles
public class MyVerticle extends AbstractVerticle {
NetServer server;
@Override
public void start() throws Exception {
NetServerOptions netServerOptions = new NetServerOptions();
netServerOptions.setReadIdleTimeout(120);
server = vertx.createNetServer(netServerOptions);
server.connectHandler(
netSocket -> {
netSocket.handler(
buffer -> {
System.out.println(buffer.length());
this.handlerSocket(buffer,netSocket);
}
);
netSocket.closeHandler(
close -> {
System.out.println("通道"+netSocket+"已关闭");
}
);
}
);
server.listen(6789,"0.0.0.0");
}
private void handlerSocket(Buffer buffer, NetSocket netSocket) {
System.out.println(buffer.toString());
}
}
配置vertx,并部署Verticle
@Configuration
public class VertxConfig {
@Bean
public Vertx myVertx() {
Vertx vertx = Vertx.vertx();
Future<String> future = vertx.deployVerticle(MyVerticle.class, new DeploymentOptions());
future.onSuccess(
handler -> {
System.out.println(future);
}
);
Set<VerticleFactory> verticleFactories = vertx.verticleFactories();
return vertx;
}
}
这里就创建完成了
通过软件测试,这里推荐一个TCP测试工具https://www.usr.cn/Down/Software/USR-TCP232-Test-V1.3.exe 打开软件,设置端口链接 可以看到控制台已经打印出消息了
总结
以上就是今天要讲的内容,本文仅仅简单介绍了vertx tcpserver的使用
|