Vertx Scala:如何设置 websocket 连接和发送消息
Vertx Scala: how to setup websocket connection and send a message
我正在使用 Vert.x Scala 库(3.8.0 版),但我不知道如何设置 websocket 连接和发送消息。在 Vert.x 3.5.4 的 doc instructions 之后,这应该是有效的:
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)
})
}
然而在编译时抛出以下错误:
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) => {
我也曾尝试让语言推断处理程序类型,但没有成功。我做错了什么?
查看 java 文档我注意到一个 API 更改,方法 client.websocket(...)
已弃用,应该使用的方法是 client.webSocket(...)
.
这是上面代码的更正版本,它应该从 Vert.x 3.6.x 版本开始工作:
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)
}
})
我正在使用 Vert.x Scala 库(3.8.0 版),但我不知道如何设置 websocket 连接和发送消息。在 Vert.x 3.5.4 的 doc instructions 之后,这应该是有效的:
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)
})
}
然而在编译时抛出以下错误:
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) => {
我也曾尝试让语言推断处理程序类型,但没有成功。我做错了什么?
查看 java 文档我注意到一个 API 更改,方法 client.websocket(...)
已弃用,应该使用的方法是 client.webSocket(...)
.
这是上面代码的更正版本,它应该从 Vert.x 3.6.x 版本开始工作:
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)
}
})