Object undefined error at var chat = $.connection.chat; while using SignalR - asp.net

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?

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;

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

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/

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>

Related

Updating and Compiling CSS Files in Xamarin

I've recently started using Xamarin/MonoDevelop as an alternative to Microsoft Visual Studio and my first project was to create a website using ASP.NET with MVC pattern. This has been going well, and I don't mind the extra code work, but I'm having a rough time with the CSS.
For whatever reason, any CSS file only seems to compile once, ignoring any changes I write to the file. From my research, this because content files are cached for the build.
Is there a method in Xamorin's files, Microsoft's aspnet and web packages, or Newtonsoft's or Razor's library? If so, in which part of my solution do I call it?
Is there an add-on to Xamarin? How do I use it?
Do I have to modify a config file?
As pointed out in the comments, the issue was being caused by the browser cache not refreshing with changes to the build. The solution to this is to append a version to the end of the path name for the stylesheet. Since I am using cshtml, I decided to use this:
<head>
#{
string version = "?v=2";
string stylePathSite = "../../Content/Styles/SiteStyles.css" +
version;
string stylePathNav = "../../Content/Styles/NavBarStyles.css" +
version;
}
<link rel="stylesheet" href="#stylePathSite"/>
<link rel="stylesheet" href="#stylePathNav"/>
</head>
I can later write a controller to be update the version variable from my model. Hope any fellow newbies out there find this helpful.

SignalR is not loaded. Please ensure jquery.sigalR-x.js is referenced before ~/signalr/js

I have referenced below two libraries dynamically in my page. When I browse my application below mentioned error did not come in Chrome browser. But in Internet Explorer the error occurs.
var signalRLibrary = document.createElement('script');
signalRLibrary.type = "text/javascript";
signalRLibrary.src = 'jquery.signalR-2.1.2.min.js';
document.getElementsByTagName('head')[0].appendChild(signalRLibrary);
var signlaRHub = document.createElement('script');
signlaRHub.type = "text/javascript";
signlaRHub.src = "~/signalr/hubs";
document.getElementsByTagName('head')[0].appendChild(signlaRHub);
During page load I got below error as SignalR is not loaded. Please ensure jquery.signalR-x.js is referenced before ~/signalr/js.
if (typeof ($.signalR) !== "function") {
throw new Error("SignalR: SignalR is not loaded. Please ensure jquery.signalR-x.js is referenced before ~/signalr/js.");
}
A JavaScript client requires references to jQuery and the SignalR core
JavaScript file. The jQuery version must be 1.6.4 or major later
versions, such as 1.7.2, 1.8.2, or 1.9.1. If you decide to use the
generated proxy, you also need a reference to the SignalR generated
proxy JavaScript file. The following example shows what the references
might look like in an HTML page that uses the generated proxy.
<script src="Scripts/jquery-1.10.2.min.js"></script>
<script src="Scripts/jquery.signalR-2.1.0.min.js"></script>
<script src="signalr/hubs"></script>
These references must be included in this order: jQuery first, SignalR
core after that, and SignalR proxies last.
- from ASP.NET SignalR Hubs API Guide - JavaScript Client
Your problem is that, somehow, the order in what you are loading the references is wrong.
100% working if you want to debug on locally.
how to debug SingalR locally?
Answer : on cshtml page drag and drop js jquery.signalR-2.4.2.js in this way.Don't use min.js for debug and use this version for locally debug mode.
<script src="~/Scripts/jquery.signalR-2.4.2.js"></script>
<script src="#Url.Content("http://localhost:57305/signalr/hubs")"></script>
Download all supported Signal R js from cdn.
https://www.cdnpkg.com/signalr.js/file/jquery.signalR.min.js/

SignalR $.connection not defined, intermittent successful definition on some PCs

I have a MVC5 project using signalr to perform updates to a list. Inside script.js, there is a variable link to defined as var link = $.connection.hub. There is also a bundleconfig file to load the dependent scripts, signalr/jquery and jquery in the proper order of course.
The script /signalr/hubs is loaded in from the html manually.
bundles.Add(new ScriptBundle("~/bundles/signalr").Include(
"~/Scripts/script.js",
"~/Scripts/jquery.signalR-2.0.2.min.js"));
When the VS 2013 Web project is run, it works fine until the project is copied over to another PC that it does not work as intended. The variable link is undefined as $.connection is undefined too ($ is defined though).
I presume that jquery.signalR-2.0.2.min.js is not read or executed at all.
Questions:
Might this have anything to do with the environment of the PCs? (Windows 7 is OK, Windows 8 is not OK)
What could prevent jquery.signalR-2.0.2.min.js from running?
Does creating a new View/Controller play a part in the error (creating a view, using a layout page)?
Please see similar question: SignalR and MVC bundle as it shows how to add SignalR into an MVC bundle. I think their answer should help solve your problem.
I would comment instead of answer, but I don't have enough reputation.

