Giter VIP home page Giter VIP logo

magician's Introduction




一个异步非阻塞的网络协议解析包

项目简介

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>

一、创建TCP服务(默认使用http解码器)

创建Handler

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);

二、创建WebSocket

只需要在创建http服务的时候加一个handler即可

Magician.createTCPServer()
                    .handler("/", new DemoHandler())
                    .webSocketHandler("/websocket", new DemoSocketHandler())
                    .bind(8080);

三、创建UDP服务

Magician.createUdpServer()
                .handler(outputStream -> {
                    // outputStream 是ByteArrayOutputStream类型的
                    // 它是客户端发过来的数据,自行解析即可
                }).bind(8088);

除了这种写法,也可以单独创建handler,在这里add进去

TFB测试结果(第二轮,持续优化中)

image

TFB地址

开发资源

magician's People

Contributors

yuyenews avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.