c# – 在.Net Core中将Nexmo连接到Websocket失败(远程方关闭了We
我正在尝试在进行呼入时将Nexmo连接到Web套接字(用户使用nexmo调用已购买的号码并链接到应用程序).
截至目前,我只是尝试这个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); } } } 这里捕到的异常:
更多例外:
什么想法发生了什么?或者我如何解决这个问题? 我还尝试检查Websocket的WebSocketState,并将其设置为“Open”. 解决方法
我们找到了一个解决方案:
> ngrock不支持websockets(不是免费),所以我们在azure上发布了我们的应用程序. 现在一切正常:) 澄清:ngrock不适用于安全的websockets(wss) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |