2

所以,我对 SignalR 很陌生(这周才开始玩它)但还没有让它工作。我试图在 MVC 4 中进行简单的聊天,但我已经被困了一段时间,这就是我到目前为止所拥有的,任何提示或提示都会很有用!

我目前使用 firebug 得到的错误是:“TypeError:chat.send 不是函数”

启动.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using Owin;

namespace SignalRWebChatVer2
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var config = new HubConfiguration
                {
                    EnableCrossDomain = true
                };
            app.MapHubs(config);
        }
    }
}

聊天中心.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;

namespace SignalRWebChatVer2
{
    [HubName("ChatHub")]
    public class ChatHub : Hub
    {
        public void Send(string messages)
        {
            Clients.All.addMessage(messages);
        }
    }
}

聊天.cshtml

@{
ViewBag.Title = "Chat";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Chat</h2>
<div>
    <input type="text" id="msg"/>
    <input type="button" id="broadcast" value="Send"/>
    <ul id="messages"></ul>
</div>

_Layout.csthml

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>@ViewBag.Title - My ASP.NET MVC Application</title>
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <meta name="viewport" content="width=device-width" />

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script src="@Url.Content("~/Scripts/jquery.signalR-2.0.0-beta2.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/signalr/hubs")" type="text/javascript"></script>

</head>
<body>
    <script type="text/javascript">
        $(function() {
            var chat = $.connection.chatHub;

            //$.connection.hub.start();
            chat.client.addMessage = function(messages) {
                $("#messages").append('<li>' + messages + '</li>');
            };

            $.connection.hub.start().done(function() {
                $("#broadcast").click(function() {
                    //var message = $('#msg').val();
                    chat.send($('#msg').val());
                    //chat.server.send(message);
                });
            });
        });
    </script>
    <header>
        <div class="content-wrapper">
            <div class="float-left">
                <p class="site-title">@Html.ActionLink("your logo here", "Index", "Home")</p>
            </div>
            <div class="float-right">
                <section id="login">
                    @Html.Partial("_LoginPartial")
                </section>
                <nav>
                    <ul id="menu">
                        <li>@Html.ActionLink("Home", "Index", "Home")</li>
                        <li>@Html.ActionLink("About", "About", "Home")</li>
                        <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                        <li>@Html.ActionLink("Chat", "Chat", "Home")</li>
                    </ul>
                </nav>
            </div>
        </div>
    </header>
    <div id="body">
        @RenderSection("featured", required: false)
        <section class="content-wrapper main-content clear-fix">
            @RenderBody()
        </section>
    </div>
    <footer>
        <div class="content-wrapper">
            <div class="float-left">
                <p>&copy; @DateTime.Now.Year - My ASP.NET MVC Application</p>
            </div>
        </div>
    </footer>
    @*@RenderSection("scripts", required: false)*@
</body>

你还有什么需要看的吗?如果是这样就打我,我会尽快回来。

无论如何,提前谢谢。此致,

4

1 回答 1

6

您正在错误地执行“发送”功能。

它应该是:

chat.server.send($('#msg').val());

您已将其注释掉,因此不确定您是否尝试过,但 ^ 是执行发送方法的正确方法。

如果这不是您遇到的问题,请确保您可以在浏览器中看到您的 /signalr/hubs(验证它是否生成了一个 javascript 文件)。

于 2013-08-23T16:34:04.273 回答