我正在写一个基于旧学校文本的telnet服务器,现在是Scala的一个美化聊天室,基于Akka actor的IO。发生的事情是客户端客户端将开始输入内容然后事件将发生,当它被写入时,它会清除已经输入的任何内容。在下面的示例中,Tom已经开始输入“say你好吗?”但弗雷德在他只输入了“说如何”之后就到了,这个输入就被消灭了:
Tom > say How ar
Fred has arrived.
Tom >
有没有办法让telnet重新显示它还没有刷新的输出缓冲区呢?
这是服务器:
class TcpServer(port: Int) extends Actor {
import TcpServer._
import context.system
val log = Logging(system, this)
var connectionNum: Int = 1
log.info(STARTING_SERVER)
IO(Tcp) ! Bind(self, new InetSocketAddress("0.0.0.0", port))
def receive = {
case b @ Bound(localAddress) =>
log.info(PORT_BOUND, localAddress)
case c @ Connected(remote, local) =>
log.info(CONNECTION_ACCEPTED)
context.actorOf(ConnectionHandler.connectionProps(sender()), s"conn$connectionNum")
connectionNum += 1
case CommandFailed(_: Bind) =>
log.error(BINDING_FAILED)
context stop self
}
}
这是ConnectionHandler,它的伴随对象,以及它使用的消息案例类:
class ConnectionHandler(connection: ActorRef) extends Actor {
import context._
val log = Logging(context.system, this)
connection ! Register(self)
var delegate = actorOf(Props[LoginHandler], "login")
watch(delegate)
def receive = {
case Received(data) =>
val dataString = data.utf8String.trim
log.info("Received data from connection: {}", dataString)
delegate ! Input(dataString)
case Output(content) =>
connection ! Write(ByteString(content))
case LoggedIn(user) =>
unwatch(delegate)
delegate ! PoisonPill
delegate = actorOf(UserHandler.connectionProps(user), user.name.toLowerCase)
watch(delegate)
case Terminated =>
log.warning("User delegate died unexpectedly.")
connection ! ConfirmedClose
case CloseConnection(message) =>
connection ! Write(ByteString(message + "\n"))
connection ! ConfirmedClose
log.info("User quit.")
case ConfirmedClosed =>
log.info("Connection closed.")
stop(self)
case PeerClosed =>
log.info("Connection closed by client.")
stop(self)
}
}
object ConnectionHandler {
def connectionProps(connection: ActorRef): Props = Props(new ConnectionHandler(connection))
}
case class Input(input: String)
case class Output(output: String)
case class LoggedIn(user: User)
case class CloseConnection(message: String)
答案 0 :(得分:0)
好的,在最终正确地表达我的谷歌查询之后,我发现了我需要的东西: Force telnet client into character mode
基本的解决方案是我在一个时间模式下强迫客户进入角色,并回复我关心的角色。这样做的好处是,现在我可以完成标签完成,命令历史记录,并且不会显示密码。
以下是相关的代码段:
val controlString = ByteString('\u00ff','\u00fb','\u0001','\u00ff','\u00fb','\u0003','\u00ff','\u00fc','\u0022')
connection ! Write(controlString)