在ASP.NET Web API应用程序中使用原始websockets的首选方法是什么?
我们想在ASP.NET Web API应用程序的几个接口上使用二进制 WebSockets.我很难确定应该如何完成这项工作,因为.NET似乎存在多个在线冲突和/或过时的实现.
有这似乎是ASP.NET这样的例子一个,但我认为必须有在Web API框架内使用WebSockets的手段.据我所知,你可以在WebAPI中使用Signalr.
我以为使用Microsoft.AspNet.SignalR.WebSockets.WebSocketHandler会起作用,但我不确定如何将WebSocketHandler链接到Controller ......
class MyServiceController : ApiController
{
[HttpGet]
public HttpResponseMessage SwitchProtocols (string param)
{
HttpContext currentContext = HttpContext.Current;
if (currentContext.IsWebSocketRequest ||
currentContext.IsWebSocketRequestUpgrading)
{
// several out-dated(?) examples would
// use 'new MySocketHandler' for ???
var unknown = new ????
currentContext.AcceptWebSocketRequest(unknown);
return Request.CreateResponse(HttpStatusCode.SwitchingProtocols);
}
}
}
class MySocketHandler : WebSocketHandler
{
public MySocketHandler(): base(2048){}
...
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,AcceptWebSocketRequest不再接受WebSocketHandler,而是它的新签名是......
public void AcceptWebSocketRequest(Func<AspNetWebSocketContext, Task> userFunc)
Run Code Online (Sandbox Code Playgroud)
有没有人在ASP.NET Web API应用程序中有最新的实现原始websockets的链接或快速示例?