Skip to content

Instantly share code, notes, and snippets.

@gamlerhart
Created November 22, 2011 21:55
Show Gist options
  • Select an option

  • Save gamlerhart/1387130 to your computer and use it in GitHub Desktop.

Select an option

Save gamlerhart/1387130 to your computer and use it in GitHub Desktop.
akka-mobile-first-code-tour
class AkkaDroidActivity extends Activity with ActivityActor {
override def onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
actorOf[DoSomeWork].start()
}
protected def receive = {
case WorkDone(happend) => {
val inputTextBox = findViewById(R.id.enterMsgBox).asInstanceOf[EditText]
inputTextBox.setText("happend")
}
}
}
case class WorkDone(result:String)
class DoSomeWork extends Actor{
protected def receive = {
case "Start" =>{
val result = workForAWhile()
self.reply(result)
}
}
private def workForAWhile() :String ={
// work work
"work-done"
}
}
akka {
event-handlers = ["akka.mobile.android.LogcatLogger"]
event-handler-level = "DEBUG" # Options: ERROR, WARNING, INFO, DEBUG
mobile{
client{
host = "our.server.host"
port = 2552
}
}
}
class ChatClientActor extends Actor{
protected def receive = {
case SentToServer(msg) =>{
val chatService = MyApplication.remote.actorFor("chat-service")
chatService ! msg
}
case MessageFromServer(msg) =>{
// well, show the message or do what is needed
}
}
}
class TalksToMobileDevices extends Actor with ServiceActor{
protected def receive = {
case msg =>{
self.reply("Answer for: "+msg)
val clientId = this.clientId;
if(clientId.isDefined){
// store it in database. So that you later can talk to this client
}
}
}
}
remote.register("notifications", Actor.actorOf(new Actor() {
protected def receive = {
case s: String => {
// react to the notification
}
}
})
val clientId = // clientId. Like from the database
val serviceOnPhone = chatServer.actorOf(clientId, "notifications")
serviceOnPhone ! "Hi Phone"
// Registred in the AndroidManifest as our application object
class MyApplication extends android.app.Application {
lazy val remote = MobileRemoteClient.createClient(
AndroidDevice(this))
}
val chatServer = NettyRemoteServer.start(2552);
chatServer.register("chat-service", Actor.actorOf[ChatServerActor])
class ChatServerActor extends Actor{
protected def receive = {
case msg =>{
self.reply("Answer for: "+msg)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment