Created
August 8, 2014 10:22
-
-
Save wbspry/3a9bd195267df198dafe to your computer and use it in GitHub Desktop.
Playframework付属のサンプル'websocket-chat'を見てみる(3/4) ref: http://qiita.com/yyyske/items/87c49e925237a208373e
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static WebSocket<JsonNode> chat(final String username) { | |
return new WebSocket<JsonNode>() { | |
// Called when the Websocket Handshake is done. | |
public void onReady(WebSocket.In<JsonNode> in, WebSocket.Out<JsonNode> out){ | |
// Join the chat room. | |
try { | |
ChatRoom.join(username, in, out); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
} | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if("OK".equals(result)) { | |
// For each event received on the socket, | |
in.onMessage(new Callback<JsonNode>() { | |
public void invoke(JsonNode event) { | |
// Send a Talk message to the room. | |
defaultRoom.tell(new Talk(username, event.get("text").asText()), null); | |
} | |
}); | |
// When the socket is closed. | |
in.onClose(new Callback0() { | |
public void invoke() { | |
// Send a Quit message to the room. | |
defaultRoom.tell(new Quit(username), null); | |
} | |
}); | |
} else { | |
// Cannot connect, create a Json error. | |
ObjectNode error = Json.newObject(); | |
error.put("error", result); | |
// Send the error to the socket. | |
out.write(error); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def result[T](awaitable: Awaitable[T], atMost: Duration): T | |
Await and return the result (of type T) of an Awaitable. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ask (actor: ActorRef, message: Any, timeoutMillis: Long): Future[AnyRef] | |
Java API for `akka.pattern.ask`: Sends a message asynchronously and returns a Future holding the eventual reply message; this means that the target actor needs to send the result to the sender reference provided. The Future will be completed with an akka.actor.AskTimeoutException after the given timeout has expired; this is independent from any timeout applied while awaiting a result for this future (i.e. in Await.result(..., timeout)). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static akka.actor.ActorSystem system() | |
Retrieve the application Akka Actor system. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def actorOf (props: Props): ActorRef | |
Create new actor as child of this context and give it an automatically generated name (currently similar to base64-encoded integer count, reversed and with “$” prepended, may change in the future). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static Props create(java.lang.Class<?> clazz, java.lang.Object... args) | |
Java API: create a Props given a class and its constructor arguments. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment