我遇到一个奇怪的问题,其中使用 SignalR 发送到多个客户端的回调对于某些客户端来说会延迟。
正如您所看到的,客户端 A 接收回调和客户端 B 接收回调之间有整整 7 秒的延迟。
有一个“主”客户端在计时器结束时执行调用:
vm.bettingconnection
.invoke("MoveToNextRound", gameId)
.catch(function (err) {
return console.error(err.toString());
});
Run Code Online (Sandbox Code Playgroud)
“vm”是一个 VueJS 对象
var vm = new Vue({
el: '#gamesquares',
data: function () {
return {
rawgamedata: [],
connection: "",
bettingconnection: "",
scorepredictShow: true,
boardShow: false,
timerShow: false,
expectationShow: false,
restseconds: 0,
roundseconds: 0,
restsecondsleft: 0,
roundsecondsleft: 0,
roundsleft: 0,
currentround: 0,
gamemode: 0,
estimatedpoints: 0,
scoreindex: 0,
currentscore: 0,
targetscore: 0
};
},
Run Code Online (Sandbox Code Playgroud)
被调用的端点的逻辑非常简单:
public async Task MoveToNextRound(Guid gameId) …Run Code Online (Sandbox Code Playgroud) 我和SignalR有以下问题,用户从移动设备访问我的网站.问题是,当手机锁定或用户转到另一个应用程序时,服务器上会触发Disconnect()方法(我尝试向服务器发送一条消息并等待它通过SignalR回复但是没有来自服务器上触发Disconnect方法后的服务器).在此之后它接缝,因为客户端不再连接到服务器.
客户端如何告诉服务器它又回来了?
编辑:David Fowler要求完整代码尝试复制问题.这是完整的代码
除了这里复制的代码之外,我还将一个完整的Visual Studio 2010解决方案上传到以下URL
http://www.sendspace.com/file/lfeurf(URL已过期,不再有效)
请随时下载并测试它.
以下是我的发现
原始问题:我在MVC3网站上使用Signalr 1.0.0-rc1.我的客户有时需要属于多个组.这是我的Hub和客户端javascript中的代码
客户端javascript和HTML
<script type="text/javascript">
//start signalr
var message = $.connection.messageHub;
// Declare a function on the message hub so the server can invoke it
message.client.send = function (message) {
alert(message);
}
// Start the connection and join groups
$.connection.hub.start();
//bind click event of button to invoke send function on server
$(document).ready(function () {
$("#target").click(function () {
message.server.send("Hello from signalr");
});
});
</script>
<button id="target">Click to send message to …Run Code Online (Sandbox Code Playgroud) 我在IIS Express上设置了一个SignalR集线器,以及一个带有标准聊天示例应用程序的示例网站.它在浏览器中运行良好,我可以看到同时在不同浏览器上显示的消息.不幸的是,我遇到了类似在WPF中工作的障碍.
我已经将聊天示例设置为基础.最终,我将需要服务器将更新推送到所有连接的WPF客户端.有人能告诉我哪里出错了吗?
毂:
public class Chat : Hub
{
public void Send(string message)
{
Clients.All.send(message);
}
}
Run Code Online (Sandbox Code Playgroud)
WPF示例类:
public class ChatSample
{
public ChatSample()
{
var hubConnection = new HubConnection("http://localhost:21638/");
var proxy = hubConnection.CreateHubProxy("Chat");
proxy.On<string>("send", Console.WriteLine);
hubConnection.Start().Wait();
}
}
Run Code Online (Sandbox Code Playgroud)
当前实现给出以下例外:
hubConnection.Start().Wait();
{"The remote server returned an error: (401) Unauthorized."}
Run Code Online (Sandbox Code Playgroud) 我有一个Web窗体应用程序和测试,以查看SignalR如何满足我的要求.我的中心代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace SignalRTest.Hubs
{
public class NotificationHub : Hub
{
public static readonly System.Timers.Timer _Timer = new System.Timers.Timer();
public NotificationHub()
{
var myInfo = Context.QueryString["myInfo"];
_Timer.Interval = 2000;
_Timer.Elapsed += TimerElapsed;
_Timer.Start();
}
void TimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Random rnd = new Random();
int i = rnd.Next(0, 2);
var hub = GlobalHost.ConnectionManager.GetHubContext("NotificationHub");
hub.Clients.All.Alert(i);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的客户电话:
<script type="text/javascript">
$(function () {
var logger = $.connection.notificationHub;
logger.client.Alert = function …Run Code Online (Sandbox Code Playgroud) 我正在使用asp.net webapi启动signalR,我正在尝试了解整个signalR请求/连接管道以及集线器的生命周期以及创建和部署集线器实例的人员(它是否在客户端之间的每次通信中创建)和服务器?)
其中一个原因是我们需要了解我们应该如何在signalR请求场景中使用IoC包含,我们如何控制依赖的生命周期,尤其是ISession或DBContext.
我有一个小型的SignalR项目,我已经开始了,它现在所做的只是接收一个字符串并将其回送给所有连接的用户.
我想知道的是,因为SignalR在我的服务器上打开websockets - 如何使用常规websockets javascript代码连接到服务?(我有理由想要在没有SignalR库的情况下这样做).
我看过使用chrome开发人员工具,我发现浏览器连接的地址是:
ws://localhost:53675/signalr/connect?transport=webSockets&clientProtocol=1.4&connectionToken=YKgNxA6dhmtHya1srzPPIv6KFIYEjLTFOogXqcjPErFcKCmRdjvS2X6A2KmraW%2BrLnRUNf68gYPdOkOOgJ8yRcq4iCDm%2BbUyLejsr2NySNZBvLloWuMIAvgI6oC%2Fyt%2Ba&connectionData=%5B%7B%22name%22%3A%22ophirhubtest%22%7D%5D&tid=7
Run Code Online (Sandbox Code Playgroud)
如何生成令牌?
然后,似乎客户端和服务器之间的消息只是常规的json格式文本(很容易模仿):
{"C":"d-9E7D682A-A,1|E,0|F,1|G,0","S":1,"M":[]}
{"H":"ophirhubtest","M":"Echo","A":["test"],"I":0}
{"C":"d-9E7D682A-A,2|E,0|F,1|G,0","M":[{"H":"ophirHubTest","M":"printEcho","A":["You said: test"]}]}
Run Code Online (Sandbox Code Playgroud)
如果我只是尝试连接而不是连接,但连接很快就会关闭.如果我删除令牌,它会立即关闭.
是否可以"手动"连接到WS?
我有一些执行I/O操作的集线器方法,我想异步调用它.
ConfigureAwait(false)在我的集线器方法中使用是否安全,或者SignalR是否需要捕获的SynchronizationContext用于请求信息或类似的东西?
public async Task<Response> Save() {
// do some prep work
var response = await SaveThingsAsync().ConfigureAwait(false);
// do something with response
return response;
}
Run Code Online (Sandbox Code Playgroud)
为了澄清:我的代码不需要线程文化或SynchronizationContext中存储的任何其他内容.
我担心的是SignalR服务器代码可能在那里存储信息,例如客户端ID,请求信息等,如果从不同的线程继续执行异步方法,它可能无法访问.
当我测试我的方法它似乎工作正常,但这并不能证明任何必要.
我正在尝试在我的webapp中创建一个停止按钮.webapp会为不同的文件创建批量快捷方式.我尝试过使用$.connection.shortcutHub.stop()但是这会出现错误Cannot read property 'shortcutHub' of undefined(anonymous function)
代码如下.stop单击按钮后,我需要停止连接.停止按钮的ID是stopButton.
$(document).ready(function () {
// initialize the connection to the server
var progressNotifier = $.connection.shortcutHub;
// client-side sendMessage function that will be called from the server-side
progressNotifier.client.sendMessage = function (message, percent) {
// update progress
UpdateMessage(message, percent);
};
progressNotifier.client.redo = function () {
redo();
};
progressNotifier.client.success = function () {
success();
};
progressNotifier.client.fail = function () {
fail();
};
// establish the connection to the server …Run Code Online (Sandbox Code Playgroud) 我正在尝试将userId传递给连接到signalR的集线器。客户端通过以下方式建立连接:
connection = new HubConnectionBuilder()
.WithUrl("http://localhost:56587/hub", options =>
{
options.Headers["UserId"] = loginTextBox.Text;
})
.AddMessagePackProtocol()
.Build();
Run Code Online (Sandbox Code Playgroud)
如何在我的集线器的OnConnectedAsync()方法中读取此标头?
header asp.net-web-api signalr-hub asp.net-core asp.net-core-signalr
signalr-hub ×10
signalr ×9
c# ×4
javascript ×2
.net-core ×1
asp.net-core ×1
header ×1
websocket ×1