Signalr 如何跟踪信号机连接状态?

Signalr 如何跟踪信号机连接状态?,signalr,Signalr,David Fowler亲自向我指出了这段代码,以跟踪信号机用户和州。我已经实现了所有这一切,它的工作非常好,除了我不能弄清楚集线器连接状态变化的显示。我有这个似乎不起作用。有人知道为什么吗 !DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <style> #state {

David Fowler亲自向我指出了这段代码,以跟踪信号机用户和州。我已经实现了所有这一切,它的工作非常好,除了我不能弄清楚集线器连接状态变化的显示。我有这个似乎不起作用。有人知道为什么吗

!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style>
        #state {
            width: 20px;
            height: 50px;
        }
    </style>
</head>
<body>
    <h3 id="msg"></h3>
    <div id="state"></div>

    <script src="../Scripts/jquery-1.10.2.min.js"></script>
    <script src="../Scripts/jquery.signalR-2.0.2.min.js"></script>
    <script src="signalr/hubs"></script>
    <script>

        $(function () {
            var userTracking = $.connection.alphaHub;

            // Need at least one callback for events to be raised on the hub
            userTracking.client.void = function () { };
            $.connection.logging = true;

            $.connection.hub.stateChanged(function (change) {
                if (change.newState === $.signalR.connectionState.connected) {
                    $('#state').css('background-color', 'green');
                } else if (change.newState === $.signalR.connectionState.reconnecting) {
                    $('#state').css('background-color', 'yellow');
                } else if (change.newState === $.signalR.connectionState.disconnected) {
                    $('#state').css('background-color', 'red');
                }

            });

            $.connection.hub.disconnected(function () {
                setTimeout(function () {
                    $.connection.hub.start();
                }, 1000);
            });
        });
    </script>
</body>
</html>
!DOCTYPE html>
#陈述{
宽度:20px;
高度:50px;
}
$(函数(){
var userTracking=$.connection.alphaHub;
//需要至少一个回调才能在集线器上引发事件
userTracking.client.void=函数(){};
$.connection.logging=true;
$.connection.hub.stateChanged(函数(更改){
if(change.newState==$.signal.connectionState.connected){
$('#state').css('background-color','green');
}else if(change.newState===$.signal.connectionState.reconnecting){
$('#state').css('background-color','yellow');
}else if(change.newState==$.signal.connectionState.disconnected){
$('#state').css('background-color','red');
}
});
$.connection.hub.disconnected(函数(){
setTimeout(函数(){
$.connection.hub.start();
}, 1000);
});
});
我的中心部分显示在此处:

public class AlphaHub : Hub
    {
public override async Task OnConnected()
        {
            try
            {
                var name = Context.User.Identity.Name;

                using (savitasEntities2 entities = new savitasEntities2())
                {
                    var user = entities.SUsers
                        .Include(u => u.SConnections)
                        .SingleOrDefault(u => u.UserName == name);

                    if (user == null)
                    {
                        user = new SUser
                        {
                            UserName = name,
                            SConnections = new List<SConnection>()
                        };
                        entities.SUsers.Add(user);
                    }

                    user.SConnections.Add(new SConnection
                    {
                        ConnectionID = Context.ConnectionId,
                        UserAgent = Context.Request.Headers["User-Agent"],
                        LastActivity = DateTimeOffset.UtcNow
                    });
                    // entities.SaveChanges();
                    await entities.SaveChangesAsync();
                }

            }

 public override async Task OnDisconnected()
        {
            try
            { 
                using (savitasEntities2 db = new savitasEntities2())
                {
                    var connection = await db.SConnections.FindAsync(Context.ConnectionId);
                    db.SConnections.Remove(connection);
                    await db.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                c.LogError(ex.Message, "AlphaHub.cs" + " - " + this.GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name);   
            }
        }
公共类AlphaHub:Hub
{
公共覆盖异步任务OnConnected()
{
尝试
{
var name=Context.User.Identity.name;
使用(savitasEntities2 entities=new savitasEntities2())
{
var user=entities.SUsers
.包括(u=>u.S连接)
.SingleOrDefault(u=>u.UserName==name);
if(user==null)
{
用户=新用户
{
用户名=名称,
SConnections=新列表()
};
实体.SUsers.Add(用户);
}
user.SConnections.Add(新的SConnection
{
ConnectionID=Context.ConnectionID,
UserAgent=Context.Request.Headers[“用户代理”],
LastActivity=DateTimeOffset.UtcNow
});
//entities.SaveChanges();
wait entities.saveChangesSync();
}
}
公共重写异步任务OnDisconnected()
{
尝试
{ 
使用(savitasEntities2 db=new savitasenties2())
{
var connection=wait db.SConnections.FindAsync(Context.ConnectionId);
db.SConnections.Remove(连接);
等待db.saveChangesSync();
}
}
捕获(例外情况除外)
{
c、 日志错误(例如,消息“AlphaHub.cs”+“-”+this.GetType().FullName+”+System.Reflection.MethodBase.GetCurrentMethod().Name);
}
}

似乎找不到您的集线器

更改:

<script src="signalr/hubs"></script>

致:



JavaScript控制台中有错误吗?是
OnConnected()和
OnDisconnected()吗
正在中心上激发的方法?是否正在执行hub.stateChanged回调?感谢您的响应。我得到了SCRIPT5007:无法在控制台中获取未定义或空引用的属性“client”,因此它不能连接到hub$.connection.alphaHub;我是否需要像添加[HubName(“alphaHub”)]一样添加[HubName(“alphaHub”)(“userTracking”)]?我认为JavaScript中的默认设置是使用camelCase?检查浏览器开发工具中的“网络”选项卡,查看脚本“信号器/集线器”是否正在加载。其他脚本的位置使我认为它应该类似于“./信号器/集线器”。
<script src="~/signalr/hubs"></script>