所以,我是SignalR的新手(本周刚刚开始玩它),但尚未开始工作。我试图在MVC 4中进行一次简单的聊天,但我已经停留了一段时间了,这是我到目前为止,任何提示或技巧都会有用!
我目前用firebug得到的错误是这样的: “TypeError:chat.send不是函数”
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.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>© @DateTime.Now.Year - My ASP.NET MVC Application</p>
</div>
</div>
</footer>
@*@RenderSection("scripts", required: false)*@
</body>
还有什么需要看的吗?如果是这样,只要打我,我会尽快回来。
无论如何,提前谢谢。 最好的问候,
答案 0 :(得分:6)
您正在执行“发送”功能。
应该是:
chat.server.send($('#msg').val());
你已经注释掉了,所以不确定你是否尝试过,但^是执行send方法的正确方法。
如果这不是您遇到的问题,请确保您可以在浏览器中看到/ signalr / hubs(验证它是否生成了javascript文件)。