How to connect to a SignalR self-host wpf app remotely?

Viewed 32

I am trying to connect to my self-hosted SignalR server remotely using a web-based client. I can successfully connect to the server locally, but I cannot connect to the server remotely.

When I type (self-hosted ip4 address):7777/signalr/hubs in the URL of my remote browser, I see the generated hubs file. Therefore, I believe that I'm making a connection. But I want to see the web page. If you couldn't tell already, I'm a beginner to SignalR

2 more questions:

  1. What URL should I type into the other computer's web browser to connect to my server?
  2. What code determines what that URL is?

Here's my code

The self-hosted SignalR server (a wpf application)

public partial class MainWindow : Window
{
    IDisposable SignalR;
    private string url = "http://*:7777";

    public MainWindow()
    {
        InitializeComponent();

        SignalR = WebApp.Start(url);
        Console.WriteLine("Server running on {0}", url);
    }
}

Startup class

class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseCors(CorsOptions.AllowAll);
        app.MapSignalR();
    }
}

Index Page (scripts)

@section scripts
{
    <!--Reference the jQuery library. -->
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <!--Reference the SignalR library. -->
    <script src="~/Scripts/jquery.signalR-2.4.3.min.js"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script src="http://localhost:7777/signalr/hubs"></script>
    <!--Add script to update the page and send messages.-->
    <script src="~/MyScripts/Main.js"></script>
}

My js file

$(function () {
    $.connection.hub.url = "http://localhost:7777/signalr";

    var hub = $.connection.mainHub;

    $.connection.hub.logging = true;
    $.connection.hub.log("Trying to connect...");


    // Start connection
    $.connection.hub.start()
        .done(function () {
            $.connection.hub.log('Now connected, connection ID=' + $.connection.hub.id);
            });
        })
        .fail(function () {
            $.connection.hub.log("Error!");
        });
});
1 Answers

I figured out how to connect remotely to my self-hosted server. I changed localhost with my ip4 address (192.168.0.183) in the js and index script section

My new code

$.connection.hub.url = "http://192.168.0.183:7777/signalr";

and

<script src="http://192.168.0.183:7777/signalr/hubs"></script>

Finally, I had to edit my applicationhost.config file located at

  • /{project folder}/.vs/{solution name}/config/applicationhost.config

In the binding protocol section, I changed localhost after the port to my ip4 address

New code

<bindings>
    <binding protocol="http" bindingInformation="*:58038:192.168.0.183" />
    <binding protocol="https" bindingInformation="*:44389:192.168.0.183" />
</bindings>

This at least allows me to connect to the http url (doesn't work with https in my experience so far)

Related