Skip to content

Instantly share code, notes, and snippets.

@wbspry
Created August 8, 2014 10:22
Show Gist options
  • Save wbspry/3a9bd195267df198dafe to your computer and use it in GitHub Desktop.
Save wbspry/3a9bd195267df198dafe to your computer and use it in GitHub Desktop.
Playframework付属のサンプル'websocket-chat'を見てみる(3/4) ref: http://qiita.com/yyyske/items/87c49e925237a208373e
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();
}
}
};
}
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);
}
def result[T](awaitable: Awaitable[T], atMost: Duration): T
Await and return the result (of type T) of an Awaitable.
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)).
public static akka.actor.ActorSystem system()
Retrieve the application Akka Actor system.
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).
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