-
Notifications
You must be signed in to change notification settings - Fork 18
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 supported protocols to server and client #34
Add supported protocols to server and client #34
Conversation
Thanks for the contribution! But reading up on RFC the logic of this PR does not appear to be fully correct. As far as I understand it, the Client may request one or more subprotocols by adding headers. But these should be considered as a "can I use any of these" request. So it would be up to the Client to close connection if it require a subprotocol the Server do not support. As per https://datatracker.ietf.org/doc/html/rfc6455#section-1.9
The Server may handle headers in current code by using the onConnect listener. |
That makes sense. What do you prefer? Shall I edit the code so it just replies without the header if there is no match or completely remove it an add a middleware to add this feature to the server? My personal preference would be to keep it natively supported since it is in the spec :-) Middleware can be used for anything specific to the implementation on top of the spec. Let me know, i'll be happy to update! |
As of version So my preference here would be a Middleware for Subprotocol negotiation. Then add it to the list of "standard" middlewares in documentation. Available middlewares are added in |
Maybe I misunderstand, but shouldn't it be the processIncomingInterface.php and ProcessOutgoingInterface.php |
Those are middlewares for WebSocket messages. To get and modify the http request/response during handshake you would need the ones with "http" in the name. As example, to add subprotocol headers to client request, you would do something like; public function processHttpOutgoing(ProcessHttpStack $stack, Connection $connection, Message $message): Message
{
// Only for outgoing requests by Client
if ($message instance of Request) {
foreach ($this->subprotocols as $subprotocol) {
$message = $message->withHeader('Sec-WebSocket-Protocol', $subprotocol);
}
}
// Continue processing, when all added middlewares are served it will write $message on connection
return $stack->handleOutgoing($message);
} |
Ah right, we are working with websockets at the moment, but i'll try to add both in there :-) I'll keep you posted! |
@sirn-se After some testing and debugging, it seems a middleware can not be used. The This means that changing the I have updated the current method to be more aligned with the spec and not fail when there is no matching protocol. Can you get behind this, or do you have other ideas? |
@ChristianVermeulen The middleware stack can be a bit tricky at first. I use the same strategy as PSR-15: HTTP Server Request Handlers where a middleware need to consider to change things before or after it delegates further down the chain. I did get it to work, prototype here; https://github.com/sirn-se/websocket-php/pull/35/files Found a small bug though, repo currently do not allow repeated headers. |
@sirn-se Ah, that works perfectly 👌 Thanks for that! I've been looking into writing tests but I can't seem to wrap my head around the Do you have any idea when you might be able to release this? |
closing in favor of #35. |
We needed a bit more control over the handshake. Since it is quite common for websockets to adhere to a specific protocol I chose to specifically implement this. In the
Client
it was already possible to set a protocol in the handshake header, but theServer
had no such way.With this update it is now possible to define which protocols are supported by the server, which will be validated and replied to the client during the handshake.