Skip to content

Commit

Permalink
Allow sending null messages.
Browse files Browse the repository at this point in the history
Doing so doesn't actually send data, but will trigger sending headers
for http streams, and will call the onComplete handler and honor the
close option for any Channel type.
  • Loading branch information
tobias committed Apr 2, 2015
1 parent 403b807 commit 9865784
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
Expand Up @@ -87,7 +87,9 @@ public boolean send(final Object message,
}

byte[] data;
if (message instanceof String) {
if (message == null) {
data = null;
} else if (message instanceof String) {
data = ((String)message).getBytes(getResponseCharset());
} else if (message instanceof byte[]) {
data = (byte[])message;
Expand Down Expand Up @@ -147,14 +149,18 @@ protected void doSend(final byte[] data,
Throwable ex = null;
try {
if (!sendStarted) {
if (shouldClose) {
if (shouldClose &&
data != null) {
setContentLength(data.length);
}
this.stream = getOutputStream();
sendStarted = true;
}

this.stream.write(data);
if (data != null) {
this.stream.write(data);
}

if (shouldClose) {
this.closer.run();
} else {
Expand Down
Expand Up @@ -108,7 +108,9 @@ public void onResult(SendResult sendResult) {
}
};

if (message instanceof String) {
if (message == null) {
handler.onResult(null);
} else if (message instanceof String) {
this.session.getAsyncRemote().sendText((String)message, handler);
} else if (message instanceof byte[]) {
this.session.getAsyncRemote().sendBinary(ByteBuffer.wrap((byte[])message), handler);
Expand Down
Expand Up @@ -98,7 +98,10 @@ public void onError(WebSocketChannel channel, Void context, Throwable throwable)
maybeCloseOnError(throwable);
}
};
if (message instanceof String) {

if (message == null) {
callback.complete(this.underlyingChannel, null);
} else if (message instanceof String) {
WebSockets.sendText((String) message,
this.underlyingChannel,
callback);
Expand Down

0 comments on commit 9865784

Please sign in to comment.