Skip to content

Instantly share code, notes, and snippets.

@activetheory
Created January 5, 2016 20:50
Show Gist options
  • Save activetheory/235b4410d38730bf0bf0 to your computer and use it in GitHub Desktop.
Save activetheory/235b4410d38730bf0bf0 to your computer and use it in GitHub Desktop.
private void setup() {
AsyncServer.getDefault().connectSocket(_host, new ConnectCallback() {
@Override
public void onConnectCompleted(Exception ex, final AsyncSocket socket) {
handleConnectCompleted(ex, socket);
}
});
}
private void handleConnectCompleted(Exception ex, final AsyncSocket socket) {
if(ex != null) throw new RuntimeException(ex);
_server = socket;
socket.setDataCallback(new DataCallback() {
@Override
public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
String message = new String(bb.getAllByteArray());
_parent.receiveFromServer(message);
}
});
socket.setClosedCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
if(ex != null) throw new RuntimeException(ex);
}
});
socket.setEndCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
if(ex != null) throw new RuntimeException(ex);
}
});
_parent.connectionEstablished();
}
public void send(String message) {
_server.write(ByteBuffer.wrap(message.getBytes()));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment