如何使用vertx在宁静的处理程序中获取websocket实例?

时间:2019-01-10 16:10:37

标签: websocket vert.x

我是vert.x的新手。这是我要完成的事情:

首先,我有一个端点处理客户端应用程序的紧急请求。

router.post("/clientrequest").handler(routingContext -> {

  // This handler will be called for every request
  HttpServerResponse response = routingContext.response();
  HttpServerRequest request = routingContext.request();

  request.handler(buffer->{
     // This handler will be called for every chunk of my post request with multi-parts
     //

     //client is a HttpClient = vertx.createHttpClient()
     client.websocket("/anotherWebsockReq",ws->{
     //here I get ws as my websocket client instance ws.writeFrame......
        });       
});

我的问题是如何获取一个websocket实例来发送剩余的缓冲区,而无需在请求中创建新的缓冲区。处理程序循环。

1 个答案:

答案 0 :(得分:0)

在处理缓冲区之前获取websocket:

router.post("/clientrequest").handler(routingContext -> {

  // This handler will be called for every request
  HttpServerResponse response = routingContext.response();
  HttpServerRequest request = routingContext.request();

  request.pause()

  client.websocket("/anotherWebsockReq",ws->{
    request.handler(buffer->{
      // write your frames here
    }    
    request.resume();
  }

});

请注意对pauseresume的呼叫。这是必需的,因为否则我的数据将会丢失。