最近刚接触到websocket,公司的功能是基于libwebsocket来实现的,遇到一个需求,server端需要拒绝一些client端的连接请求
查了libwebsocket 的官方api文档(User protocol callback)
发现可以通过对LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION 请求回调的返回值做修改达到目的:
called when the handshake has been received and parsed from the client, but the response is not sent yet. Return non-zero to disallow the connection. user is a pointer to the connection user space allocation, in is the requested protocol name In your handler you can use the public APIs lws_hdr_total_length() / lws_hdr_copy() to access all of the headers using the header enums lws_token_indexes from libwebsockets.h to check for and read the supported header presence and content before deciding to allow the handshake to proceed or to kill the connection.
callback返回一个非0值即可。