Skip to content

Commit

Permalink
Add WebSocket.request property and add scoped overloads for handleWeb…
Browse files Browse the repository at this point in the history
…Sockets.
  • Loading branch information
s-ludwig committed Oct 23, 2013
1 parent 68ca4e7 commit ad1cdd7
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions source/vibe/http/websockets.d
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,21 @@ import std.functional;

/**
Returns a HTTP request handler that establishes web socket conections.
Note:
The overloads taking non-scoped callback parameters are scheduled to
be deprecated soon.
*/
HTTPServerRequestDelegate handleWebSockets(void delegate(scope WebSocket) on_handshake)
{
return handleWebSockets(ws => on_handshake(ws));
}
/// ditto
HTTPServerRequestDelegate handleWebSockets(void function(scope WebSocket) on_handshake)
{
return handleWebSockets(ws => on_handshake(ws));
}
/// ditto
HTTPServerRequestDelegate handleWebSockets(void delegate(WebSocket) on_handshake)
{
void callback(HTTPServerRequest req, HTTPServerResponse res)
Expand Down Expand Up @@ -83,7 +97,7 @@ HTTPServerRequestDelegate handleWebSockets(void delegate(WebSocket) on_handshake
res.headers["Connection"] = "Upgrade";
Stream conn = res.switchProtocol("websocket");

auto socket = new WebSocket(conn);
/*scope*/ auto socket = new WebSocket(conn, req);
on_handshake(socket);
}
return &callback;
Expand All @@ -103,11 +117,13 @@ class WebSocket {
TCPConnection m_conn;
bool m_sentCloseFrame = false;
IncomingWebSocketMessage m_nextMessage = null;
const HTTPServerRequest m_request;
}

this(Stream conn)
this(Stream conn, HTTPServerRequest request)
{
m_conn = cast(TCPConnection)conn;
m_request = request;
assert(m_conn);
}

Expand All @@ -128,6 +144,11 @@ class WebSocket {
return m_conn.connected && !m_sentCloseFrame;
}

/**
The HTTP request the established the web socket connection.
*/
@property const(HTTPServerRequest) request() const { return m_request; }

/**
Checks if data is readily available for read.
*/
Expand Down

0 comments on commit ad1cdd7

Please sign in to comment.