Error in running example application, Uncaught Error: SignalR: jQuery not found

I am new to asp.net. I am learning SignalR, going through this tutorial. I have created everything correctly as mentioned in the tutorial. Now when I ran the Console Application, ERROR --> "One or more errors occurred." at hubConnection.Start().Wait();.(the console application is still running). Anyhow I tried even running the Web application which is not giving any error but the button broadcast doesnt do any action(I think it should). When I checked the code in Web application, I saw that one of the script source has green scribbles. Here is the Image.
Please explain what I am doing wrong..
Edit: I am using ASP.NET Development server.
Edit2: The errors I get in Google Chrome Console are:
Uncaught Error: SignalR: jQuery not found. Please ensure jQuery is referenced before the SignalR.js file. jquery.signalR-1.0.0-alpha2.min.js:10
Uncaught TypeError: Cannot read property 'signalR' of undefined hubs:17
Uncaught ReferenceError: $ is not defined
Answer: I just added reference of jQuery in my code above all the scripts and the "file not found" error is careless because it is running on server side.
The Link has also been updated with reference to jQuery
Just for anyone else who comes across this problem, I made the mistake of just copying the code from the tutorial and updating to the latest version of SignalR. The version had obviously changed and this needs to be updated in the script references i.e.
The jQuery src should be (depending on the version of signalR you have installed):
<script src="Scripts/jquery.signalR-1.1.3.js"></script>
The same thing would obviously apply to the standard jQuery library.
The error message describes fully what the problem is:
Uncaught Error: SignalR: jQuery not found. Please ensure jQuery is referenced before the SignalR.js file.
You need to include jQuery.
try "signalr/hubs" instead of "/signalr/hubs"
If the jQuery version is defined with npm with a *, e.g: "jquery": "*", in package.json and that version get's upgraded you might also see an issue very similar to this.
So downgrade the version of jQuery: "jquery": "2.2.4",
Today, I upgraded jQuery and SignalR to 2.2.0 and jQuery could not loading although I added jQuery before SignalR.
And, I downgraded jQuery as 2.1.4 system working well.
<script type="text/javascript" src="Scripts/jquery-2.1.4.js"></script>
<script type="text/javascript" src="Scripts/jquery.signalR-2.2.0.js"></script>

ASP.NET Sound Resource not publishing

So I created an ASP.NET 4 application in VS2010, that needs to play sound to the end user, and it is working perfectly in my local development environment. The problem is the sound resource nor the Resources.resx is not being published to the server. Any idea why?
What I did:
1) Under Project  Properties  Recources I added my sound resource called: soundbyte (containing soundbyte.wav). I noticed this creates a Resource folder with the wav file and under my project a Resources.resx file referencing the file
2) In my code I play the file as follows:
Dim audioFile = My.Resources. soundbyte
Dim player = New Media.SoundPlayer(audioFile)
player.Load()
player.Play()
In the Visual Studio Solution Explorer right-click on Resources.resx and select Properties. Build Action. Set to content.
EDIT: The following resource might also help.
http://blog.andreloker.de/post/2010/07/02/Visual-Studio-default-build-action-for-non-default-file-types.aspx
Ultimately, I found a way to play the sound to the client browser (as opposed to the server the asp app is running on) was to follow the techniques in this example: http://www.vbdotnetheaven.com/UploadFile/scottlysle/PlaySoundsInASPX09032006083212AM/PlaySoundsInASPX.aspx
But I found an even better way in my case was to use Javascript, which doesnt' require the Resources technique.
simply embed the sound on the page after the tag:
<embed src="Sounds/jump.wav" autostart=false width=1 height=1 id="sound1" enablejavascript="true">
Then in javascript setup the function:
function EvalSound(soundobj) {
var thissound=document.getElementById(soundobj);
thissound.Play();
}
Finally play the sound in the browser as needed in Javascript:
EvalSound('sound1');

Resources