signalR:/ signalr /不产生集线器集线器、signalR、signalr

2023-09-14 23:07:02 作者:清茶

我能得到这个tutorial在新项目中的工作,但不是在我现有的项目。

我的项目是在web.config文件中的下列属性的ASP.Net MVC 4 Web应用程序:

 <&的appSettings GT;  <添加键=网页:启用VALUE =真/>< /的appSettings> 

这是因为我的应用程序是一个单页的应用程序,它在客户端使用AngularJS。在我的应用程序的唯一页面index.cshtml,而我已经添加了相关的code为signalR:

 <! -  signalR聊天 - ><脚本SRC =〜/脚本/ jquery.signalR-1.0.0.js>< / SCRIPT><! - 参考自动生成的SignalR枢纽脚本。 - ><脚本的src =/ signalr /集线器>< / SCRIPT><! - 加入脚本来更新页面和发送消息.--><脚本类型=文/ JavaScript的>    $(函数(){        //声明一个代理来引用枢纽。        VAR聊天= $ .connection.chatHub;        //创建集线器可以打电话到广播消息的功能。        chat.client.broadcastMessage =功能(姓名,消息){            // EN的Html code显示名称和消息。            变种EN codedName = $('< D​​IV />')。文本(名称)。html的();            变种EN codedMsg = $('< D​​IV />')。文本(消息)的.html();            //信息添加到页面。            $('#讨论')追加('<立GT;<强>'+ EN codedName                +'< / STRONG>:&安培; NBSP;&安培; NBSP; EN + codedMsg +'< /李>');        };        //获取用户名,并将其存储到prePEND邮件。        $('#显示名称)VAL(提示(请输入您的名字:',''));        //设置初始焦点消息输入框。        $('#消息)专注()。        //开始连接。        $ .connection.hub.start()。完成(功能(){            $('#SendMessage函数')。点击(函数(){                //调用集线器上的发送方法。                chat.server.send($('#显示名称)VAL(),$('#消息)VAL());                //清除文本框和重置重点下一评论。                $('#消息)VAL('')专注()。            });        });    });< / SCRIPT> 

然后,我已经得到了ChatHub.cs文件:

 公共类ChatHub:集线器{    公共无效发送(字符串名称,字符串消息)    {        //调用broadcastMessage方法来更新客户端。        Clients.All.broadcastMessage(姓名,邮件);    }} 
主机静音改造,be quite 这才是工作站应该有的样子

最后在Global.asax:

 保护无效的Application_Start()    {        RouteTable.Routes.MapHubs();        BundleConfig.RegisterBundles(BundleTable.Bundles);    } 

当我运行应用程序,不产生/ signalr /集线器文件。我收到了404请求文件时,它崩溃就行了:

  chat.client.broadcastMessage =功能(姓名,消息){.... 

由于聊天null作为previous线没发现chatHub:

  VAR聊天= $ .connection.chatHub; 

有谁知道什么地方错了我的code?

更新

我已经改变这一行::解决我的问题。

 <脚本的src =/ signalr /集线器>< / SCRIPT> 

 <脚本SRC =〜/ signalr /集线器>< / SCRIPT> 

解决方案

我已经改变这一行::解决我的问题。

 <脚本的src =/ signalr /集线器>< / SCRIPT> 

 <脚本SRC =〜/ signalr /集线器>< / SCRIPT> 

I can get this tutorial to work in a new project, but not in my existing project.

My project is an ASP.Net MVC 4 web application with the following attribute in the web.config file:

<appSettings>
  <add key="webpages:Enabled" value="true"/>
</appSettings>

This is because my application is a Single-Page-Application, which uses AngularJS on the client side. The only page in my application is index.cshtml, to which I've added the relevant code for signalR:

 <!-- signalR chat -->
<script src="~/Scripts/jquery.signalR-1.0.0.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="/signalr/hubs"></script>
<!--Add script to update the page and send messages.--> 
<script type="text/javascript">
    $(function () {
        // Declare a proxy to reference the hub. 
        var chat = $.connection.chatHub;
        // Create a function that the hub can call to broadcast messages.
        chat.client.broadcastMessage = function (name, message) {
            // Html encode display name and message. 
            var encodedName = $('<div />').text(name).html();
            var encodedMsg = $('<div />').text(message).html();
            // Add the message to the page. 
            $('#discussion').append('<li><strong>' + encodedName
                + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
        };
        // Get the user name and store it to prepend to messages.
        $('#displayname').val(prompt('Enter your name:', ''));
        // Set initial focus to message input box.  
        $('#message').focus();
        // Start the connection.
        $.connection.hub.start().done(function () {
            $('#sendmessage').click(function () {
                // Call the Send method on the hub. 
                chat.server.send($('#displayname').val(), $('#message').val());
                // Clear text box and reset focus for next comment. 
                $('#message').val('').focus();
            });
        });
    });
</script>

Then I've got the ChatHub.cs file:

public class ChatHub : Hub
{
    public void Send(string name, string message)
    {
        // Call the broadcastMessage method to update clients.
        Clients.All.broadcastMessage(name, message);
    }
}

And finally in the global.asax:

 protected void Application_Start()
    {
        RouteTable.Routes.MapHubs();
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

When I run the application, the /signalr/hubs file is not generated. I get a 404 when requesting the file, and it crashes on the line:

 chat.client.broadcastMessage = function (name, message) { ....

because chat is null as the previous line did not find chatHub:

var chat = $.connection.chatHub;

Does anyone know what's wrong with my code ?

UPDATE

I have solved my problem by changing the line::

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

to

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

解决方案

I have solved my problem by changing the line::

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

to

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