我可以在SignalR Hub中调试OnConnected方法吗?

时间:2015-08-03 23:17:29

标签: c# asp.net asp.net-mvc signalr signalr-hub

我的asp.net项目中有一个Hub,我重写了OnConnected方法,我可以在OnConnected方法中调试代码吗?我放了一个断点,但它没有开火。

var name = 'Manish';
var str = 'Hey, how are you \\ndoing? My name is Manish. It\'s a really hot day in here. Nice to meet you \\nManish. Bye';
if (str.indexOf(name)>-1) {
 alert('Name was found');
} else {
 alert('Your name wasn\'t found');
}

1 个答案:

答案 0 :(得分:8)

这是一个棘手的问题。按照设计,如果您没有订阅集线器,那么客户端无法从服务器获取任何消息,因此OnConnected不会被调用。 我尝试了以下方法:

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() {
        //breakpoint here
        //some code
        //[...]
        return base.OnConnected();
    }
}

致电:

var chat = $.connection.consultasHub;

console.log("connecting...");
$.connection.hub.start().done(function () {
    console.log("done.");
});

OnConnected未触发,控制台消息已完成。'是写的。

但是,一旦我有活动和订阅,

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() {
        //breakpoint here
        //some code
        //[...]
        return base.OnConnected();
    }

    public void SendMessage( string message ) {
        Clients.All.message(message);
    }
}   
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已触发(已达到断点)。试试看! 我希望它有所帮助。