Skip to content

Instantly share code, notes, and snippets.

@odadoda
Created November 8, 2013 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odadoda/7370955 to your computer and use it in GitHub Desktop.
Save odadoda/7370955 to your computer and use it in GitHub Desktop.
Taking care of websockets and in and out messages
package models;
import play.mvc.*;
import play.libs.*;
import play.libs.F.*;
import java.util.*;
public class SimpleChat{
// collect all websockets here
private static List<WebSocket.Out<String>> connections = new ArrayList<WebSocket.Out<String>>();
public static void start(WebSocket.In<String> in, WebSocket.Out<String> out){
connections.add(out);
in.onMessage(new Callback<String>(){
public void invoke(String event){
SimpleChat.notifyAll(event);
}
});
in.onClose(new Callback0(){
public void invoke(){
SimpleChat.notifyAll("A connection closed");
}
});
}
// Iterate connection list and write incoming message
public static void notifyAll(String message){
for (WebSocket.Out<String> out : connections) {
out.write(message);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment