Code Monkey home page Code Monkey logo

websocket-api's Introduction

websocket-client

Maven Central License

Getting it

websocket-api requires Java 11 or + to run.

With Maven Central repositories (stable releases only):

    <dependency>
        <groupId>org.zhongweixian</groupId>
        <artifactId>websocket-api</artifactId>
        <version>2.1.4</version>
    </dependency>

start server

 WebSocketServer webSocketServer = new WebSocketServer(8190, 60, "ws", new ConnectionListener() {
            @Override
            public void connect(Channel channel) throws Exception {
                logger.info("channel {} is connect", channel.id());
            }

            @Override
            public void onClose(Channel channel, int closeCode, String reason) {

            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onFail(int status, String reason) {
                logger.info("status:{} , reason:{}", status, reason);
            }

            @Override
            public void onMessage(Channel channel, String text) throws Exception {
                logger.info("channel {} , receive message:{}", channel.id(), text);
            }

            @Override
            public void onMessage(Channel channel, ByteBuf byteBuf) throws Exception {
                logger.info("");
            }
        });
        webSocketServer.start();

websocket client(不重连)

        /**
         * 启动ws客户端
         */
        WebSocketClient client = new WebSocketClient("ws://192.168.181.178:8190/ws");


        for (int i = 0; i < 1000; i++) {
            client.connection(new ConnectionListener() {
                @Override
                public void onClose(Channel channel, int closeCode, String reason) {
                    logger.warn("channelId:{} , closeCode:{} , reason:{}", channel.id(), closeCode, reason);
                }

                @Override
                public void onError(Throwable throwable) {
                    logger.warn("onError:{}", throwable.getMessage());
                }

                @Override
                public void onFail(int status, String reason) {

                }

                @Override
                public void onMessage(Channel channel, String text) throws Exception {
                    logger.info("client:{} onMessage:{}", channel.id(), text);
                }

                @Override
                public void onMessage(Channel channel, ByteBuf byteBuf) throws Exception {
                    logger.info("client:{} onMessage:{}", channel.id(), byteBuf.toString(Charset.defaultCharset()));
                }

                @Override
                public void connect(Channel channel) throws Exception {
                    logger.info("client:{} connect", channel.id());
                }
            });
            Thread.sleep(10);
        }

websocket client (支持重连)

    public static void main(String[] args) {
        try {
            String login = "{\"cmd\":\"connect\",\"agentId\":\"1000\",\"sip\":\"\",\"companyId\":129,\"businessIds\":[831,823],\"operatorType\":1}";
            WsClient client = new WsClient("ws://192.168.183.145:8190/ws", login, new ConnectionListener() {
                @Override
                public void onClose(Channel channel, int closeCode, String reason) {

                }

                @Override
                public void onError(Throwable throwable) {

                }

                @Override
                public void onFail(int status, String reason) {

                }

                @Override
                public void onMessage(Channel channel, String text) throws Exception {
                    System.out.println(text);
                }

                @Override
                public void onMessage(Channel channel, ByteBuf byteBuf) throws Exception {

                }

                @Override
                public void connect(Channel channel) throws Exception {
                    System.out.println("连接成功 ");
                }
            });
            new Thread(client).start();

            System.out.println(" start ok");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

websocket-api's People

Contributors

caoliang1918 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.