【发布时间】:2016-07-18 20:38:30
【问题描述】:
如果这是一个愚蠢的问题,我很抱歉,但如果不自己设置某种 ASCII 码 -> 字符映射器,我真的无法弄清楚,我认为这不是正确的方法。
所以目前我正在使用 Scala 和 Akka 制作一个“聊天应用程序”,其中我使用单独的客户端和服务器实体。客户端连接到服务器,发送一条消息,服务器用它做一些事情。
我可以发送消息,但现在我只能在服务器端阅读消息。每当我收到一条消息时,我都会得到一个 ByteString,其中包含消息中字符的 ASCII 值。如何将此 ByteString 转换为实际的字符串?
相关代码(服务器端):
package chatapp.server
import java.net.InetSocketAddress
import akka.actor.{Actor, ActorSystem}
import akka.io.Tcp._
import akka.io.{IO, Tcp}
/**
* Created by Niels Bokmans on 30-3-2016.
*/
class ServerActor(actorSystem: ActorSystem) extends Actor {
val Port = 18573
val Server = "localhost"
IO(Tcp)(actorSystem) ! Bind(self, new InetSocketAddress("localhost", Port))
def receive: Receive = {
case CommandFailed(_: Bind) =>
println("Failed to start listening on " + Server + ":" + Port)
context stop self
actorSystem.terminate()
case Bound(localAddress: InetSocketAddress) =>
println("Started listening on " + localAddress)
case Connected(remote, local) =>
println("New connection!")
sender ! Register(self)
case Received(data) =>
println(data)
}
}
【问题讨论】: