0

I am trying to create a webapp which is capable of reaching client side local resources.

I.e. I am going to install exe to client's computer. I want to fire this exe's function via JavaScript code (which is on the remote server) and fire a function that connects to a serial port, reads data and returns that data to a web page.

I can write C# or Visual Basic for client side app.

I was able to get some result via XMLHttpRequest so far. I opened port 9091 with C# and able to connect that port via writing localhost:9091 to browser's address bar and get data but I wasn't able to do it with JavaScript. (Getting CORS error)

Is it possible to accomplish this task with my skill set?

Example for client side app

Imports System.Net
Imports System.Globalization
Imports System.Collections.Specialized

Module HttpListener

    Sub Main()
        Dim prefixes(0) As String
        prefixes(0) = "http://*:9091/"
        ProcessRequests(prefixes)
    End Sub

    Private Sub ProcessRequests(ByVal prefixes() As String)
        If Not System.Net.HttpListener.IsSupported Then
            Console.WriteLine( _
                "Windows XP SP2, Server 2003, or higher is required to " & _
                "use the HttpListener class.")
            Exit Sub
        End If

        ' URI prefixes are required,
        If prefixes Is Nothing OrElse prefixes.Length = 0 Then
            Throw New ArgumentException("prefixes")
        End If

        ' Create a listener and add the prefixes.
        Dim listener As System.Net.HttpListener = _
            New System.Net.HttpListener()
        For Each s As String In prefixes
            listener.Prefixes.Add(s)
        Next

        Try
            ' Start the listener to begin listening for requests.
            listener.Start()
            Console.WriteLine("Listening...")

            ' Set the number of requests this application will handle.
            Dim numRequestsToBeHandled As Integer = 10

            For i As Integer = 0 To numRequestsToBeHandled
                Dim response As HttpListenerResponse = Nothing
                Try
                    ' Note: GetContext blocks while waiting for a request.

                    Dim context As HttpListenerContext = listener.GetContext()

                    ' Create the response.
                    response = context.Response

                    Dim IncMessage As New NameValueCollection
                    IncMessage = context.Request.QueryString

                    Dim Field As String
                    Dim FValue As String
                    Dim Values() As String

                    For Each key As String In IncMessage.Keys
                        Values = IncMessage.GetValues(key)
                        Field = key
                        For Each value In Values
                            FValue = value

                            MsgBox(Field & " equals " & FValue)
                        Next value
                    Next key


                Catch ex As HttpListenerException
                    Console.WriteLine(ex.Message)
                Finally
                    If response IsNot Nothing Then
                        response.Close()
                    End If
                End Try
            Next
        Catch ex As HttpListenerException
            Console.WriteLine(ex.Message)
        Finally
            ' Stop listening for requests.
            listener.Close()
            Console.WriteLine("Done Listening...")
        End Try
    End Sub

End Module

6
  • 2
    soo you think it's a secure thing to do for a browser to let any arbitrary website run arbitrary binary files on the clients computer? what you gotta do is write a browser plugin. or let your program directly communicate with the server. Commented Nov 19, 2021 at 15:14
  • "javascript which is on remote server", yes, it may be loaded from the remote server but it's executed in the browsers context. And no, you cannot access ANY of the local resources from javascript within the browser. Especially you cannot just start some random executables ... That would be a HUGE security hole ... Commented Nov 19, 2021 at 15:16
  • javascript code (which is in remote server)...unless you mean nodeJS then the Javascript runs in the browser, not the server. But in either case you cannot make a web app execute a local script. As others have said, this would be a total security disaster. There's a reason we don't have things like ActiveX any more! Commented Nov 19, 2021 at 15:54
  • You could however register a custom protocol in the O/S, and get the user to click on a link in the web app which then launches the desktop application - much like the links which are used to open Zoom and Teams calls on your desktop, and other similar things. Commented Nov 19, 2021 at 15:55
  • Why not just write an application that communicates with the same server as the web server via HTTP web services (REST or whatever). The application sends the data to the service. The web server (or the client) gets the data from the service. Is there a specific need for the real-time communication that you believe you need? This seems like a solution in search of a problem. Commented Nov 20, 2021 at 21:01

1 Answer 1

0

Technically, you could write a standalone application which is served as download via your server, tell user to download it and run it, open some kind of local http server, connect to it from script on your page via localhost:someport, do the required work in that standalone application and return result back to server but it requires few things:

  1. User must trust you to download and run the application
  2. His account must have privileges to do so, including opening socket so your script can connect to it
  3. He needs to have all prerequisites ready (.Net framework, etc.) so your application can work

As you can see, it's not trivial but it can be done, we had a similar situation with web POS application which used local client resources (serial port) to print invoice. End user machines were under our control (we preinstalled application which communicates with printer) but concept is the same.

Sign up to request clarification or add additional context in comments.

1 Comment

This exactly what I am trying to do. Can you provide me some simple example (for both side)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.