scalawebsocketvert.xvertx-httpclient

Vertx Scala: how to setup websocket connection and send a message


I am using Vert.x Scala library (version 3.8.0) and I can't figure out how to setup a websocket connection and send a message. Following the doc instructions for Vert.x 3.5.4 this should be working:

import io.vertx.scala.core.Vertx
import io.vertx.scala.core.http.HttpClientOptions

object WsClient extends App {
  val vertx = Vertx.vertx
  val client = vertx.createHttpClient

  client.websocket(8080, "localhost", "/api", (ws: io.vertx.scala.core.http.WebSocket) => {
    println("connected")
    val message = "hello"
    ws.writeTextMessage(message)
  })
}

However the following error is thrown at compile time:

Error:(9, 10) overloaded method value websocket with alternatives:
  (requestURI: String,headers: io.vertx.scala.core.MultiMap,version: io.vertx.core.http.WebsocketVersion,wsConnect: io.vertx.core.Handler[io.vertx.scala.core.http.WebSocket])io.vertx.scala.core.http.HttpClient <and>
  (requestURI: String,headers: io.vertx.scala.core.MultiMap,wsConnect: io.vertx.core.Handler[io.vertx.scala.core.http.WebSocket],failureHandler: io.vertx.core.Handler[Throwable])io.vertx.scala.core.http.HttpClient <and>
  (options: io.vertx.scala.core.http.RequestOptions,headers: io.vertx.scala.core.MultiMap,version: io.vertx.core.http.WebsocketVersion,wsConnect: io.vertx.core.Handler[io.vertx.scala.core.http.WebSocket])io.vertx.scala.core.http.HttpClient <and>
  (host: String,requestURI: String,headers: io.vertx.scala.core.MultiMap,wsConnect: io.vertx.core.Handler[io.vertx.scala.core.http.WebSocket])io.vertx.scala.core.http.HttpClient <and>
  (options: io.vertx.scala.core.http.RequestOptions,headers: io.vertx.scala.core.MultiMap,wsConnect: io.vertx.core.Handler[io.vertx.scala.core.http.WebSocket],failureHandler: io.vertx.core.Handler[Throwable])io.vertx.scala.core.http.HttpClient <and>
  (host: String,requestURI: String,wsConnect: io.vertx.core.Handler[io.vertx.scala.core.http.WebSocket],failureHandler: io.vertx.core.Handler[Throwable])io.vertx.scala.core.http.HttpClient <and>
  (port: Int,host: String,requestURI: String,wsConnect: io.vertx.core.Handler[io.vertx.scala.core.http.WebSocket])io.vertx.scala.core.http.HttpClient
 cannot be applied to (Int, String, String, io.vertx.scala.core.http.WebSocket => io.vertx.scala.core.http.WebSocket)
  client.websocket(8080, "localhost", "/api", (ws: io.vertx.scala.core.http.WebSocket) => {

I have also tried to make the language infer the handler type, but with no success. What am I doing wrong?


Solution

  • Looking at the java doc I noticed an API change, the method client.websocket(...) is deprecated, the one that should be used is client.webSocket(...).

    Here's a corrected version of the code above, it should be working from Vert.x 3.6.x version:

    import io.vertx.core.AsyncResult
    import io.vertx.scala.core.http.WebSocket
    
    client.webSocket(8080, "localhost", "/api", (res: AsyncResult[WebSocket]) => {
      if (res.succeeded) {
        println("connected")
        val ws = res.result()
        val message = "hello"
        ws.writeTextMessage(message)
      } else {
        println(res.cause)
      }
    })