我想通过TCP发送文本消息.满容易.我想用akka这样做.我读了这篇关于akka IO的文章:http://doc.akka.io/docs/akka/snapshot/scala/io-tcp.html
本文介绍了TCP客户端的简单实现,但我不清楚如何使用此客户端.
构造函数采用InetSocketAddress和ActorRef.InetSocketAddress是有道理的(我假设这是目的地),但是ActorRef是什么?这是我第一次使用akka,但据我所知,ActorRef是另一个actor的引用.由于我的TCP客户端是一个actor,我希望这个TCP actor与TCP服务器通信,而不是与另一个actor通信,为什么我会给它一个actor ref?
伴侣对象中的道具功能是什么?
一旦实例化,我将如何使用此actor发送TCP消息?我应该以ByteString的形式向我发送一条包含我要发送的数据的消息吗?
4.什么是连接/区别
case Received(data) =>
listener ! data
Run Code Online (Sandbox Code Playgroud)
和
case data: ByteString =>
connection ! Write(data)
Run Code Online (Sandbox Code Playgroud) 使用 Akka-IO TCP,在 Actor 中建立连接的过程如下:
class MyActor(remote: InetSocketAddress) extends Actor {
IO(Tcp) ! Connect(remote) //this is the first step, remote is the address to connect to
def receive = {
case CommandFailed(_: Connect) => context stop self // failed to connect
case Connected(remote, local) =>
val connection = sender()
connection ! Register(self)
// do cool things...
}
}
Run Code Online (Sandbox Code Playgroud)
您Connect向 发送消息IO(Tcp)并期望收到CommandFailed或Connected消息。
现在,我的目标是创建一个包装 TCP 连接的 actor,但我希望我的 actor 仅在建立连接后才开始接受消息 - 否则,在等待消息时,Connected它将开始接受查询,但没有人可以发送他们到。
我尝试过的:
class …Run Code Online (Sandbox Code Playgroud) 在学习如何使用akka I/OI时,我试图在akka i/o上实现一个简单的协议,并在此处遵循文档.
但是在我的gradle文件中,我使用版本2.3.9,如下所示
dependencies {
compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.7'
compile group: 'com.typesafe.akka', name: 'akka-actor_2.11', version: '2.3.9'
compile group: 'com.typesafe.akka', name: 'akka-contrib_2.11', version: '2.3.9'
compile group: 'org.scala-lang', name: 'scala-library', version: '2.11.5'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
Run Code Online (Sandbox Code Playgroud)
导入一些特定于管道的东西
import akka.io.SymmetricPipelineStage;
import akka.io.PipelineContext;
import akka.io.SymmetricPipePair;
Run Code Online (Sandbox Code Playgroud)
生成无法解决符号错误.
因此我的问题.