尝试通过TCP连接传递Byte时出错



我需要通过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会返回一个可变的Seq。解决此问题的一种方法是调用.toList

val a1 = Array("0x02", "0x02").toList

最新更新