WebSockets production ready server in Java?

后端 未结 6 661
情书的邮戳
情书的邮戳 2020-11-28 04:57

EDIT: removed reference to C# as the only accepted answer is about Java. If someone needs information about websocket server implementation in C#, ask a new

6条回答
  •  臣服心动
    2020-11-28 05:13

    The Vert.x option is also worth considering.

    Creating a ws server can be as simple as

    vertx.websocketHandler(new Handler() {
        public void handle(ServerWebSocket ws) {
            // A WebSocket has connected!
        }
    }).listen(8080);
    

    or

    vertx.createHttpServer().websocketHandler(new Handler() {
            @Override
            public void handle(final ServerWebSocket ws) {
                logger.info("ws connection established with " + ws.remoteAddress());
                ws.dataHandler(new Handler() {
                    @Override
                    public void handle(Buffer data) {
                        JsonObject item = new JsonObject(data.toString());
                        logger.info("data in -> " + item.encodePrettily());
                           // if you want to write something back in response to the client
                        //ws.writeTextFrame(...);
                }
                });
            }
        }).listen(port, new Handler>() {
            @Override
            public void handle(AsyncResult event) {
                logger.info("ws server is up and listening on port " + port);
            }
        });
    

    For more details look here http://vertx.io/docs/vertx-core/java/#_websockets

    So one can write his own WebSocket server with Vert.x, package it as FatJar, and run it on its own.

    Or you can embed Vert.x env. in your app, and deploy your verticle (that implements the ws server) programmatically.


    Another alternative is JBoss's web server Undertow. Which is easily embeddable in applications.

    Add these dependencies:

    
      io.undertow
      undertow-servlet
      ${version.io.undertow}
    
    
    
      io.undertow
      undertow-websockets-jsr
      ${version.io.undertow}
    
    

    And here's a sample ws server:

    Undertow server = Undertow.builder()
                .addHttpListener(8080, "localhost")
                .setHandler(path()
                        .addPrefixPath("/myapp", websocket(new WebSocketConnectionCallback() {
    
                            @Override
                            public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) {
                                channel.getReceiveSetter().set(new AbstractReceiveListener() {
    
                                    @Override
                                    protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) {
                                        final String messageData = message.getData();
                                        for (WebSocketChannel session : channel.getPeerConnections()) {
                                            WebSockets.sendText(messageData, session, null);
                                        }
                                    }
                                });
                                channel.resumeReceives();
                            }
                        }))
                .build();
    
        server.start();
    

提交回复
热议问题