2013-07-02 17 views
20

Mogę uzyskać to tutorial do pracy w nowym projekcie, ale nie w moim istniejącym projekcie.signalR:/signalr/hubs nie jest generowany

Mój projekt jest aplikacja ASP.Net MVC 4 www z następującym atrybutu w pliku web.config:

<appSettings> 
    <add key="webpages:Enabled" value="true"/> 
</appSettings> 

To dlatego, że moja aplikacja jest Single-Page-aplikacji, która wykorzystuje angularjs na po stronie klienta. Jedyna strona w mojej aplikacji jest index.cshtml, do którego dodałem odpowiedni kod dla signalR:

<!-- signalR chat --> 
<script src="~/Scripts/jquery.signalR-1.0.0.js"></script> 
<!--Reference the autogenerated SignalR hub script. --> 
<script src="/signalr/hubs"></script> 
<!--Add script to update the page and send messages.--> 
<script type="text/javascript"> 
    $(function() { 
     // Declare a proxy to reference the hub. 
     var chat = $.connection.chatHub; 
     // Create a function that the hub can call to broadcast messages. 
     chat.client.broadcastMessage = function (name, message) { 
      // Html encode display name and message. 
      var encodedName = $('<div />').text(name).html(); 
      var encodedMsg = $('<div />').text(message).html(); 
      // Add the message to the page. 
      $('#discussion').append('<li><strong>' + encodedName 
       + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>'); 
     }; 
     // Get the user name and store it to prepend to messages. 
     $('#displayname').val(prompt('Enter your name:', '')); 
     // Set initial focus to message input box. 
     $('#message').focus(); 
     // Start the connection. 
     $.connection.hub.start().done(function() { 
      $('#sendmessage').click(function() { 
       // Call the Send method on the hub. 
       chat.server.send($('#displayname').val(), $('#message').val()); 
       // Clear text box and reset focus for next comment. 
       $('#message').val('').focus(); 
      }); 
     }); 
    }); 
</script> 

Następnie Mam plik ChatHub.cs:

public class ChatHub : Hub 
{ 
    public void Send(string name, string message) 
    { 
     // Call the broadcastMessage method to update clients. 
     Clients.All.broadcastMessage(name, message); 
    } 
} 

i wreszcie na świecie.asax:

protected void Application_Start() 
    { 
     RouteTable.Routes.MapHubs(); 
     BundleConfig.RegisterBundles(BundleTable.Bundles); 
    } 

Po uruchomieniu aplikacji plik/signalr/hubs nie jest generowany. Dostaję 404 przy żądaniu plik, a on awarii na linii:

chat.client.broadcastMessage = function (name, message) { .... 

bo czat jest null, jak poprzednia linia nie znaleźliśmy chatHub:

var chat = $.connection.chatHub; 

Czy ktoś wie co się stało z moim kod ?

UPDATE

Mam rozwiązać mój problem, zmieniając linię ::

<script src="/signalr/hubs"></script> 

do

<script src="~/signalr/hubs"></script> 

Odpowiedz

15

Mam rozwiązać mój problem, zmieniając linię ::

<script src="/signalr/hubs"></script> 

do

<script src="~/signalr/hubs"></script> 
+12

BTW, '/ signalr/hubs' nie jest plikiem w projekcie. Jeśli ktokolwiek ma ochotę tracić czas na szukanie. Jeśli uruchomisz aplikację, a następnie przejdź do tego adresu URL, pokaże ci to. – toddmo

+0

@toddmo, co to znaczy, że to nie plik? , stworzyłem system powiadamiania w czasie rzeczywistym i nie wywoływał $ .connection.hub.start(). Myślę, że ~/signalr/hubs musi coś z tym zrobić, ponieważ nie ma go w rozwiązaniu nawet po dodaniu pakietu – Saurabh

+0

@toddmo, przepraszam za komentarz, ponieważ nie chcę ponownie pisać tego samego pytania, Mój katalog główny to http: // localhost: 56547/Home/Index, sugerujesz, aby przejść do http: // localhost: 56547/signalr/hubs? – Saurabh

1

miałem podobny problem, gdzie plik koncentratory nie był wygenerowany. Wygląda na to, że OP to following the steps here. Sposób, w jaki naprawiłem problem związany z jQuery obejmuje. Samouczek, który dołączyłem poniżej został napisany przy pomocy jquery 1.6.4 i jquery-signalr w wersji 2.1.0. Gdy program Visual Studio wygenerował dla mnie folder Skrypty, używał wersji jQuery 1.10.2 i jquery-signalr wersji 2.0.2.

Sposób, w jaki to naprawiłem, polegał po prostu na edycji pliku index.html. Zauważ, że możesz korzystać z okna konsoli Ctrl + Shift + J przeglądarki Chrome, aby wyświetlić błędy.

3

Powodem, dla którego/signalr/hubs nie są generowane, jest zapomnienie o Map SignalR w OWIN Startup Configuration.

public class Startup 
{ 
    public void Configuration(IAppBuilder appBuilder){ 
     ... 
     appBuilder.MapSignalR(); 
     ... 
    } 
... 
2

W moim przypadku było tak, ponieważ moja klasa ChatHub nie była oznaczona jako publiczna.

0

Chciałbym dodać, że plik signalR Readme ma jakąś uwagę na temat tego problemu. A jeśli twoja strona signalR jest w PartialView, jakiś skrypt powinien być umieszczony na stronie wzorcowej.

Please see http://go.microsoft.com/fwlink/?LinkId=272764 for more information on using SignalR. 

Upgrading from 1.x to 2.0 
------------------------- 
Please see http://go.microsoft.com/fwlink/?LinkId=320578 for more information on how to 
upgrade your SignalR 1.x application to 2.0. 

Mapping the Hubs connection 
---------------------------- 
To enable SignalR in your application, create a class called Startup with the following: 

using Microsoft.Owin; 
using Owin; 
using MyWebApplication; 

namespace MyWebApplication 
{ 
    public class Startup 
    { 
     public void Configuration(IAppBuilder app) 
     { 
      app.MapSignalR(); 
     } 
    } 
} 

Getting Started 
--------------- 
See http://www.asp.net/signalr/overview/getting-started for more information on how to get started. 

Why does ~/signalr/hubs return 404 or Why do I get a JavaScript error: 'myhub is undefined'? 
-------------------------------------------------------------------------------------------- 
This issue is generally due to a missing or invalid script reference to the auto-generated Hub JavaScript proxy at '~/signalr/hubs'. 
Please make sure that the Hub route is registered before any other routes in your application. 

In ASP.NET MVC 4 you can do the following: 

     <script src="~/signalr/hubs"></script> 

If you're writing an ASP.NET MVC 3 application, make sure that you are using Url.Content for your script references: 

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

If you're writing a regular ASP.NET application use ResolveClientUrl for your script references or register them via the ScriptManager 
using a app root relative path (starting with a '~/'): 

    <script src='<%: ResolveClientUrl("~/signalr/hubs") %>'></script> 

If the above still doesn't work, you may have an issue with routing and extensionless URLs. To fix this, ensure you have the latest 
patches installed for IIS and ASP.NET. 
0

Dla mnie rozwiązaniem było ponownie zainstalować wszystkie pakietyi przywrócić wszystkie dependecies.

Otwórz konsolę PowerShell Nuget i użyj tego polecenia.

Update-Package -Reinstall 
Powiązane problemy