最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - TypeError: chat.send is not a function - Stack Overflow

programmeradmin1浏览0评论

So, i am pretty new to SignalR(just started playing with it this week) but havent got it to work yet. Im trying to get a simple chat working in MVC 4 but ive been stuck for some time now, this is what i have so far, any hints or tips would be useful!

The error i currently get with firebug is this: "TypeError: chat.send is not a function"

Startup.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);
        }
    }
}

ChatHub.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);
        }
    }
}

Chat.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/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>

Is there anything more you need to see? If so just hit me up and i will get back ASAP.

Anyway, thanks in advance. Best regards,

So, i am pretty new to SignalR(just started playing with it this week) but havent got it to work yet. Im trying to get a simple chat working in MVC 4 but ive been stuck for some time now, this is what i have so far, any hints or tips would be useful!

The error i currently get with firebug is this: "TypeError: chat.send is not a function"

Startup.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);
        }
    }
}

ChatHub.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);
        }
    }
}

Chat.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./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>

Is there anything more you need to see? If so just hit me up and i will get back ASAP.

Anyway, thanks in advance. Best regards,

Share Improve this question edited Aug 23, 2013 at 13:56 M K asked Aug 23, 2013 at 11:50 M KM K 251 silver badge6 bronze badges 8
  • You are using javascript. And you are trying to call it using javascript which is not possible. – Nisarg Shah Commented Aug 23, 2013 at 11:54
  • You need to make either AJAX call – Nisarg Shah Commented Aug 23, 2013 at 11:54
  • Hmm okay, thank you. Will do a few searches and see what i find thank you. – M K Commented Aug 23, 2013 at 12:03
  • possible duplicate of Simple example of signalR not working – metadings Commented Aug 23, 2013 at 12:08
  • metadings: I have tried the fixes on that thread, adding the [HubName("ChatHub")], and moving $.connection.hub.start(); up just below var chat = $.connection.chatHub; . I tried now by also adding chat.server.send and chat.client.addMessage, it removed the error "TypeError: chat.send is not a function". But now nothing happens, not even an error. [EDIT: upon another try i now recieved this in firebug: "Error: SignalR: Error loading hubs. Ensure your hubs reference is correct, e.g. <script src='/signalr/js'></script>." – M K Commented Aug 23, 2013 at 12:23
 |  Show 3 more ments

1 Answer 1

Reset to default 6

You're executing the "send" function incorrectly.

It should be:

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

You have that mented out so not sure if you tried it but ^ is the correct way how to execute the send method.

IF that's not the issue you're encountering ensure that you can see your /signalr/hubs in the browser (verify that it generates a javascript file).

发布评论

评论列表(0)

  1. 暂无评论