首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SignalR 2不生成/signalr/集线器

SignalR 2不生成/signalr/集线器
EN

Stack Overflow用户
提问于 2014-10-09 08:30:35
回答 6查看 14.2K关注 0票数 6

这是一页:

代码语言:javascript
复制
        <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/jquery.signalR-2.1.2.min.js"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script src="~/signalr/hubs"></script>
    <!--SignalR script to update the chat page and send messages.-->
    <script>
        $(function () {
            // Reference the auto-generated proxy for the hub.
            var notification = $.connection.notificationHub;
            // Create a function that the hub can call back to display messages.
            notification.client.addNewMessage = function (message) {
                // Add the message to the page.
                $('#discussion').append('<li><strong>'
                    + '</strong>: ' + htmlEncode(message) + '</li>');
            };
            // 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($('#message').val());
                    // Clear text box and reset focus for next comment.
                    $('#message').val('').focus();
                });
            });
        });
        // This optional function html-encodes messages for display in the page.
        function htmlEncode(value) {
            var encodedValue = $('<div />').text(value).html();
            return encodedValue;
        }
    </script>

以下是集线器类:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;

namespace AdminWebApp.Hubs
{
     [HubName("notificationHub")] 
    public class NotificationHub : Hub
    {

        public void SendNotification(string message)
        {
            Clients.All.addNewMessage(message);
        }
    }
}

Startup.cs:

代码语言:javascript
复制
using Microsoft.Owin;
using Owin;

[assembly: OwinStartupAttribute(typeof(AdminWebApp.Startup))]
namespace AdminWebApp
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
        }
    }
}

当我尝试访问:http://localhost:4551/signalr/hubs时,我得到了HTTP404NotFindError,当我试图运行页面时,我得到了:

代码语言:javascript
复制
 Failed to load resource: the server responded with a status of 404 (Not Found)
 Uncaught TypeError: Cannot read property 'client' of undefined 

我试过这样做:signalR : /signalr/hubs is not generated和它不起作用。

有什么想法吗?

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2014-10-09 08:35:56

在关于Global.asax事件的Application_Start文件中,您必须注册集线器url。

代码语言:javascript
复制
    protected void Application_Start()
    {
        RouteTable.Routes.MapHubs();
    }
票数 4
EN

Stack Overflow用户

发布于 2016-02-04 22:23:29

对我来说很管用:

转到项目中的启动类,在Configuration方法中添加以下内容:

app.MapSignalR("/signalr",新HubConfiguration());

我希望它对你有用

票数 4
EN

Stack Overflow用户

发布于 2014-11-19 10:56:14

尝试此操作,无需在事件中写入行。

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Owin;
using Microsoft.Owin;
[assembly: OwinStartup(typeof(Faceless_Books.Hubs.Startup))]

namespace Faceless_Books.Hubs
{
public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        // Any connection or hub wire up and configuration should go here
        app.MapSignalR();
    }
}
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26273766

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档