2
votes

I'm struggling at connecting my C# code to an existing SignalR hub. This hub is created in SignalR 1.0
I have a pretty straight forward situation. The hub is defined as follows:

using System; 
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR.Hubs;    

namespace POC.SignalR.WebHost.Hubs 
{
        [HubName("SignalRHub")]
        public class SignalRHub : Hub
        {
            /// <summary>
            /// Joins the group.
            /// </summary>
            /// <param name="groupname">The groupname.</param>
            public void JoinGroup(string groupname)
            {
                Groups.Add(Context.ConnectionId, groupname);
                Clients.OthersInGroup(groupname).newMemberMessage(Context.ConnectionId);
                Clients.Caller.JoinedGroup(groupname);  
            }

When I connect to the hub using Javascript it all works like a charm.

// Check if url exists and give it a default value if that's the case.
if (!url) { url = 'http://www.someurl.com/signalr'; }

conn = $.connection.SignalRHub;
var currentGroupName = '';
if (typeof groupName == "string") currentGroupName = groupName;

$.connection.hub.url = url;
$.connection.hub.logging = true;

// Start the connection
$.connection.hub.start().done(function () {
    verbosemsg(conn.connection.state);
    conn.server.connectionName();// init to get my connectioID   
    verbosemsg('Connection made now joining group:' + currentGroupName);
    if (currentGroupName != '') conn.server.joinGroup(currentGroupName);
});     

But when I connect to the hub in C# using the following code, I keep running into a "'JoinGroup' method could not be resolved." error. The hubConnection is in "connected" state and seems correct.

HubConnection hubConnection = new HubConnection("http://www.someurl.com/signalr", false);
IHubProxy hubProxy = hubConnection.CreateHubProxy("SignalRHub");
hubConnection.Start().Wait();
hubProxy.Invoke("JoinGroup", hubConnection.ConnectionId, "SignalRChatRoom").Wait();

As far as I can see I've implemented my code similar as found on this example: http://www.asp.net/signalr/overview/older-versions/signalr-1x-hubs-api-guide-net-client#establishconnection There must be something I've overlooked but I cannot figure it out. It would be great if someone can point me in the right direction.

Thx.

2
Shouldn't the url be "someurl.com/signalr/hub?" If you just launch "someurl.com/signalr" in your browser what are you seeing? Do you see the generated proxy?Gjohn
you are invoking the JoinGroup method with two parameters while the methods on the server takes just on parameter - the group name. This should work: hubProxy.Invoke("JoinGroup", "SignalRChatRoom");. Also you should not use .Wait() but await otherwise you can have deadlocks.Pawel

2 Answers

1
votes

this link can help trouble shoot a number of SignalR issues. http://www.asp.net/signalr/overview/testing-and-debugging/troubleshooting

“Exception: method could not be resolved” when client calls method on server

This error can result from using data types that cannot be discovered in a JSON payload, such as Array. The workaround is to use a data type that is discoverable by JSON, such as IList. For more information, see .NET Client unable to call hub methods with array parameters.

Also In your hub your JoinGroup is

public void JoinGroup(string groupname)

but in your client you're adding another argument, there is no JoinGroup that takes 2 arguments.

hubProxy.Invoke("JoinGroup", hubConnection.ConnectionId, "SignalRChatRoom").Wait();

// Summary:
//     Executes a method on the server side hub asynchronously.
//
// Parameters:
//   method:
//     The name of the method.
//
//   args:
//     The arguments
//
// Type parameters:
//   T:
//     The type of result returned from the hub
//
// Returns:
//     A task that represents when invocation returned.
Task<T> Invoke<T>(string method, params object[] args);
0
votes

Server

public class ChatHub : Hub
{

        public int TryAddNewUser(string userName)
        {
            //some logic...
            Clients.All.AddUserToUserList(id, userName);
            return id;
        }

        public void AddNewMessageToPage(int id, string message)
        {
            //some logic...
            Clients.All.addNewMessageToPage(u.Login, message);
        }

}

Client

$(document).ready(function () {
    //first need register client methods
    var chat = $.connection.chatHub;
    chat.client.addUserToUserList = function (id, login) {
        //client logic for add new user
    }

    chat.client.addNewMessageToPage = function (login, message) {
        //client logic for add new message from user
    }

    //second need start chat
    $.connection.hub.start().done(function () {
        chat.server.tryAddNewUser(login).done(function (id) {
            alert("Added " + id)
        });
    });

});

Note, dynamic js file must be added with the same path

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