Created
April 5, 2012 14:55
-
-
Save ndeverge/2311723 to your computer and use it in GitHub Desktop.
MapTracker : server side websocket
This file contains hidden or 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 class MapAnime extends UntypedActor { | |
static ActorRef actor = Akka.system().actorOf(new Props(MapAnime.class)); | |
Map<String, WebSocket.Out<JsonNode>> registrered = new HashMap<String, WebSocket.Out<JsonNode>>(); | |
/** | |
* | |
* @param id | |
* @param in | |
* @param out | |
* @throws Exception | |
*/ | |
public static void register(final String id, | |
final WebSocket.In<JsonNode> in, final WebSocket.Out<JsonNode> out) | |
throws Exception { | |
actor.tell(new RegistrationMessage(id, out)); | |
// For each event received on the socket, | |
in.onMessage(new Callback<JsonNode>() { | |
@Override | |
public void invoke(JsonNode event) { | |
// nothing to do | |
} | |
}); | |
// When the socket is closed. | |
in.onClose(new Callback0() { | |
@Override | |
public void invoke() { | |
actor.tell(new UnregistrationMessage(id)); | |
} | |
}); | |
} | |
public static void moveTo(float longitude, float latitude) { | |
actor.tell(new MoveMessage(longitude, latitude)); | |
} | |
@Override | |
public void onReceive(Object message) throws Exception { | |
if (message instanceof RegistrationMessage) { | |
// Received a Join message | |
RegistrationMessage registration = (RegistrationMessage) message; | |
Logger.info("Registering " + registration.id + "..."); | |
registrered.put(registration.id, registration.channel); | |
} else if (message instanceof MoveMessage) { | |
// Received a Move message | |
MoveMessage move = (MoveMessage) message; | |
for (WebSocket.Out<JsonNode> channel : registrered.values()) { | |
ObjectNode event = Json.newObject(); | |
event.put("longitude", move.longitude); | |
event.put("latitude", move.latitude); | |
channel.write(event); | |
} | |
} else if (message instanceof UnregistrationMessage) { | |
// Received a Unregistration message | |
UnregistrationMessage quit = (UnregistrationMessage) message; | |
Logger.info("Unregistering " + quit.id + "..."); | |
registrered.remove(quit.id); | |
} else { | |
unhandled(message); | |
} | |
} | |
public static class RegistrationMessage { | |
public String id; | |
public WebSocket.Out<JsonNode> channel; | |
public RegistrationMessage(String id, WebSocket.Out<JsonNode> channel) { | |
super(); | |
this.id = id; | |
this.channel = channel; | |
} | |
} | |
public static class UnregistrationMessage { | |
public String id; | |
public UnregistrationMessage(String id) { | |
super(); | |
this.id = id; | |
} | |
} | |
public static class MoveMessage { | |
public float longitude; | |
public float latitude; | |
public MoveMessage(float longitude, float latitude) { | |
this.longitude = longitude; | |
this.latitude = latitude; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment