截至目前,我只是尝试这个Sample Code(简单地回复了调用者所说的内容),并按照“文档”Here通过Nexmo连接到这个websocket.
我成功地向nexmo发送了一个动作“connect”.在调用Nexmo购买的号码时,它正确地重定向到端点(API / nexmo / socket),如使用断点时所示,但是当它在Echo方法中到达webSocket.ReceiveAsync时它会挂起.
using System; using System.Net.WebSockets; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.http; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.linq; namespace MyProject.Web.Controllers { [Route("API/[controller]")] [APIController] public class NexmoController : ControllerBase { private WebSocket _webSocket; [httpGet("answer")] public IActionResult AnswerHandler() { const string host = "MY_NGROK_URL"; const string locale = "fr-FR"; var nccos = new JArray(); var nccoConnect = new JObject() { { "action","connect" },{ "endpoint",new JArray(new JObject{ { "type","websocket" },{ "uri",$"wss://{host}/API/nexmo/socket"},{ "content-type","audio/l16;rate=16000"},{ "headers",new JObject { { "language",locale },{ "callerID","MY_NUMBER_HARDCODED_WHILE_TESTING" } } } }) } }; nccos.Add(nccoConnect); return Content(nccos.ToString(),"application/Json"); } [httpPost("event")] public IActionResult EventHandler() { return Ok(); } [httpGet("socket")] public async Task GetAudio() { if (httpContext.WebSockets.IsWebSocketRequest) { _webSocket = await httpContext.WebSockets.AcceptWebSocketAsync(); await Echo(httpContext,_webSocket); } else { httpContext.Response.StatusCode = 400; } } //copy Paste from the Sample Code private async Task Echo(httpContext context,WebSocket webSocket) { var buffer = new byte[1024 * 4]; //Breakpoint : ReceiveAsync generates an exception WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer),CancellationToken.None); while (!result.CloseStatus.HasValue) { await webSocket.SendAsync(new ArraySegment<byte>(buffer,result.Count),result.MessageType,result.EndOfMessage,CancellationToken.None); result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer),CancellationToken.None); } await webSocket.CloseAsync(result.CloseStatus.Value,result.CloseStatusDescription,CancellationToken.None); } } }
这里捕到的异常:
System.Net.WebSockets.WebSocketException (0x80004005): The remote
party closed the WebSocket connection without completing the close
handshake. —> System.Net.WebSockets.WebSocketException (0x80004005):
The remote party closed the WebSocket connection without completing
the close handshake.
更多例外:
at System.Net.WebSockets.ManageDWebSocket.ThrowIfEOFUnexpected(Boolean
throwOnPrematureClosure)
at System.Net.WebSockets.ManageDWebSocket.EnsureBufferContainsAsync(Int32
minimumrequiredBytes,CancellationToken cancellationToken,Boolean
throwOnPrematureClosure)
at System.Net.WebSockets.ManageDWebSocket.ReceiveAsyncPrivate[TWebSocketReceiveResultGetter,TWebSocketReceiveResult](Memory`1
payloadBuffer,
TWebSocketReceiveResultGetter resultGetter) at
System.Net.WebSockets.ManageDWebSocket.ReceiveAsyncPrivate[TWebSocketReceiveResultGetter,
TWebSocketReceiveResultGetter resultGetter) at
….Controllers.NexmoController.Echo(httpContext context,
WebSocket webSocket) in
C:…\Controllers\NexmoController.cs:line
97 at ….Controllers.NexmoController.GetAudio() in
C:…\Controllers\NexmoController.cs:line
68 at lambda_method(Closure,Object ) at
Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult()
at
Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.AwaitableResultExecutor.Execute(IActionResultTypeMapper
mapper,ObjectMethodExecutor executor,Object controller,Object[]
arguments) at System.Threading.Tasks.ValueTask`1.get_Result() at
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
at
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterasync()
at
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext
context) at
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State&
next,Scope& scope,Object& state,Boolean& isCompleted) at
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterasync()
at
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
at
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext
context) at
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next,
Scope& scope,Boolean& isCompleted) at
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
at Microsoft.AspNetCore.Routing.EndpointMIDdleware.Invoke(httpContext
httpContext) at
Microsoft.AspNetCore.Routing.EndpointRoutingMIDdleware.Invoke(httpContext
httpContext) at
Microsoft.AspNetCore.Staticfiles.StaticfileMIDdleware.Invoke(httpContext
context) at
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMIDdleware.Invoke(httpContext
context)
什么想法发生了什么?或者我如何解决这个问题?
我真的不明白这里的问题是什么.
我还尝试检查Websocket的WebSocketState,并将其设置为“Open”.
有关信息,我自己测试了示例代码(websocket回应用户的输入)并且它可以工作.
> ngrock不支持websockets(不是免费),所以我们在azure上发布了我们的应用程序.
>我们需要实例化websocket
StartUp而不是我们的控制器(如sample code
listed in the original post).我们已经采用了“Echo”方法
另一个类(更多是为了保持良好的结构)并设置它
静态的.
现在一切正常:)
澄清:ngrock不适用于安全的websockets(wss)
总结以上是内存溢出为你收集整理的c# – 在.Net Core中将Nexmo连接到Websocket失败(远程方关闭了WebSocket)全部内容,希望文章能够帮你解决c# – 在.Net Core中将Nexmo连接到Websocket失败(远程方关闭了WebSocket)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)