【发布时间】:2015-12-16 11:17:02
【问题描述】:
我已经成功地为我在 .net 网站中的时事通讯发送功能开发了一个集线器。 服务器程序由集线器调用,然后在发送例程期间,我发送一个客户端方法报告进度状态,最后发送另一个客户端方法报告例程结束。
我的开发环境是带有 IIS 10、VS2013、.NET 4.5 SignalR 2.2.0 的 win10。 我可以在我的开发中使用 websocket 并且工作正常,但是我的生产服务器是 win 2008r2 (IIS 7.5),所以我必须使用 serverSentEvents,在开发中也可以正常工作。
这里是我的客户端代码:
$.connection.hub.logging = true;
hubConn = $.connection.newsletterHub;
hubConn.client.addProgress = function (perc, label) {
UpdateProgress(perc, label)
}
hubConn.client.raiseError = function (message) {
alert(message);
}
hubConn.client.finishSent = function (D) {
Sent(D);
}
hubConn.client.notify = function (msg) {
console.log(msg);
}
$.connection.hub.start({ transport: ['serverSentEvents'] }).done(function () {
ishubdone = true;
});
还有我的 Hub 类:
public class NewsletterHub : Hub, IRequiresSessionState
{
public void DoSendReal()
{
// other stuff
foreach (string mre in tosend_Manual)
{
// other stuff
Clients.Caller.addProgress(((decimal)cur / (decimal)tc), string.Format("{0}Emails", cur));
}
// other stuff
Clients.Caller.finishSent(R.ToString());
}
}
还有我的 Owin 初创公司:
using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;
using Microsoft.AspNet.SignalR;
[assembly: OwinStartup(typeof(Shopper.OwinStartup))]
public static partial class Shopper
{
public class OwinStartup
{
public void Configuration(IAppBuilder app)
{
var hubConfiguration = new HubConfiguration();
hubConfiguration.EnableDetailedErrors = true;
app.MapSignalR();
}
}
}
这是开发环境中的chrome登录,一切正常,服务器调用,客户端方法触发:
[13:01:18 GMT+0100] SignalR: Client subscribed to hub 'newsletterhub'.
[13:01:18 GMT+0100] SignalR: Negotiating with '/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22newsletterhub%22%7D%5D'.
[13:01:18 GMT+0100] SignalR: serverSentEvents transport starting.
[13:01:18 GMT+0100] SignalR: Attempting to connect to SSE endpoint 'http://barzo.topten/signalr/connect?transport=serverSentEvents&clientProtoc…VOMDrrj&connectionData=%5B%7B%22name%22%3A%22newsletterhub%22%7D%5D&tid=10'.
[13:01:18 GMT+0100] SignalR: EventSource connected.
[13:01:18 GMT+0100] SignalR: serverSentEvents transport connected. Initiating start request.
[13:01:18 GMT+0100] SignalR: The start request succeeded. Transitioning to the connected state.
[13:01:18 GMT+0100] SignalR: Now monitoring keep alive with a warning timeout of 13333.333333333332, keep alive timeout of 20000 and disconnecting timeout of 30000
[13:01:26 GMT+0100] SignalR: Invoking newsletterhub.DoSend
[13:01:30 GMT+0100] SignalR: Triggering client hub event 'addProgress' on hub 'NewsletterHub'.
[13:01:30 GMT+0100] SignalR: Triggering client hub event 'finishSent' on hub 'NewsletterHub'.
[13:01:30 GMT+0100] SignalR: Invoked newsletterhub.DoSend
在生产环境中,仅进行服务器调用,程序运行正常(发送电子邮件),但未触发客户端方法:
[12:58:38 GMT+0100] SignalR: Client subscribed to hub 'newsletterhub'.
[12:58:38 GMT+0100] SignalR: Negotiating with '/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22newsletterhub%22%7D%5D'.
[12:58:38 GMT+0100] SignalR: serverSentEvents transport starting.
[12:58:38 GMT+0100] SignalR: Attempting to connect to SSE endpoint 'http://www.topten-italia.com/signalr/connect?transport=serverSentEvents&cli…v5PQ4%3D&connectionData=%5B%7B%22name%22%3A%22newsletterhub%22%7D%5D&tid=9'.
[12:58:38 GMT+0100] SignalR: EventSource connected.
[12:58:38 GMT+0100] SignalR: serverSentEvents transport connected. Initiating start request.
[12:58:39 GMT+0100] SignalR: The start request succeeded. Transitioning to the connected state.
[12:58:39 GMT+0100] SignalR: Now monitoring keep alive with a warning timeout of 13333.333333333332, keep alive timeout of 20000 and disconnecting timeout of 30000
[13:00:14 GMT+0100] SignalR: Invoking newsletterhub.DoSend
[13:00:17 GMT+0100] SignalR: Invoked newsletterhub.DoSend
我的代码是预编译的,在开发环境中源代码和编译的工作,在生产编译的代码不起作用。
我也尝试过使用 Clients.Client(Context.ConnectionId) 而不是 Clients.Caller 具有相同的行为。
我已阅读常见问题解答中链接的StackOverflow 文章。
如何验证客户端方法未触发的原因?
最好的问候
【问题讨论】:
-
从 Chrome 中的 JavScript 控制台,您可以模拟服务器事件并检查客户端方法是否正常运行。
$.connection.yourHub.client.clientMethodName(arguments)这将触发clientMethodName和arguments。试试这个,让我知道它是否有效
标签: asp.net c#-4.0 signalr signalr-hub signalr.client