我试图在Asp.Net MVC中使用SignalR 2.4.1,我已经在下面完成了示例代码,
我正在使用 WithGenerateProxy 建立连接。
在服务器端调用集线器方法,在客户端接收到日志
[18:56:10 GMT + 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>
答案 0 :(得分:0)
如果您确实想发送一些数据,我认为它会起作用:
var data = "hello world";
Clients.All.SendAsync("Broadcast", data);
对于要接收数据的函数,请输入参数:
connection.on("Broadcast", function (data) {
console.log(data);
});
答案 1 :(得分:0)
您需要在
之类的js中添加客户端方法 chat.client.receiveBroadcast = function (data) {
console.log(data);
};
和在中心
public void Broadcast()
{
Clients.All.receiveBroadcast("Broadcast");
}