Signalr send message from browser to .net client on the same machine

570 views Asked by At

We need to communicate a native application with a web application.

We think to use signalr to send the message/command.

The pipeline would be:

  • User clicks to make an action.
  • Javascript (with signalr) send a message to a server in azure.
  • The server re-send the message a specific client. It must be the client installed on the same machine.
  • Once the result is completed, NET sends the resulting reverse.

The matter is, How I can find client from the same machine in the signalr Server?

The organization in our system is:

  • There is center/gym.
  • Every center has staff who can login.

We could identify client at the same center with some file configuration. Saving our key center, for example. But, in a center, could there are more than one.NET client installed on the different computer.

We think to use the private IP of the computer to make a key on the signalr server.

var ips = [];

    var RTCPeerConnection = window.RTCPeerConnection ||
        window.webkitRTCPeerConnection || window.mozRTCPeerConnection;

    var pc = new RTCPeerConnection({
        // Don't specify any stun/turn servers, otherwise you will
        // also find your public IP addresses.
        iceServers: []
    });
    // Add a media line, this is needed to activate candidate gathering.
    pc.createDataChannel('');

    // onicecandidate is triggered whenever a candidate has been found.
    pc.onicecandidate = function (e) {
        if (!e.candidate) { // Candidate gathering completed.
            pc.close();
            console.log(ips);
            return;
        }
        var ip = /^candidate:.+ (\S+) \d+ typ/.exec(e.candidate.candidate)[1];

        ips.push(ip);
    };
    pc.createOffer(function (sdp) {
        pc.setLocalDescription(sdp);
    }, function onerror() { });

This data can be obtained in .NET client without a problem. But in javascript, the previous code works regularly. In some PC, it only returns ipv4. And in Mozilla it doesn't work.

How can we identify both clients? Do You know another way to reach the goal?

Thanks,

1

There are 1 answers

0
Serafín On BEST ANSWER

Finally, we didn't find a good solution filtering ip adress.

We did the as follow:

We used URI schema to launch our app. URI Schema windows


    Public Class RegistrarURI


        Const URI_SCHEME As String = "xxx"
        Const URI_KEY As String = "URL:xxx"
        Private Shared APP_PATH As String = Location.AssemblyDirectory() ' "C:\Program Files (x86)\xxx.exe"

        Public Shared Sub RegisterUriScheme()

            Using hkcrClass As RegistryKey = Registry.ClassesRoot.CreateSubKey(URI_SCHEME)
                hkcrClass.SetValue(Nothing, URI_KEY)
                hkcrClass.SetValue("URL Protocol", [String].Empty, RegistryValueKind.[String])

                Using defaultIcon As RegistryKey = hkcrClass.CreateSubKey("DefaultIcon")
                    Dim iconValue As String = [String].Format("""{0}"",0", APP_PATH)
                    defaultIcon.SetValue(Nothing, iconValue)
                End Using

                Using shell As RegistryKey = hkcrClass.CreateSubKey("shell")
                    Using open As RegistryKey = shell.CreateSubKey("open")
                        Using command As RegistryKey = open.CreateSubKey("command")
                            Dim cmdValue As String = [String].Format("""{0}"" ""%1""", APP_PATH)
                            command.SetValue(Nothing, cmdValue)
                        End Using
                    End Using
                End Using
            End Using
        End Sub

    End Class

In an Azure WebApp we launch a SignalR Server. This server will send data from our .NET app to Chrome.

To achive that, when the web is loaded, we connect to the signalR server. To build de uri, We send the connectionId from Javascript client to the .NET Client.

Then, when the native process is completed. .NET client send the information to signalR server, and this server mirrored the data to javacript client using the connectionId.

To avoid launch some instance of our native app, we use IPC channel to send data to one instance to the previous and closind the new one.

Link to source Blog source