13

I am looking for the quick and dirty answer. I am just blanking, and after staring at a screen for over 12 hours now, I think I am shot.

I want to do a simple SignalR application as a tutorial. I found this example, but I keep getting the error that tickets is undefined. I have found a couple more, and keep getting the same error. I compared the sample project to mine and I cannot find any descrepencies.

It seems as though $.connection.ticketHub or $.connection.chat are returning undefined objects. Is there something special that I need to do with /signalr/hubs? Any other thoughts are greatly appreciated.

bdparrish
  • 3,216
  • 3
  • 37
  • 58

5 Answers5

14

You need to add the /signalr/hubs to the page, it's a javascript dynamically generated by SignalR containing method stubs for your hubs and the methods on the hubs.

So if you have a .NET-hub named TestHub, with a method called SendMessage(string message) javascript will be generated so you can from JavaScript call: $.connection.testHub.sendMessage("some message to server");

Point your browser to the url: /signalr/hubs, and you should get a javascript.

about 150 lines down you will see the ticketHub stub:

$.extend(signalR, {
   ticketHub: {
      _: {
          hubName: 'YourNameSpace.TicketHub',
          ignoreMembers: ['someMethod', 'namespace', 'ignoreMembers', 'callbacks'],
          connection: function () { return signalR.hub; }
      },

You can use Mozilla Firebug plugin or Chrome developer tools (wrench-icon->Tools->Developer Tools) to see what's sent to and returned from server.

EDIT: There was a bug in SignalR preventing /signalr/hubs to be correctly generated (it didn't generate the method stubs). https://github.com/SignalR/SignalR/issues/134

EDIT2: you could have a incorrect script tag, try:

<script src="@Url.Content("~/signalr/hubs")" type="text/javascript"></script>

or you haven't referenced the SignalR.AspNet.dll assembly. If I recall correctly it's that assembly that wires up the route to /signalr.

MatteKarla
  • 2,707
  • 1
  • 20
  • 29
  • yes, you are correct, I am getting a 404 from the /signalr/hubs request. the issue you pointed me to showed a fix, but downloading the project file and building it did not fix the issue. – bdparrish Jan 20 '12 at 02:00
  • I am now getting this after adding in `SignalR.AspNet.dll` ... `Could not load type 'SignalR.IJsonSerializer' from assembly 'SignalR, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.` – bdparrish Jan 20 '12 at 14:49
  • Have you added the `SignalR.dll` assembly? Those are the required assemblies. – MatteKarla Jan 20 '12 at 14:55
  • 3
    yes i have, does NuGet have the updated package for SignalR? It only installs `SignalR.dll` and NOT `SignalR.AspNet.dll`, so adding that in gives me the error. Is there something else NuGet is not installing that I would need? Or is installing the wrong dll for `SignalR.dll`? – bdparrish Jan 20 '12 at 15:05
  • Try the latest from GitHub (https://github.com/SignalR/SignalR). You would probably have to change ClientId to ConnectionId in your code if you are using it. – MatteKarla Jan 20 '12 at 15:27
  • Where would that change take place? – bdparrish Jan 20 '12 at 16:08
  • The Hub has a Context, and that context has a property previously called `ClientId`, which is now called `ConectionId` (replace `Context.ClientId` with `Context.ConnectionId` if you used it) – MatteKarla Jan 24 '12 at 11:24
  • I am not using this anywhere. – bdparrish Jan 24 '12 at 15:52
3

Lee Smiths answer was right on the money. I got the latest json2 from nuget and also make sure that you have the change in your web.config from the the signalR faq site:

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
ren
  • 31
  • 1
3

Are you using IE8?

If you are, make sure you include a Json2 script ref above the signalR script:

<script src="../../Scripts/json2.min.js" type="text/javascript"></script>

(Get Json2 from Nuget)

Lee Smith
  • 6,339
  • 6
  • 27
  • 34
2

Change your script tag to this :

<script type="text/javascript" src='<%= ResolveClientUrl("~/signalr/hubs") %>'></script>
dhyabi
  • 329
  • 2
  • 13
2

With Visual Studio 2010 / MVC 3 / Firefox Browser , my Index.cshtml is using SignalR just fine with the following

<script src="@Url.Content("~/Scripts/jquery-1.8.0.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.signalR-0.5.2.min.js")" type="text/javascript"></script>

<script src="/signalr/hubs" type="text/javascript"></script>

I had another issue, but I did not have to add in the @Url.Content to the signalr/hubs For simplicity on testing I also just have my class file in root of Controllers folder. I also have the following from Nuget

SignalR / SignalR.Js / SignalR.Server / SignalR.Hosting.AspNet / SignalR.Hosting.Common

(Not that you NEED all of those dependencies, but that is what I have)

Hope that helps someone...

Tom Stickel
  • 19,633
  • 6
  • 111
  • 113