所以,我对 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>© @DateTime.Now.Year - My ASP.NET MVC Application</p>
</div>
</div>
</footer>
@*@RenderSection("scripts", required: false)*@
</body>
你还有什么需要看的吗?如果是这样就打我,我会尽快回来。
无论如何,提前谢谢。此致,