Reputation: 4021
I am trying to figure out a way to get WebSocket.Out to fire out some data when an WebSocket.In's onMessage is called.
For example:
public class Application extends Controller {
public static Result index() {
return ok(index.render("Your new application is ready."));
}
public static WebSocket<String> sockHandler() {
return new WebSocket<String>() {
public void onReady(WebSocket.In<String> in, WebSocket.Out<String> out) {
in.onMessage(new F.Callback<String>() {
// FIRE SOME DATA BACK OUT TO BROWSER HERE!!!
public void invoke(String event) {
Logger.info(event);
}
});
out.write("I'm contacting you regarding your recent websocket.");
}
};
}
private static void send(WebSocket.Out<String> out, String data){
out.write(data);
}
}
Any help is much appreciated.
Upvotes: 3
Views: 1759
Reputation: 16439
Please check the code of the websocket-chat sample application. It will give you a model to copy for websocket management.
For example, this code:
// Send a Json event to all members
public void notifyAll(String kind, String user, String text) {
for(WebSocket.Out<JsonNode> channel: members.values()) {
ObjectNode event = Json.newObject();
event.put("kind", kind);
event.put("user", user);
event.put("message", text);
ArrayNode m = event.putArray("members");
for(String u: members.keySet()) {
m.add(u);
}
channel.write(event);
}
}
writes some data into channel
, which is a WebSocket.Out<JsonNode>
.
Upvotes: 2