我正在尝试将WebSocket添加到处理HTTP请求的现有代码中。不使用IIS或ASP.NET,只使用带有HttpListener的C#代码。当客户端发送升级请求时,服务器会发生炸弹:
System.Net.WebSockets.WebSocketException was unhandled by user code
ErrorCode=1229
HResult=-2147467259
Message=An error occurred when sending the WebSocket HTTP upgrade response during the AcceptWebSocketAsync operation. The HRESULT returned is '1229'
NativeErrorCode=1229
Source=System
StackTrace:
at System.Net.WebSockets.WebSocketHelpers.<AcceptWebSocketAsyncCore>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at mycode.<mymethod>d__46.MoveNext()
InnerException:
致电代码:
HttpListenerContext context = listener.GetContext();
if (context.Request.IsWebSocketRequest)
{
HttpListenerWebSocketContext wsContext = await context.AcceptWebSocketAsync(null);
我发现整个WebSockets命名空间上的MS文档没有代码示例,没用。我不知道它在这里告诉我的是什么(除了明显的事实,我没有处理异常 - 我想知道为什么首先出现异常)。
这是Windows 10,.NET framework 4.5。我注意到文档中的地方说WebSockets仅适用于Windows 8和2012,但我认为它也适用于10。
答案 0 :(得分:0)
必须转到IIS下的Windows功能(即使我没有使用IIS),然后打开WebSocket协议。我不知道这是关闭是Win10默认还是仅仅因为为我安装了IT图像。