简体   繁体   中英

SignalR Client connection issues? Can't connect to path with HubConnection()

I've been trying to look in the wiki about doing this, I'm following it, but I seem to be missing something?

So here is where I am at:

I have a client side JS that works fine locally. I now want to send something via an API to update the client side version.I should use SignalR Client right?

This is what i have:

    var connection = new HubConnection("http://localhost/test/echo", useDefaultUrl: false);

Global.asax

 RouteTable.Routes.MapConnection<MyConnection>("echo", "echo/{*operation}");

I'm getting an error along the lines of no cancellation token is declared.... Is it that I'm not hitting my HubConnection page?

If you need me to clarify let me know. Thanks,

UPDATE:

Thanks for replying! I'm still uncertain on how a Hub can talk to a persistent connection?

This is what I have so far...

namespace ConnectionHubDemo{

 public class ChatHub : Hub
{
    public void SendMessage(string message)
    {
        Clients.NewMessage(message);
    }
}
public class ConnectionHub
{
    public string test(string data)
    {
        //Will this talk to my PersistentConnection?
        var connection = new HubConnection("http://localhost/test", false);
        var myHub = connection.CreateProxy("ConnectionHubDemo.ServiceHub");
         //How would I send a message to my persisten connection?
        //myHub...
        //If succcessful bla bla bla
        return data;
    }
}

}

That's because you're not using Hubs. You're mixing Hubs and PersistentConnections. On the server Hubs are automatically routed so there's no need to map anything (see https://github.com/SignalR/SignalR/wiki/Hubs ). From the wiki:

Unlike low level PersistentConnections, there's no need to specify a route for the hub as they are automatically accessible over a special url (/signalr). This url is configurable:

To make the client side work you just declare a HubConnection with the root url (see https://github.com/SignalR/SignalR/wiki/SignalR-Client-Hubs ). Again from the documentation:

To connect to a hub using SignalR, create a HubConnection with the appropriate url. NOTE: This url will not point to a specific connection. But will instead point to the root of your site. Example

var connection = new HubConnection("http://mysite/");

So in your case, this would be:

var connection = new HubConnection("http://localhost/test");

Hope this helps.

UPDATE

Hubs DO NOT talk to persistent connections. All you have to do is follow the documentation. My answer above shows how to use Hubs on the server and on the client.

If you want to use Persistent connections then look at the documentation https://github.com/SignalR/SignalR/wiki/PersistentConnection (Server) and https://github.com/SignalR/SignalR/wiki/SignalR-Client (Client).

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