【发布时间】:2019-05-22 19:59:28
【问题描述】:
我遇到了服务器端无法与客户端通信的问题。一旦从客户端发送数据,我就会在后端接收数据,但是,反之亦然(当数据发送回客户端时)。
我的项目设置:
使用框架 4.6.2 创建了一个空的 ASP.NET 应用程序
已安装 nuget 包 'Microsoft.AspNet.SignalR' v2.4.0
使用 jQuery 3.3.1
添加了启动类并插入了“app.MapSignalR();”进入 配置方法
添加了一个集线器,该集线器具有一种与 客户
添加了一个 js 文件,用于启动集线器并与 服务器
我尝试了以下方法但没有成功:
- 使用框架 4.5
- 使用旧版本的 jQuery
- 记录数据 后端(我确实从客户端接收数据)
- 尝试将消息发送回所有客户端 (Clients.All),而不仅仅是调用者
- 重新创建一个新的 signalR 项目
- 确保没有 JS 错误
- 在多个浏览器(Chrome 和 Edge)中本地运行
index.aspx:
<script src="/js/third_party/jquery-3.3.1.min.js"></script>
<script src="/js/third_party/jquery-ui.min.js"></script>
<script src="/js/third_party/jquery.signalR-2.4.0.min.js"></script>
<script src='/signalr/js'></script>
<script src="/js/client.js"></script>
client.js:
var conn = null;
$(function () {
$.connection.hub.stateChanged(connectionStateChanged);
$.connection.hub.start({ waitForPageLoad: false });
conn = $.connection.login;
function connectionStateChanged(state) {
switch (state.newState) {
case 1: // Connected
conn.server.announce('This is an announcement.'); // Works fine and sends the message
break;
}
}
// I'm using '.client', so there is no reason why this should not be triggered
conn.client.onAnnounce = function (message) {
alert(message); // Does NOT receive the message back
debugger;
}
Login.cs:
public class Login : Hub
{
public void Announce(string message)
{
// I receive the data here from the client... but it does not get sent back to the client
Clients.Caller.onAnnounce(message);
}
}
Startup.cs:
[assembly: OwinStartup(typeof(AutoPlayer.Startup))]
namespace SignalRTest
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
我希望服务器收到一条消息,服务器将同样的消息发送回客户端,并让客户端提醒该消息。
【问题讨论】:
标签: javascript c# asp.net signalr signalr.client