Sending output to websocket in game 2.0

I am trying to figure out a way to get WebSocket.Out to run some data when calling WebSocket.In onMessage.

For instance:

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 greatly appreciated.

+3
source share
1 answer

Please check the app code for the websocket-chat example . This will give you a copy model for managing websites.

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 to channelwhich is WebSocket.Out<JsonNode>.

+2
source

All Articles