1

我尝试在 win server 2008 下的 MS Http Server API 上实现 WebSocket 协议(没有 HTTP_SEND_RESPONSE_FLAG_OPAQUE 标志)。

HTTP_RESPONSE response={0};
const char upgrade_val[]="Websocket";
response.Headers.KnownHeaders[HttpHeaderUpgrade].RawValueLength=strlen(upgrade_val);
response.Headers.KnownHeaders[HttpHeaderUpgrade].pRawValue     =upgrade_val;
const char connection_val[]="Upgrade";
response.Headers.KnownHeaders[HttpHeaderConnection].RawValueLength=strlen(connection_val);
response.Headers.KnownHeaders[HttpHeaderConnection].pRawValue     =connection_val;
HTTP_UNKNOWN_HEADER unknown[1];
response.Headers.UnknownHeaderCount=1;
response.Headers.pUnknownHeaders   =unknown;
const char accept_name[]="Sec-WebSocket-Accept";
unknown[0].NameLength    =_countof(accept_name)-1;
unknown[0].pName         =accept_name;
unknown[0].RawValueLength=HANDSHAKE_KEY_LENGTH;
unknown[0].pRawValue     =base64_key;
response.Version.MajorVersion=1;
response.Version.MinorVersion=1;
response.StatusCode          =HTTP_SWITCHING_PROTOCOLS;
const char reason[]              ="Switching Protocols";
response.ReasonLength        =strlen(reason);
response.pReason             =reason;
HttpSendHttpResponse(iocp,RequestId,HTTP_SEND_RESPONSE_FLAG_MORE_DATA,&raw_response,NULL,NULL,NULL,NULL,NULL,NULL);

浏览器发送标头(来自 Fiddler)

GET http://server.host/ HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: server.host
Origin: null
Pragma: no-cache
Cache-Control: no-cache
Sec-WebSocket-Key: V86c1TFOwWfZqhS42C0arA==
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: x-webkit-deflate-frame
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)     Chrome/27.0.1453.94 Safari/537.36

服务器的响应(来自 Fiddler)

HTTP/1.1 101 Switching Protocols
Upgrade: Websocket
Server: Microsoft-HTTPAPI/2.0
Sec-WebSocket-Accept: 3io2SU7uJIeFlwy0+OFJUDNrA44=
Date: Tue, 28 May 2013 09:27:59 GMT
EndTime: 13:27:59.006
ReceivedBytes: 0
SentBytes: 0

并且浏览器显示 1016 错误。

4

2 回答 2

1

假设您的Sec-WebSocket-Accept标头值计算正确,并假设您的response变量应该被命名raw_response,那么您的响应缺少所需的Connection: Upgrade标头,尽管您的代码为其分配了一个值。

于 2013-05-28T23:22:40.883 回答
0

WebSockets 需要HTTP_SEND_RESPONSE_FLAG_OPAQUE(Windows 8 及更高版本)。

Windows Server 上的 WebSocket 服务器(2011-12-20)

就目前而言,Server 2008/R2 机器无法托管 WebSocket。至少,在与 IIS Web 服务器共享端口 80 和 443 时不会

回复:[hybi] WebSocket 协议的现状(2010-11-06)

我们需要调整 http.sys 以识别真正的非 HTTP 请求,即 HTTP 请求

于 2014-02-04T21:38:54.597 回答