SignalR 客户端方法未被解雇
SignalR Client Method is not getting fired
我正在尝试在 Asp.Net MVC 中使用 SignalR 2.4.1,我已经完成了下面的示例代码,
我正在使用 WithGenerateProxy 建立连接。
在服务器端调用集线器方法,在客户端接收到日志
[18:56:10 GMT+0530 (India Standard Time)] SignalR: Triggering client hub event 'SendAsync' on hub 'ChatHub'.
但是客户端方法广播没有被触发。
我缺少什么,下面是我的代码。
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");
}
我正在尝试在 Asp.Net MVC 中使用 SignalR 2.4.1,我已经完成了下面的示例代码,
我正在使用 WithGenerateProxy 建立连接。
在服务器端调用集线器方法,在客户端接收到日志
[18:56:10 GMT+0530 (India Standard Time)] SignalR: Triggering client hub event 'SendAsync' on hub 'ChatHub'.
但是客户端方法广播没有被触发。
我缺少什么,下面是我的代码。
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");
}