开发者

Using SignalR with ASP.NET MVC3

I'm attempting to use SignalR hubs in an application, but am having no luck currently. I've read through Scott Hanselman's article, and also looked at this blog post which shows how to implement it in MVC, basically doing the same thing. Unfortunately, I'm having no luck. On the client side, the javascript seems to work fine [apart from nothing happening] but when I place breakpoints in the code it shows that the controller is being called, not the hub code. The code I'm using is this:

// Client side javascript:
var hooking;
$(function() {
    // Setup SignalR
    hooking = $.connection.hooking;
    hooking.removeLead = function(ref) {
        $("lead" + ref).remove();
    };
    $.connection.hub.start();
    }
});

// Hooking.cs (placed in application root)
public class Hooking : Hub
{
    public void Submit(string jsonString)
    {
        var serializer = new JavaScriptSerializer();
        var json = serializer.Deserialize<HookingLeadResult>(jsonString);
        Clients.removeLead(json.Ref); // Remove lead from client hooking windows
        // update lead gen
    }
}

When I call hooking.submit(resultJson); later in my code for some reason it calls the Index action of my Hooki开发者_C百科ngController (which is the current page). Anyone know how to correctly call the Submit function from Hooking.cs?


Did you look at the network traffic in firebug or some other tool that sniffs http traffic to make sure you have no errors? It might be something to do with routing.

Also a side note, you don't need to serialize anything we do that for you. Just send objects back and forth.

When you're in an mvc app you need to include the hub script like any other static script:

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


There were two issues with this - 1. For some reason an old version of jquery.validate was causing errors, but updating it sorted this. 2. I attempted to cast to a nullable int, which I guess isn't supported. I'm sure there are more elegant ways to solve this, but I simply cast my nullable data inputs to strings and parsed them if not null


jquery.validate.js was the culprit. For some reason it was triggering GET request, and after excluding the js file, SignalR requests started triggering POST request. Perhaps I should update it as Jordan Wallwork mentioned.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