scala - 尝试通过 TCP 连接传递字节时出错
问题描述
我需要通过 tcp 连接传递像“0x02”这样的字节并在服务器上处理它。客户端代码在这里:
package protocol
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Source, Tcp}
import akka.util.ByteString
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Source, Tcp}
import akka.util.ByteString
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
class UpperServiceClient(ip: String, port: Int) {
def run = {
implicit val system = ActorSystem("ClientSys")
implicit val materializer = ActorMaterializer()
val a1 = Array("0x02", "0x02").toSeq
val testInput = a1.map(ByteString(_))
val result: Future[ByteString] = Source(testInput).via(Tcp().outgoingConnection(ip, port)).
runFold(ByteString.empty) { (acc, in) => acc ++ in }
val res: ByteString = Await.result(result, 10.seconds)
}
}
但 IDEA 向我显示错误:
类型不匹配,预期:Iterable[NotInferedT],实际:Seq[ByteString]
我应该怎么做才能将“0x02”作为整个字节传递?
解决方案
您正在使用的Source
工厂方法需要一个不可变的Iterable
. 默认情况下,调用.toSeq
数组会返回一个 mutable Seq
。解决此问题的一种方法是改为调用.toList
:
val a1 = Array("0x02", "0x02").toList
推荐阅读
- python - 将多层次数据帧转换为非层次数据
- php - PHP - 是否可以在 __destruct() 方法中使用自定义异常处理程序 (set_exception_handler)?
- javascript - 抓取移入列表框 onClick 的项目
- python - 我在 python 的 statsmodels 中找不到 holtwinters
- vue.js - Vue 错误:客户端渲染的虚拟 DOM 树与服务器渲染的内容不匹配
- json - Ajax 数据表排序
- c++ - 如果消毒剂没有显示任何内容,我该如何调试内存崩溃?
- python - python tqdm 多个进度条
- excel - 从下往上遍历列中的每个单元格,但没有进行任何更改 VBA
- android - 手势捕获的 Flutter 异常。在 showModalBottomSheet 中找不到 MediaQuery 小部件