c# – 在.Net Core中将Nexmo连接到Websocket失败(远程方关闭了WebSocket)
作者:互联网
我正在尝试在进行呼入时将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, 0, 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, CancellationToken cancellationToken,
TWebSocketReceiveResultGetter resultGetter) at
System.Net.WebSockets.ManagedWebSocket.ReceiveAsyncPrivate[TWebSocketReceiveResultGetter,TWebSocketReceiveResult](Memory`1
payloadBuffer, CancellationToken cancellationToken,
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, Object& state, 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,websocket,asp-net-core,phone-call,nexmo 来源: https://codeday.me/bug/20190705/1387060.html