可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I tried installing SignalR library to create a sample chat application. I believe I have followed all steps given in documentation. I am not sure what could be a reason of failing.
It is failing when it creates a chat object. I am using VS2010 and I downloaded SignalR using VS2010 package download utility.
Is anyone had an issue with this?
Thanks,
Samir
Thanks Hurricanepkt for helping me out.
Yes, I did get all signalR via nuget, using VS2010 'Add Library Package' dialog box. I was getting object undefined error, at var chat = $.connection.chat;
I just made it work but it was ASP.NET Web Application Project. I could not make it work with ASP.NET Website project. I don't know why.
I believe it due to dynamic dll creation in Website Project vs. fixed dll in ASP.NET Web Application Project.
Have you encounter such issue?
回答1:
What's the name of your hub? If you've changed it to something other than "Chat" then it won't work. I had the same issue because I changed mine to:
public class ChatHub : Hub
{
public void Send(string message)
{
Clients.addMessage(message);
}
}
Within the Javascript this:
var chat = $.connection.chat;
needed to change to
var chat = $.connection.chatHub;
回答2:
I was frustrated because of the same problem.
It works in Web Project but in Web site it's not. So I checked the script file which is dynamically created.
Left: Web Project - Right: Web Site (http://i.imgur.com/X1XrT.jpg)
As you can see in web site it is not creating "chat" object so it says undefined.
After reading your sentences about dynamic dll creation, I put my code behind file in a seperate .cs file and I put that cs file in App_Code folder. I tried and bam, it worked. Checked the dynamic script file:
(http://i.imgur.com/CSInO.jpg)
I don't know much about the technical issue in here, but putting your code in a seperete Class file which is located in App_Code folder resolves the issue..
Have a nice day
回答3:
Reading your problem, I ran into this same issue when using it in an MVC3 application.
Can you post your script reference? I can almost bet that you're using something like this, a static location string:
"../script/signalr.min.js"
When you should be using (or the WebForms equivalent of relative paths):
<!-- Used for SignalR -->
<script src="@Url.Content("~/Scripts/jquery-1.6.2.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.signalR.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/signalr/hubs")" type="text/javascript"></script>
I ran into this same exact issue today and David Fowler himself helped me.
In any case, read my blog post and follow it to a T and you'll have a working version with MVC3.
http://sergiotapia.com/2011/09/signalr-with-mvc3-chat-app-build-asynchronous-real-time-persistant-connection-websites/
回答4:
Well I suspect that we have followed the same documentation/tutorial - tutorial for signalR 1.1.3 using ASP.net Web Application(Latest signalR version for .NET framework 4 - higher versions of signalR is not supported).
If you are like me, you should also run signalR in web application without any problem, and then proceed in implementing the same for the website. In my case, it is all about referencing my JavaScript files, thanks to Lars Höppner.
These lines
<script src="/Scripts/jquery-1.6.4.min.js" ></script>
<script src="/Scripts/jquery.signalR-1.1.3.js"></script>
<script src="/signalr/hubs"></script>
should only be
<script src="Scripts/jquery-1.6.4.min.js" ></script>
<script src="Scripts/jquery.signalR-1.1.3.js"></script>
<script src="signalr/hubs"></script>