我一直在尝试向连接到我的集线器的所有客户端广播消息,所有桌面浏览器(IE 9、Chrome 20.x)都会收到消息,但通过 WiFi 连接的移动浏览器除外。我在 IIS7 中部署了应用程序并使用在 LAN 中访问它。
所以我拥有的服务器代码如下 -
[HubName("notifications")]
public class Notifications : Hub
{
try
{
using (var context = new xxxEntities())
{
// EF code that post the record to the database
Clients.taskAdded(message);
return true;
}
}
catch (Exception ex)
{
Caller.reportError("Unable to create notification message. Make sure title length is between 10 and 140");
return false;
}
客户端代码 -
//Handlers for our Hub callbacks
//Invoked from our TaskHub.cs
this.hub.taskAdded = function (t) {
messages.push(new notificationViewModel(t.Id, t.Message, t.User, t.Notified, self));
//if (guid != test.guid) //notify all clients except the caller
};
这一切都适用于桌面浏览器,我使用 knockout.js 来刷新绑定到 HTML 元素(例如列表)的数据模型。当任务被添加到数据库中时,所有桌面浏览器客户端都得到了刷新,除了移动客户端。
所以你知道为什么移动浏览器没有收到通知吗?
更新 - 我在 IE 9 中也有同样的问题,从 IE 的开发人员工具中复制了堆栈
它使用长轮询传输,我相信 IE 不支持网络套接字,因此 signalR 将传输切换到“长轮询”
这是它每 2 秒尝试一次的 URL
URL - /m.Notifications/signalr/reconnect?transport=longPolling&connectionId=311eed98-4e1d-4a0c-a012-2a41b20eded0&connectionData=%5B%7B%22name%22%3A%22notifications%22%7D%5D&messageId=18&tid=7
Method - GET Result - 500 (Http response code) Initiator - JS 库 XMLHttpRequest
你调用的对象是空的。
堆栈跟踪:
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.HttpContext.GetWebSocketInitStatus() +251
System.Web.HttpContextWrapper.get_IsWebSocketRequest() +46
SignalR.Hosting.AspNet.AspNetResponse.get_IsClientConnected() +48
SignalR.Transports.TransportHeartBeat.AddConnection(ITrackingConnection connection) +280
SignalR.Transports.LongPollingTransport.ProcessReceiveRequest(ITransportConnection connection, Action postReceive) +38
SignalR.TaskAsyncHelper.Interleave(Func`3 before, Func`1 after, T arg, TaskCompletionSource`1 tcs) +181
SignalR.Transports.LongPollingTransport.ProcessRequest(ITransportConnection connection) +295
SignalR.PersistentConnection.ProcessRequestAsync(HostContext context) +892
SignalR.Hosting.AspNet.AspNetHandler.ProcessRequestAsync(HttpContextBase context) +866
System.Web.TaskAsyncHelper.BeginTask(Func`1 taskFunc, AsyncCallback callback, Object state) +50 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +12519412
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288