Skip to content

Commit

Permalink
Added sendBinary method for WebSocket class.
Browse files Browse the repository at this point in the history
  • Loading branch information
kyubuns committed Oct 26, 2012
1 parent 960fa82 commit adf2672
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions source/vibe/http/websockets.d
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,14 @@ struct Frame {
class OutgoingWebSocketMessage : OutputStream {
private {
Stream m_conn;
FrameOpcode m_frameOpcode;
Appender!(ubyte[]) m_buffer;
}

this( Stream conn ) {
this( Stream conn, FrameOpcode frameOpcode ) {
assert(conn !is null);
m_conn = conn;
m_frameOpcode = frameOpcode;
}

void write(in ubyte[] bytes, bool do_flush = true) {
Expand All @@ -133,7 +135,7 @@ class OutgoingWebSocketMessage : OutputStream {
}
void flush() {
Frame frame;
frame.opcode = FrameOpcode.Text;
frame.opcode = m_frameOpcode;
frame.fin = true;
frame.payload = m_buffer.data;
frame.writeFrame(m_conn);
Expand All @@ -142,9 +144,9 @@ class OutgoingWebSocketMessage : OutputStream {
void finalize() {
Frame frame;
frame.fin = true;
frame.opcode = FrameOpcode.Text;
frame.opcode = m_frameOpcode;
frame.payload = m_buffer.data;
frame.writeFrame(m_conn);
frame.writeFrame(m_conn);
m_buffer.clear();
}
void write(InputStream stream, ulong nbytes = 0, bool do_flush = true) {
Expand Down Expand Up @@ -231,8 +233,12 @@ class WebSocket {
{
send( (message) { message.write(data); });
}
void send(void delegate(OutgoingWebSocketMessage) sender) {
auto message = new OutgoingWebSocketMessage(m_conn);
void sendBinary(ubyte[] data)
{
send( (message) { message.write(data); }, FrameOpcode.Binary );
}
void send(void delegate(OutgoingWebSocketMessage) sender, FrameOpcode frameOpcode = FrameOpcode.Text) {
auto message = new OutgoingWebSocketMessage(m_conn, frameOpcode);
sender(message);
}

Expand Down Expand Up @@ -291,4 +297,4 @@ HttpServerRequestDelegate handleWebSockets(void delegate(WebSocket) onHandshake)
onHandshake(socket);
}
return &callback;
}
}

1 comment on commit adf2672

@s-ludwig
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the technical details(*), but wouldn't it make sense to have two overloads of send - one that takes a string and one a ubyte[]? The former would then send as Text and the latter as Binary.

(*) like are text messages UTF-8 encoded and is there any difference in transport between text and binary at all

Please sign in to comment.