Giter VIP home page Giter VIP logo

Comments (5)

azzahrah avatar azzahrah commented on May 28, 2024

My websocket server just working, but after client send always close as soon as after receive message, even if i have change code to

    @Provides 
    AsyncServlet servlet() {
      return RoutingServlet.create()
              .mapWebSocket("/", (webSocket) -> webSocket.readMessage()
               .whenResult((message) -> System.out.println("Received:" + message.getText()))
               .then(() -> webSocket.writeMessage(WebSocket.Message.text("Halo Bosss"))));

from activej.

eduard-vasinskyi avatar eduard-vasinskyi commented on May 28, 2024

Hello, @azzahrah
Did you manage to run an example?
Actually, server should not be closed after serving a request from the client. In Ping - Pong example it is client the one who shuts down after sending a request and receiving a response. Server should be listening for connections until explicit shutdown.

from activej.

azzahrah avatar azzahrah commented on May 28, 2024

@eduard-vasinskyi,
yes, i have run an example, and working fine, below is my modified example that working for me:

public final class WebSocketPongServerExample extends HttpServerLauncher {

    //[START EXAMPLE]
    @Provides
    AsyncServlet servlet() {
        return RoutingServlet.create()
                .mapWebSocket("/websocket", webSocket -> readMessage(webSocket))
                .mapWebSocket("/", webSocket -> readMessage(webSocket));
    }
    //[END EXAMPLE]

    void readMessage(WebSocket websocket) {
        websocket.readMessage()
                .whenResult(message -> System.out.println("Received:" + message.getText()))
                .whenComplete(() -> {
                    if (!websocket.isClosed()) {
                        readMessage(websocket);
                    } else {
                        websocket.close();
                    }
                });
    }

    public static void main(String[] args) throws Exception {
        WebSocketPongServerExample launcher = new WebSocketPongServerExample();
        launcher.launch(args);
    }
}

the above example force application to re read again after receive data, i don't know is this right way to wait incoming and read data...

from activej.

eduard-vasinskyi avatar eduard-vasinskyi commented on May 28, 2024

This is one way to create a WebSocket that reads data infinitely.

Although, you should not close a WebSocket that has already been closed, as it will do nothing. And you should check for message type (TEXT or BINARY) before calling message.getText() (unless you are sure that the client will only send TEXT WebSocket frames).
You can use our CSP (Communicating sequential process) API to rewrite readMessage() to make it more concise:

void readMessage(WebSocket websocket) {
     websocket.messageReadChannel()
//          .filter(message -> message.getType() == MessageType.TEXT)
            .map(Message::getText)
            .streamTo(ChannelConsumer.ofConsumer(text -> System.out.println("Received:" + text)));
  }

from activej.

azzahrah avatar azzahrah commented on May 28, 2024

@eduard-vasinskyi thanks for your help... you are really save my time

from activej.

Related Issues (20)

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.