Я пытаюсь использовать SignalR 2.4.1 в Asp.Net MVC, я сделал пример кода ниже,
Я использую WithGenerateProxy для установления соединения.
Метод хаба вызывается на стороне сервера, получен журнал на стороне клиента
[18:56:10 GMT + 0530 (стандартное время Индии)] SignalR: запуск события хаба клиента 'SendAsync'на концентраторе ChatHub.
Но клиентский метод broadcast не запускается.
Что мне не хватает, ниже мои коды.
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>