在将代码放入母版页时,SignalR仅第一次工作

时间:2015-04-08 07:02:38

标签: c# asp.net signalr master-pages

我将signalR代码放在masterpage.aspx中。像这样

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

<script type="text/javascript">   
    function GetChat() {
        var chat = $.connection.notificationHub;
        return chat;
    }
    function OnStartHub(loginID) {            
        var chat = GetChat();
        chat.client.addMessage = function (name, message) {

            alert(message);
        };           
        $.connection.hub.start().done(function () {             
            var loginID = clienthiddenUserID.Get("UserID");
            var loginname = clienthiddenUserID.Get("UserName");
            $.connection.hub.qs = "name=" + loginID + "";
            chat.server.receive(loginID, loginname);
        });
    }
    $(function () {

        OnStartHub();

        setTimeout(ReceiveNoti, 10000);
    });
    function ReceiveNoti() {
       // alert("Enter Receive");
        var chat = GetChat();
        var loginID = clienthiddenUserID.Get("UserID");
        var loginname = clienthiddenUserID.Get("UserName");
        chat.server.receive(loginID, loginname);
        setTimeout(ReceiveNoti, 10000);
    }

当我登录我的应用程序并default.aspx加载时,这很有用。但当我移动到另一个页面,如Users.aspx时,它会在js中引发错误

Unhandled exception at line 18, column 13 in http://localhost:49483/Targets.aspx

 0x800a138f - JavaScript runtime error: Unable to get property 'notificationHub' of undefined or null reference

1 个答案:

答案 0 :(得分:0)

您已将脚本引用添加为<script src="signalr/hubs"></script>,因此referance相对于该特定页面,将其更改为<script src="/signalr/hubs"></script>,因此它将相对于base而脚本将加载。