Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add doHandleWebsocket function #946

Merged
merged 2 commits into from
Jan 15, 2015
Merged

Conversation

lultimouomo
Copy link
Contributor

This allows establishing a websocket connection directly in an
HTTPServerRequestDelegate, which is useful if one needs to make some
checks on the request (and possibly send an http response with an error
code) before opening the websocket.

Signed-off-by: Luca Niccoli l.niccoli@awtech.it

This allows establishing a websocket connection directly in an
HTTPServerRequestDelegate, which is useful if one needs to make some
checks on the request (and possibly send an http response with an error
code) before opening the websocket.

Signed-off-by: Luca Niccoli <l.niccoli@awtech.it>
res.headers["Connection"] = "Upgrade";
ConnectionStream conn = res.switchProtocol("websocket");

scope socket = new WebSocket(conn, req);
Copy link
Contributor

Choose a reason for hiding this comment

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

How does this compile without warning ?

Copy link
Contributor

Choose a reason for hiding this comment

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

If you refer to scope it was never actually deprecated and still works as it did in D1 days.

Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason behind that ? In Vibe.d's case, would Scoped be a good (better?) replacement ?

Copy link
Contributor

Choose a reason for hiding this comment

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

auto socket = scoped!WebSocket(conn, req); should work unless WebSocket does something nasty.

Copy link
Contributor

Choose a reason for hiding this comment

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

both are inherently unsafe though as on_handshake does not guarantee it won't escape the reference

Copy link
Contributor Author

Choose a reason for hiding this comment

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

both are inherently unsafe though as on_handshake does not guarantee it won't escape the reference

Why not? on_handshake is defined as void delegate(scope WebSocket) so in theory it should not be able to escape the reference to the WebSocket. Are scope parameters not working yet?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Anyway, WebSocket does not even have a destructor defined. Shouldn't we just drop the scope?

Copy link
Contributor

Choose a reason for hiding this comment

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

Are scope parameters not working yet?

Not even slightly so.

@lultimouomo lultimouomo reopened this Dec 31, 2014
*/
HTTPServerRequestDelegate handleWebSockets(WebSocketHandshakeDelegate on_handshake)
void doHandleWebsocket(WebSocketHandshakeDelegate on_handshake, HTTPServerRequest req, HTTPServerResponse res)
Copy link
Member

Choose a reason for hiding this comment

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

The on_handshake parameter could be scope WebSocketHandshakeDelegate here to avoid creating heap delegates at the call site.

@s-ludwig
Copy link
Member

Thanks, this change makes a lot of sense. I'm just still a little unsure about the best name. Usually I'd simply use an additional overload of handleWebSockets (analogous to handleBasicAuth), but of course the plural form doesn't really fit here. The doXYZ style generally shouldn't appear in the public API, ideally everything should be a simple verb or verbNoun combination. If no better alternative comes up, I'd suggest simply handleWebSocket.

Do not declare the WebSocket a scoped variable, it's dangerous and does
not seem to be useful anyway, since WebSocket does not declare a
destructor.
Do not simulate socket closing if the connection delegate throws an
Error, the recommended action is terminating the application anyway.

Signed-off-by: Luca Niccoli <l.niccoli@awtech.it>
@lultimouomo
Copy link
Contributor Author

handleWebSocket seems fine to me.

s-ludwig added a commit that referenced this pull request Jan 15, 2015
@s-ludwig s-ludwig merged commit 5abe244 into vibe-d:master Jan 15, 2015
@lultimouomo lultimouomo deleted the doHandleWebsocket branch January 15, 2015 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants