简体   繁体   中英

SignalR - StartUp

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.

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.

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>

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)

将脚本标记更改为:

<script type="text/javascript" src='<%= ResolveClientUrl("~/signalr/hubs") %>'></script>

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM