更新4:(已解决)
- 做了新项目;
- 进口来源;
- 将 SignalR 更新到最新版本(通过 NuGet 包管理器控制台);
- 做了相应的修改(根据官方文档:http ://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-server )。
最后,一切都像魅力一样运作。感谢 SignalR 团队!
这个 SignalR 做得很好,迫不及待想试试!
我找到了本教程 - SignalR - 5 分钟演示 ( http://www.youtube.com/watch?v=tEBaDo_sFfA )
但是,我有一些问题: - 我安装了 VS2012Ultimate,在创建示例 ASP.NET MVC4 Internet 应用程序后,右键单击参考 -> 管理 NuGet 包,搜索 SignalR .... 没有像视频中那样出现,没有那些:SignalR、SignalR.Js、SignalR.Server ... :( 有谁知道为什么?
UPDATE1:我安装了 SignalR(http://nuget.org/packages/microsoft.aspnet.signalr),之后,我安装了 asp.net 秋季更新(http://www.microsoft.com/en-us/download /details.aspx?id=35493)。
但是我从下面得到关于函数“ writeMessage
”的错误ChatHub.cs
。
"Error1'Microsoft.AspNet.SignalR.Hubs.HubConnectionContext' does not contain a definition for 'writeMessage'
and no extension method 'writeMessage' accepting a first argument of type
'Microsoft.AspNet.SignalR.Hubs.HubConnectionContext' could be found (are you missing a using directive or an
assembly reference? ..\MvcApplication1\Hubs\ChatHub.cs"
Clients.All.
在聊天初始化和使用之后使用orClients.Others.
和启动集线器“解决”此错误后chat.writeMessage
,在 GoogleChrome 控制台中加载时出现两个错误:
在Index.cshtml中:
"Uncaught TypeError: Cannot set property 'writeMessage' of undefined"
在localhost:3420/signalr/hubs:
"Uncaught TypeError: Object #<Object> has no method 'createProxy'"
在这条线上:
// Create and store the hub proxy hub._.proxy = hubConnection.createProxy(hub._.hubName);
Clients.All.
在使用orClients.Others.
并在最后启动集线器并使用chat.client.writeMessage
(我安装了 Microsoft.AspNet.SignalR.Client)“解决”此错误后,在 GoogleChrome 控制台中加载时出现两个错误:
在Index.cshtml中:
"Uncaught TypeError: Cannot read property 'client' of undefined"
任何人都可以帮助我吗?:(
提前致谢!!
文件 ChatHub.cs
:
namespace MvcApplication1.Hubs
{
//[Microsoft.AspNet.SignalR.Hubs.HubName("chathub")]
[Microsoft.AspNet.SignalR.Hubs.HubName("chatHub")]
public class ChatHub : Microsoft.AspNet.SignalR.Hubs.Hub
{
public void BroadcastMessage(string message)
{
//rebroadcast the message to all the connected clients
Clients.writeMessage(message);//ERROR on compile
//I tried both of the followings, give no error, BUT when i press Submit doesn't appear messages anywhere
//Clients.All.writeMessage(message);
//Clients.Others.writeMessage(message);
}
}
}
文件 Index.cshtml
:
@section scripts
{
<script type="text/javascript" src="~/Scripts/jquery.signalR-1.0.0-alpha2.js"></script>
@*not working, GET localhost:3420/SignalR 500 (Internal Server Error)*@
@*<script type="text/javascript" src="SignalR"></script>*@
@* with any of these declarations i don't get 500 error any more, but still not working *@
<script type="text/javascript" src="~/signalr/hubs"></script>
@*<script type="text/javascript" src="../signalr/hubs"></script>*@
<script type="text/javascript">
$(document).ready(function()
{
//i tried these, but in vain..
//var connection = new Microsoft.AspNet.SignalR.Connection("localhost:3420/");
//$.connection.hub.url = 'localhost:3420/signalr/';
var chat = $.connection.chatHub;
$.connection.hub.start();
//Uncaught TypeError: Cannot set property 'writeMessage' of undefined
//chat.writeMessage = function (msg)
//Uncaught TypeError: Cannot read property 'client' of undefined
chat.client.writeMessage = function (msg) {
{
$("#messages").append("<li>" + msg + "</li>");
}
$("#buttonSubmit").click(function ()
{
//chat.broadcastMessage($("#txtInput").val());
chat.server.broadcastMessage($("#txtInput").val());
});
$.connection.hub.start();//just one error at client
});
</script>
}
<h5>Chat with SignalR</h5>
<fieldset>
<legend>SignalR Demo</legend>
<label for="txtInput">Chat Message</label>
<input id="txtInput" type="text" />
<button id="buttonSubmit">Submit</button>
<fieldset>
<legend>Messages</legend>
<ul id="messages"></ul>
</fieldset>
</fieldset>
更新2:
根据Tim B James的建议,我:
- 创建了新的 ASP.NET MVC 4 项目;
- 使用建议的命令通过 NuGet 安装 SignalR;
- 尝试使用来自https://github.com/SignalR/SignalR/wiki/QuickStart-Hubs的集线器的源代码示例,但无法正常工作!
提及:
- 将函数中的代码Main
放入我的HomeController.cs
-> 函数中:public ActionResult Index()
;
- 改变了这一行:
var hubConnection = new HubConnection("localhost/mysite");
有了这个:
var hubConnection = new HubConnection("localhost:14079");
但是,我的页面没有加载(“等待本地主机...”)
- 如果我不将 Main 中的代码放入控制器的函数中,
我收到此错误:
"Uncaught TypeError: Cannot read property 'chat' of undefined "
on this line:
var chat = $.connection.chat;
更新3:
UPDATE2 与删除线线条
更新4:(已解决)
开头的暗示。