我正在寻找一些简单而简短的示例,如何使用 tcp 套接字进行连接和交互(双向)。换句话说,如何编写scala 2.10应用程序(使用akka-camel或netty库)与tcp进程(套接字)通信。
我在互联网上找到了很多文献,但一切都很旧(寻找 scala 2.10)和/或已弃用。
提前致谢!
最佳答案
嗯,我正在寻找这样的东西:
<强>1。服务器:
import akka.actor._
import akka.camel.{ Consumer, CamelMessage }
class Ser extends Consumer {
def endpointUri = "mina2:tcp://localhost:9002"
def receive = {
case message: CamelMessage => {
//log
println("looging, question:" + message)
sender ! "server response to request: " + message.bodyAs[String] + ", is NO"
}
case _ => println("I got something else!??!!")
}
}
object server extends App {
val system = ActorSystem("some")
val spust = system.actorOf(Props[Ser])
}
<强>2。客户:
import akka.actor._
import akka.camel._
import akka.pattern.ask
import scala.concurrent.duration._
import akka.util.Timeout
import scala.concurrent.Await
class Producer1 extends Actor with Producer {
def endpointUri = "mina2:tcp://localhost:9002"
}
object Client extends App {
implicit val timeout = Timeout(10 seconds)
val system2 = ActorSystem("some-system")
val producer = system2.actorOf(Props[Producer1])
val future = producer.ask("Hello, can I go to cinema?")
val result = Await.result(future, timeout.duration)
println("Is future over?="+future.isCompleted+";;result="+result)
println("Ende!!!")
system2.shutdown
println("system2 ended:"+system2.isTerminated)
我知道 http://doc.akka.io/docs/akka/2.1.0/scala/camel.html 中已详细描述了所有内容。 。但如果您是新手,则需要多次阅读全文才能构建非常简单的客户端-服务器应用程序。我认为某种“动机例子”会更受欢迎。
关于scala 2.10,akka-camel TCP套接字通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14484096/