Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vertx Scala:如何设置websocket连接并发送消息_Scala_Websocket_Vert.x_Vertx Httpclient - Fatal编程技术网

Vertx Scala:如何设置websocket连接并发送消息

Vertx Scala:如何设置websocket连接并发送消息,scala,websocket,vert.x,vertx-httpclient,Scala,Websocket,Vert.x,Vertx Httpclient,我正在使用Vert.x Scala库(3.8.0版),我不知道如何设置websocket连接和发送消息。对于Vert.x 3.5.4,应遵循以下步骤: import io.vertx.scala.core.Vertx import io.vertx.scala.core.http.HttpClientOptions object WsClient extends App { val vertx = Vertx.vertx val client = vertx.createHttpCli

我正在使用Vert.x Scala库(3.8.0版),我不知道如何设置websocket连接和发送消息。对于Vert.x 3.5.4,应遵循以下步骤:

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) => {
错误:(9,10)重载了方法值websocket和可选项:
(requestURI:String,头:io.vertx.scala.core.MultiMap,版本:io.vertx.core.http.WebsocketVersion,wsConnect:io.vertx.core.Handler[io.vertx.scala.core.http.WebSocket])io.vertx.scala.core.http.HttpClient
(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
(选项:io.vertx.scala.core.http.RequestOptions,标题:io.vertx.scala.core.MultiMap,版本:io.vertx.core.http.WebsocketVersion,wsConnect:io.vertx.core.Handler[io.vertx.scala.core.http.WebSocket])io.vertx.scala.core.http.HttpClient
(主机:String,requestURI:String,头:io.vertx.scala.core.MultiMap,wsConnect:io.vertx.core.Handler[io.vertx.scala.core.http.WebSocket])io.vertx.scala.core.http.HttpClient
(选项:io.vertx.scala.core.http.RequestOptions,标题: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
(主机: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
(port:Int,host:String,requestURI:String,wsConnect:io.vertx.core.Handler[io.vertx.scala.core.http.WebSocket])io.vertx.scala.core.http.HttpClient
无法应用于(Int、String、String、io.vertx.scala.core.http.WebSocket=>io.vertx.scala.core.http.WebSocket)
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)
  }
})

查看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)
  }
})