2015-10-17 48 views
0

这是我的Signal R客户端。 (0x800a139e - JavaScript运行时错误:SignalR:加载集线器出错,请确保您的集线器引用是正确的,例如。)在ASP.net MVC Signal R中连接客户端服务器

异常来自$ .connection.hub.start行

在服务器应用程序运行正常的HUBS文件夹中有一个ServerHub类。

谁能帮助我..谢谢

<script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script> 
<script src="http://localhost:39670/MySignalRServer/signalr/hubs"></script> 
var ChatHubProxy = $.hubConnection("http://localhost:39670/MySignalRServer/signalr/hubs"); 
     var chat = ChatHubProxy.createHubProxy('ServerHub'); 

    chat.on("addNewMessageToPage",function (name, message) { 
     // Add the message to the page. 
      $('#discussion').append('<li><strong>' + htmlEncode(name) 
      + '</strong>: ' + htmlEncode(message) + '</li>'); 
     }); 
     $.connection.hub.start({jsonp:true}).done(function() { 
      $('#sendmessage').click(function() { 
       // Call the Send method on the hub. 
       chat.server.send($('#displayname').val(),  $('#message').val()); 
       alert("hiii"); 
       // Clear text box and reset focus for next comment. 
       $('#message').val('').focus(); 
      }); 
     }); 
+0

1.检查毂name.2。检查是否加载JS文件 –

+0

枢纽名称是正确的.... jquery文件也正确加载....但开始异常说无法加载中心文件...! @J Santosh –

回答

1

试着改变你的代码:

<script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script> 
<script src="http://localhost:39670/MySignalRServer/signalr/hubs"></script> 

    var chat = $.connection.ServerHub; //here 

    chat.on("addNewMessageToPage", function(name, message) { 
     // Add the message to the page. 
     $('#discussion').append('<li><strong>' + htmlEncode(name) 
      + '</strong>: ' + htmlEncode(message) + '</li>'); 
    }); 

    $.connection.hub.start().done(function() { //here 
     $('#sendmessage').click(function() { 
      // Call the Send method on the hub. 
      chat.server.send($('#displayname').val(), $('#message').val()); 
      alert("hiii"); 
      // Clear text box and reset focus for next comment. 
      $('#message').val('').focus(); 
     }); 
    }); 

,并确保该地址是好的 - <script src="http://localhost:39670/MySignalRServer/signalr/hubs"></script>

我总是用这 - <script src="/signalr/hubs"></script>

A第二添加HubName属性

[HubName("ServerHub")] 
public class ServerHub : Hub 
{ 
    public string Send(string name, string message) 
    { 
     Clients.All.broadcastMessage(name, message); 

     return null; 
    } 
} 

或更改此代码:

var chat = $.connection.ServerHub; 

var chat = $.connection.serverHub; 

演示项目:https://github.com/czerwonkabartosz/SignalRDemo

+0

我会尝试这....我的服务器和客户端在两个不同的项目,所以我必须使用本地主机只是正确的? @ Bartosz Czerwonka –

+0

是的,一次运行两个项目,并使用服务器项目中的localhost:port -