}
}
private void parse(String message, List<Serializable> messages) {
if (expectingDisconnection) {
listener.onError(new AtmosphereClientException("Expecting disconnection but received message: " + message), true);
}
else if (message.isEmpty()) {
listener.onError(new AtmosphereClientException("Invalid empty message received"), true);
}
else {
char c = message.charAt(0);
switch (c) {
case '!':
String initParameters = message.substring(1);
try {
String[] params = initParameters.split(":");
connectionId = Integer.parseInt(params[1]);
listener.onConnected(Integer.parseInt(params[0]), connectionId);
}
catch (NumberFormatException e) {
listener.onError(new AtmosphereClientException("Unexpected init parameters: " + initParameters), true);
}
break;
case '?':
// clean disconnection
expectingDisconnection = true;
break;
case '#':
listener.onHeartbeat();
break;
case '@':
listener.onRefresh();
break;
case '*':
// ignore padding
break;
case '|':
messages.add(message.substring(1));
break;
case ']':
messages.add(unescape(message.substring(1)));
break;
case '[':
case 'R':
case 'r':
case 'f':
try {
messages.add(parse(message));
}
catch (SerializationException e) {
listener.onError(e, true);
}
break;
default:
listener.onError(new AtmosphereClientException("Invalid message received: " + message), true);
}
}
}