斯卡拉2.10,阿卡骆驼TCP套接字通信
我在找一些容易和简单的例子如何连接,并通过TCP套接字的相互作用(双向的)。换句话说,的如何写的斯卡拉2.10的应用程序(使用的阿卡驼或网状的库)使用TCP进程通信(插座) 。
I'm looking for some easy and short example how to connect and make interaction (two-ways) with tcp socket. In other words, how to write a scala 2.10 application (using akka-camel or netty library) to communicate with a tcp process (socket).
我发现在互联网上大量的文献,但一切都是旧的(寻找斯卡拉2.10)和/或去precated。
I found a lot of literature on Internet, but everything was old (looking for scala 2.10) and/or deprecated.
在此先感谢!
嗯,我一直在寻找这样的事情:
hmm I was looking for something like this:
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 。但如果你是新手,你需要阅读这一切,并多次以建立非常简单的客户端 - 服务器应用程序。我觉得某种动机榜样会更受欢迎。
I know it is everything written and well-described in http://doc.akka.io/docs/akka/2.1.0/scala/camel.html. But if you are novice you need to read it all and several times in order to build very simple client-server application. I think some kind of "motivation example" would be more than welcome.