It's complicated. By design, if you do not have a hub subscription, the client cannot receive messages from the server, so OnConnected will not be called. I tried the following:
using System; using System.Web; using Microsoft.AspNet.SignalR; using System.Web.Script.Serialization; using System.Collections.Generic; using System.Threading.Tasks; public class ConsultasHub : Hub { public override Task OnConnected() {
call:
var chat = $.connection.consultasHub; console.log("connecting..."); $.connection.hub.start().done(function () { console.log("done."); });
And OnConnected is not enabled, the console message is "done." it is written.
However, as soon as I have an event and a subscription,
using System; using System.Web; using Microsoft.AspNet.SignalR; using System.Web.Script.Serialization; using System.Collections.Generic; using System.Threading.Tasks; public class ConsultasHub : Hub { public override Task OnConnected() {
var chat = $.connection.consultasHub; chat.client.message = function (message) { console.log("message: " + message); }; console.log("connecting..."); $.connection.hub.start().done(function () { console.log("done."); });
OnConnected fired (breakpoint reached). Give it a try! Hope this helps.
source share