缓存消息以错误的顺序显示SignalR

时间:2014-04-06 11:57:59

标签: javascript signalr

我使用SignalR创建报告系统。我使用了一个已启动的项目,但必须开发它以满足我的需求。系统应显示其他用户以前在新用户登录时写入的消息,但是它显示的是消息,但是消息(目标)的一部分未定义。想知道我怎么能解决这个问题,所以消息应该显示出来。它应该是这样的:

切尔西vs阿森纳(3) - (0):bla bla bla

但显示如下:

bla bla blachelsea vs Arsenal:undefined

代码如下:

服务器端:

    public void SendMessageToAll(string goal, string userName, string message)
    {
        // store last 100 messages in cache
        AddMessageinCache(goal, userName, message);

        // Broad cast message
        Clients.All.messageReceived(goal, userName, message);
    }
}

的JavaScript

    function registerClientMethods(chatHub) {

        // Calls when user successfully logged in
        chatHub.client.onConnected = function (id, teamNames,  allUsers, updates) {

            $('#hdId').val(id);
            $('#hdUserName').val(teamNames);
            $('#spanUser').html(teamNames);

            // Add Existing Messages
            for (i = 0; i < updates.length; i++) {

                AddMessage(updates[i].Goal, updates[i].TeamNames, updates[i].Message);
                var view = AddMessage;
            }
        }

        // On New User Connected
        chatHub.client.onNewUserConnected = function (id, name) {

            var homeScore = $('#homeGoal').val();
            var awayScore = $('#awayGoal').val();

            var goal = "( " + homeScore + " ) - ( " + awayScore + " )";

            AddUser(chatHub, id, name);
            AddScore(chatHub, id, name, goal);
        }

        chatHub.client.messageReceived = function (goal, teamNames, updates) {

            AddMessage(goal, teamNames, updates);
            var view = AddMessage;
        }

    }

    function AddMessage(goal, teamNames, updates) {

        var homeScore = $('#homeGoal').val();
        var awayScore = $('#awayGoal').val();

        $('#divChatWindow').append('<div class="message"><span class="userName">' + teamNames + goal + '</span>: ' + updates + '</div>');

    }

1 个答案:

答案 0 :(得分:0)

在初始读取时,看起来每次集线器实例化时,您的列表可能会被覆盖 - 集线器是瞬态的,并且会为每个连接重新创建。将列表声明移到集线器之外,或者在分配之前测试列表为null。