3
我正在使用Scala和Akka actors編寫Interactive Broker API。爲什麼使用Akka TCP 1分鐘後自動關閉連接?
我有一個客戶端角色,連接到服務器並與IO管理器通信以發送請求並接收來自TWS的響應。連接工作正常,我可以發送請求並獲得響應。
然後我在1分鐘後自動從IO管理器接收到PeerClosed消息。我希望連接保持打開,除非我明確關閉它。我試圖設置但它沒有改變。
這裏是演員:
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 [email protected](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 Socket技術)
同樣的情況與我使用基於阿卡-的WebSocket庫,大概是出於同樣的原因。我感到困惑。 – rightfold
我今天重試了,我沒有再次超時了。但我仍然不明白原因。 – Fede
你在做什麼系統(mac,Linux,windows)?這可能是套接字超時。 –