io.socket.parser.IOParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of socket.io-client Show documentation
Show all versions of socket.io-client Show documentation
Socket.IO Client Library for Java
package io.socket.parser;
import io.socket.hasbinary.HasBinary;
import org.json.JSONException;
import org.json.JSONTokener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
final public class IOParser implements Parser {
private static final Logger logger = Logger.getLogger(IOParser.class.getName());
private IOParser() {}
final public static class Encoder implements Parser.Encoder {
public Encoder() {}
@Override
public void encode(Packet obj, Callback callback) {
if ((obj.type == EVENT || obj.type == ACK) && HasBinary.hasBinary(obj.data)) {
obj.type = obj.type == EVENT ? BINARY_EVENT : BINARY_ACK;
}
if (logger.isLoggable(Level.FINE)) {
logger.fine(String.format("encoding packet %s", obj));
}
if (BINARY_EVENT == obj.type || BINARY_ACK == obj.type) {
encodeAsBinary(obj, callback);
} else {
String encoding = encodeAsString(obj);
callback.call(new String[] {encoding});
}
}
private String encodeAsString(Packet obj) {
StringBuilder str = new StringBuilder("" + obj.type);
if (BINARY_EVENT == obj.type || BINARY_ACK == obj.type) {
str.append(obj.attachments);
str.append("-");
}
if (obj.nsp != null && obj.nsp.length() != 0 && !"/".equals(obj.nsp)) {
str.append(obj.nsp);
str.append(",");
}
if (obj.id >= 0) {
str.append(obj.id);
}
if (obj.data != null) {
str.append(obj.data);
}
if (logger.isLoggable(Level.FINE)) {
logger.fine(String.format("encoded %s as %s", obj, str));
}
return str.toString();
}
private void encodeAsBinary(Packet obj, Callback callback) {
Binary.DeconstructedPacket deconstruction = Binary.deconstructPacket(obj);
String pack = encodeAsString(deconstruction.packet);
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy