gpt4 book ai didi

scala - 为什么使用 Akka TCP 连接会在 1 分钟后自动关闭?

转载 作者:行者123 更新时间:2023-12-01 03:45:02 28 4
gpt4 key购买 nike

我正在使用 Scala 和 Akka Actor 编写盈透证券 API。

我有一个客户端角色,它连接到服务器并与 IO 管理器通信以发送请求并接收来自 TWS 的响应。连接工作正常,我能够发送请求并获得响应。

然后我会在 1 分钟后自动收到来自 IO 管理器的 PeerClosed 消息。我希望连接保持打开状态,除非我明确关闭它。我试图设置 keepOpenOnPeerClosed = true但它什么也没改变。

这是 Actor :

class Client(remote: InetSocketAddress, clientId: Int, extraAuth: Boolean, onConnected: Session => Unit, listener: EWrapper) extends Actor {
final val ClientVersion: Int = 63
final val ServerVersion: Int = 38
final val MinServerVerLinking: Int = 70

import Tcp._
import context.system

IO(Tcp) ! Connect(remote)

def receive = {

case CommandFailed(_: Connect) =>
print("connect failed")
context stop self

case c@Connected(remote, local) => {

val connection = sender()
connection ! Register(self, keepOpenOnPeerClosed = true)
context become connected(connection,1)
val clientVersionBytes = ByteString.fromArray(String.valueOf(ClientVersion).getBytes() ++ Array[Byte](0.toByte))
println("Sending Client Version " + clientVersionBytes)
sender() ! Write(clientVersionBytes)
}
}
def connected(connection: ActorRef, serverVersion: Int): Receive = {
case request: Request =>
print("Send request " + request)
connection ! Write(ByteString(request.toBytes(serverVersion)))
case CommandFailed(w: Write) =>
connection ! Close
print("write failed")
case Received(data) => {
println(data)
implicit val is = new DataInputStream(new ByteArrayInputStream(data.toArray))
EventDispatcher.consumers.get(readInt()) match {
case Some(consumer) => {
consumer.consume(listener, serverVersion)
}
case None => {
listener.error(EClientErrors.NoValidId, EClientErrors.UnknownId.code, EClientErrors.UnknownId.msg)
}
}
}
case _ : ConnectionClosed => context stop self
}

如果我使用 IBJts API(使用标准 Java 套接字)进行连接,则我没有相同的行为

最佳答案

您是否尝试过使用保持事件选项?
sender ! Tcp.SO.KeepAlive(on = true)

关于scala - 为什么使用 Akka TCP 连接会在 1 分钟后自动关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27710950/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com