简体   繁体   English

.NET Core 2.1 / Angular - SignalR只需2分钟即可连接到集线器

[英].NET Core 2.1 / Angular - SignalR takes exactly 2 minutes to connect to hub

I have an Ionic app and I want it to connect to my socket. 我有一个Ionic应用程序,我希望它连接到我的套接字。 This worked in the SignalR preview just fine, and it essensialy still does, but it takes 2 minutes to connect for some reason... 这在SignalR预览中工作得很好,并且它仍然可以,但由于某种原因连接需要2分钟...

在此输入图像描述

I also get some errors when it connects: 连接时我也会遇到一些错误:

在此输入图像描述

This is my javascript code: 这是我的javascript代码:

ngOnInit() {

    const connection = new signalR.HubConnectionBuilder()
          .withUrl("http://192.168.178.11:8040/socket?sessionId=3dc1dc11")
          .build();

    connection.on("ReceiveMessage", message => {
      console.log(message);

      this.zone.run(() => {
        if(this.locked == true) {
          this.locked = false
        } else {
          this.locked = true;
        }
      });

      console.log(this.locked);
    });

    connection.start().catch(err => console.error);

}

This is my Hub: 这是我的中心:

public class DeviceHub : Hub
{
        public override Task OnConnectedAsync()
        {
            var sessionId = Context.GetHttpContext().Request.Query["sessionId"];

            return Groups.AddToGroupAsync(Context.ConnectionId, sessionId);
        }
}

And this is my configuration in Startup.cs: 这是我在Startup.cs中的配置:

app.UseSignalR(routes =>
{
    routes.MapHub<DeviceHub>("/socket");
});

Now my question is: How do I solve this? 现在我的问题是:我该如何解决这个问题?

EDIT 1: 编辑1:

The delay is before the OnConnectedAsync() method is invoked. 延迟发生在调用OnConnectedAsync()方法之前。

EDIT 2: 编辑2:

One more thing I think I should add is that it directly does a request to my API: 我认为我应该添加的另一件事是它直接向我的API发出请求:

ws://192.168.178.11:8040/socket?sessionId=3dc1dc11&id=Pt-JDlSPq2_WEIl-8cdPZA

And that's the request that takes exactly two minutes to finish. 这就是需要两分钟才完成的请求。

EDIT 3: 编辑3:

One more thing I would like to point out is that I am running a proxy. 我想指出的另一件事是我正在运行代理。 I cannot connect directy from my phone to the API, so I use a proxy for it. 我无法将手机中的directy连接到API,因此我使用代理 I have no idea if this has anything to do with it, but I just wanted to point it out just in case. 我不知道这是否与它有任何关系,但我只想指出以防万一。

Well... 好...

Shame on me. 对我感到羞耻。

The proxy caused the problem. 代理引起了这个问题。

I have added bindings for my own IP in Visual Studio and it works now. 我在Visual Studio中为自己的IP添加了绑定,现在可以正常工作了。

To do this open \\APPLICATION_FOLDER\\.vs\\config\\applicationhost.config . 要执行此操作,请打开\\APPLICATION_FOLDER\\.vs\\config\\applicationhost.config

Then look for <bindings> and add your own. 然后查找<bindings>并添加您自己的。

Example: 例:

<bindings>
  <binding protocol="http" bindingInformation="*:63251:localhost" />
  <binding protocol="https" bindingInformation="*:44333:localhost" />
  <binding protocol="http" bindingInformation="*:63251:192.168.178.11" />
  <binding protocol="https" bindingInformation="*:44333:192.168.178.11" />
</bindings>

Save it, and you're done. 保存它,你就完成了。

Note: 注意:

From now on you have to launch Visual Studio as administrator otherwise your application won't launch. 从现在开始,您必须以管理员身份启动Visual Studio,否则您的应用程序将无法启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM