Skip to content

Commit

Permalink
Merge pull request #118 from kyubuns/websocket-sendbinary
Browse files Browse the repository at this point in the history
send binary with WebSocket class
  • Loading branch information
s-ludwig committed Oct 26, 2012
2 parents bc66eb8 + 76e72da commit bbe5890
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 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 @@ -227,12 +229,16 @@ class WebSocket {
@property bool connected() { return m_conn.connected; }
@property bool dataAvailableForRead() { return m_conn.dataAvailableForRead; }

void send(string data)
{
send( (message) { message.write(cast(ubyte[])data); });
}
void send(ubyte[] data)
{
send( (message) { message.write(data); });
send( (message) { message.write(data); }, FrameOpcode.Binary );
}
void send(void delegate(OutgoingWebSocketMessage) sender) {
auto message = new OutgoingWebSocketMessage(m_conn);
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;
}
}

0 comments on commit bbe5890

Please sign in to comment.