* @see "SocketNamespace.prototype.handlePacket"
* @param sessionId
* @param packet
*/
public void handlePacket(final String sessionId, JsonObject packet) {
final SocketIOSocket socket = socket(sessionId, true);
boolean isDataAck = false;
String ack = packet.getString("ack");
if (ack != null && ack.equals("data")) {
isDataAck = true;
}
String type = packet.getString("type");
switch (type) {
case "connect":
String endpoint = packet.getString("endpoint", "");
if (endpoint.equals("")) {
connect(socket);
} else {
final HandshakeData handshakeData = manager.getHandshaken().get(sessionId);
this.authorize(handshakeData, new AuthorizationCallback() {
@Override
public void handle(Exception e, boolean isAuthorized) {
if (e != null) {
error(socket, e);
return;
}
if (isAuthorized) {
manager.onHandshake(sessionId, handshakeData);
// self.store.publish('handshake', sessid, newData || handshakeData);
connect(socket);
} else {
error(socket, e);
}
}
});
}
break;
case "ack":
Map<String, Handler<JsonArray>> acks = socket.getAcks();
if (acks.size() > 0) {
Handler ackHandler = acks.get(packet.getString("ackId"));
if (ackHandler != null) {
ackHandler.handle(packet.getArray("args"));
} else {
log.info("unknown ack packet");
}
}
break;
case "event":
// check if the emitted event is not blacklisted
if (manager.getSettings().getBlacklist().indexOf(packet.getString("name")) != -1) {
log.debug("ignoring blacklisted event \'" + packet.getString("name") + "\'");
} else {
JsonObject params = new JsonObject();
JsonArray args = packet.getArray("args");
if(args != null)
params.putArray("args", args);
params.putString("name", packet.getString("name"));
if (isDataAck) {
params.putString("ack", packet.getString("ack"));
}
socket.emit(params);
}
break;
case "disconnect":
this.manager.onLeave(sessionId, this.name);
// this.store.publish('leave', sessid, this.name);
socket.emitDisconnect(packet.getString("reason", "packet"));
break;
case "json":
case "message":
JsonObject params = new JsonObject();
params.putString("name", "message");
Object data = packet.getField("data");
if(data instanceof String) {
params.putString("message", packet.getString("data"));
} else if(data instanceof JsonObject) {
params.putObject("message", packet.getObject("data"));
}
if (isDataAck) {
params.putString("ack", ack);
}
socket.emit(params);
}
}