代码拉取完成,页面将自动刷新
同步操作将从 yuye/Magician 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
Magician 是一个异步非阻塞的网络协议解析包,支持Http, WebSocket, UDP等协议
JDK11+
<dependency>
<groupId>com.github.yuyenews</groupId>
<artifactId>Magician</artifactId>
<version>最新版</version>
</dependency>
<!-- 这个是日志包,支持任意可以跟slf4j桥接的包 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.12</version>
</dependency>
public class DemoHandler implements MagicianHandler<MagicianRequest> {
@Override
public void request(MagicianRequest magicianRequest) {
// 响应数据
magicianRequest.getResponse()
.sendJson(200, "{'status':'ok'}");
}
}
Magician.createTCPServer()
.handler("/", new DemoHandler())
.bind(8080);
EventGroup ioEventGroup = new EventGroup(1, Executors.newCachedThreadPool());
EventGroup workerEventGroup = new EventGroup(10, Executors.newCachedThreadPool());
// 当前EventRunner没任务的时候,允许从其他EventRunner窃取任务
workerEventGroup.setSteal(EventEnum.STEAL.YES);
Magician.createTCPServer(ioEventGroup, workerEventGroup)
.handler("/", new DemoHandler())
.bind(8080);
// 监听几个端口,ioEventGroup的第一个参数就写几
EventGroup ioEventGroup = new EventGroup(2, Executors.newCachedThreadPool());
EventGroup workerEventGroup = new EventGroup(10, Executors.newCachedThreadPool());
// 当前EventRunner没任务的时候,允许从其他EventRunner窃取任务
workerEventGroup.setSteal(EventEnum.STEAL.YES);
TCPServer tcpServer = Magician
.createTCPServer(ioEventGroup, workerEventGroup)
.handler("/", new DemoHandler())
tcpServer.bind(8080);
tcpServer.bind(8088);
只需要在创建http服务的时候加一个handler即可
Magician.createTCPServer()
.handler("/", new DemoHandler())
.webSocketHandler("/websocket", new DemoSocketHandler())
.bind(8080);
Magician.createUdpServer()
.handler(outputStream -> {
// outputStream 是ByteArrayOutputStream类型的
// 它是客户端发过来的数据,自行解析即可
}).bind(8088);
除了这种写法,也可以单独创建handler,在这里add进去
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。