簡體   English   中英

SignalR客戶端方法沒有被解雇

[英]SignalR Client Method is not getting fired

我正在嘗試在Asp.Net MVC中使用SignalR 2.4.1,我已經完成了下面的示例代碼,

我正在使用WithGenerateProxy建立連接。

Hub方法在服務器端調用,在客戶端接收日志

[格林威治標准時間18:56:10 + 0530(印度標准時間)] SignalR:在集線器“ChatHub”上觸發客戶端集線器事件“SendAsync”。

但客戶端方法廣播沒有被觸發。

在我的代碼下面,我缺少什么。

C#


public class ChatHub : Hub
    {
        public void Broadcast()
        {
            Clients.All.SendAsync("Broadcast");
        }
    }

Startup.cs

using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(SampleSignalR.Startup))]

namespace SampleSignalR
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.MapSignalR();
            // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
        }
    }
}


JavaScript的


<script type="text/javascript" src="~/scripts/jquery-1.6.4.min.js"></script>
<script type="text/javascript" src="~/scripts/jquery.signalR-2.4.1.js"></script>
<script type="text/javascript" src="~/SignalR/hubs"></script>

<script>
var connection = $.connection.chatHub;

    $.connection.hub.url = "http://localhost:64573/signalr";
    $.connection.hub.logging = true;
    $.connection.hub.connectionSlow(function () {
        console.log("slow connection");
    });
    connection.on("Broadcast", function () {
        console.log("Successs");
    });
    $.connection.hub.start().done(function () {
        connection.server.broadcast().done(function () {
            console.log("calling server function");
        });
    });
</script>


<package id="bootstrap" version="3.0.0" targetFramework="net452" />
  <package id="jQuery" version="1.10.2" targetFramework="net452" />
  <package id="Microsoft.ApplicationInsights" version="2.0.0" targetFramework="net452" />
  <package id="Microsoft.ApplicationInsights.Agent.Intercept" version="1.2.1" targetFramework="net452" />
  <package id="Microsoft.ApplicationInsights.DependencyCollector" version="2.0.0" targetFramework="net452" />
  <package id="Microsoft.ApplicationInsights.JavaScript" version="0.22.9-build00167" targetFramework="net452" />
  <package id="Microsoft.ApplicationInsights.PerfCounterCollector" version="2.0.0" targetFramework="net452" />
  <package id="Microsoft.ApplicationInsights.Web" version="2.0.0" targetFramework="net452" />
  <package id="Microsoft.ApplicationInsights.WindowsServer" version="2.0.0" targetFramework="net452" />
  <package id="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" version="2.0.0" targetFramework="net452" />
  <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.SignalR" version="2.4.1" targetFramework="net452" />
  <package id="Microsoft.AspNet.SignalR.Core" version="2.4.1" targetFramework="net452" />
  <package id="Microsoft.AspNet.SignalR.JS" version="2.4.1" targetFramework="net452" />
  <package id="Microsoft.AspNet.SignalR.SystemWeb" version="2.4.1" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net452" />
  <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net452" />
  <package id="Microsoft.Net.Compilers" version="1.0.0" targetFramework="net452" developmentDependency="true" />
  <package id="Microsoft.Owin" version="2.1.0" targetFramework="net452" />
  <package id="Microsoft.Owin.Host.SystemWeb" version="2.1.0" targetFramework="net452" />
  <package id="Microsoft.Owin.Security" version="2.1.0" targetFramework="net452" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net452" />
  <package id="Modernizr" version="2.6.2" targetFramework="net452" />
  <package id="Newtonsoft.Json" version="6.0.4" targetFramework="net452" />
  <package id="Owin" version="1.0" targetFramework="net452" />

網絡配置


<appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.webServer>
    <modules>
      <remove name="FormsAuthenticationModule" />
    </modules>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

我認為如果你真的想發送一些數據它會工作:

var data = "hello world";
Clients.All.SendAsync("Broadcast", data);

對於接收數據的函數,請輸入參數:

connection.on("Broadcast", function (data) {
    console.log(data);
});

你需要在js中添加客戶端方法

       chat.client.receiveBroadcast = function (data) {
           console.log(data);
        };

在Hub

    public void Broadcast()
    {
        Clients.All.receiveBroadcast("Broadcast");
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM