繁体   English   中英

使用Microsoft Edge时SignalR无法接收广播

[英]SignalR unable to receive broadcast when in Microsoft Edge

使用Microsoft Edge时,从集线器获取消息时遇到问题。

连接已建立,从客户端->服务器发送消息正常,但未收到服务器推送的任何响应。 相同的代码虽然可以在Chrome和Firefox中使用。

以下是我们正在使用的一些代码:

JS:

$.connection.hub.start()
    .done(function () {
        $.connection.myHub.server.broadcastMessage().done(function (data) {
            console.log("broadcastMessage result: " + data); //work as expected when client request data from server, server does return the data
        });
    })
    .fail(function () {
        console.log("Connection failed!");
    });

$.connection.myHub.client.showMessage = function (msg) {
    alert(msg); //not working, in Microsoft Edge we're not receiving anything, this function is not triggered at all
};

C#:

public string BroadcastMessage() {
    Clients.All.showMessage("ABC");
    return "Hello World";
}

虽然我们并非完全无法100%地接收到任何广播,但是确实有95%的情况会发生。

虽然我们无法从服务器接收任何广播,但是来自Client-> Server的后续请求可以正常工作。

当我们在Edge中时, public override Task OnConnected()也不会被点击,但是当我们在Chrome / Firefox中时,代码块会被点击。

任何想法? 这是SignalR或Edge的问题吗?

P / S:我们正在使用JQUERY 3.3.1和SignalR 2.3.0

更新1:

我们试图删除所有内容,并创建了一个空项目,以查看SignalR是否存在问题。 显然,如果这是一个全新的项目,SignalR不会出现此问题,但是在实施表单身份验证后,该问题开始发生,我猜测是因为某个时候服务器尝试向客户端广播消息时,它没有经过身份验证或没有设置cookie?

以下是我们用于实现表单身份验证的代码:

Global.asax

protected void Application_AuthenticateRequest(object sender, EventArgs e)
    {
        if (HttpContext.Current.User != null)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                if (HttpContext.Current.User.Identity is FormsIdentity identity)
                {
                    FormsIdentity id = identity;
                    FormsAuthenticationTicket ticket = id.Ticket;

                    string userData = ticket.UserData;
                    string[] roles = userData.Split(',');
                    HttpContext.Current.User = new GenericPrincipal(id, roles);
                }
            }
        }
    }

Web配置

<authentication mode="Forms">
  <forms name="LoginCookie" loginUrl="/Account/Login" protection="None" path="/" defaultUrl="/Account/Login" timeout="3600" />
</authentication>

Web.Config中的代码以阻止文件夹访问

    <location path="CMS/Admin" allowOverride="true">
    <system.web>
      <authorization>
        <allow roles="Admin" />
        <deny users="*" />
      </authorization>
    </system.web>
   </location>

样本页面位于/ CMS / Admin中。

尝试在以下位置更改您的C#代码:

public string BroadcastMessage() {
   IHubContext context = GlobalHost.ConnectionManager.GetHubContext<myHub>();
   context.Clients.All.showMessage("ABC");
   return "Hello World";
}

没有IHubContext context = GlobalHost.ConnectionManager.GetHubContext<myHub>(); 我无法使用signalR

暂无
暂无

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

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