Is there a way to do a tcp connection to an IP with javascript?

前端 未结 3 407
名媛妹妹
名媛妹妹 2021-01-13 03:13

Let me give a little background on what I am trying to accomplish.

I have a device(chip and pin Terminal) that has a local IP address, It has been programmed to rece

3条回答
  •  执笔经年
    2021-01-13 03:34

    I eventually found a solution.

    What I had to do was create a windows application that will act as a websocket server.

    When installing the program it will create a URI Scheme to be able to call itself from a link like myapp://start

    Here is the code to edit the registry to add a custom URI Scheme:

    static void Main(string[] args) {
    
                try {
    
                    // SET PATH OF SERVER
                    String path = Environment.GetCommandLineArgs()[0];
    
                    // GET KEY
                    RegistryKey key = Registry.ClassesRoot.OpenSubKey("myApp");
    
                    // CHECK FOR KEY
                    if (key == null) {
    
    
                        // SET KEY
                        key = Registry.ClassesRoot.CreateSubKey("myApp");
                        key.SetValue(string.Empty, "URL: myApp Protocol");
                        key.SetValue("URL Protocol", string.Empty);
    
                        // SET COMMAND
                        key = key.CreateSubKey(@"shell\open\command");
                        key.SetValue(string.Empty, path + " " + "%1");
                        //%1 represents the argument - this tells windows to open this program with an argument / parameter
    
    
                    }
    
                    // CLOSE
                    key.Close();
    
                } catch (Exception ex) {
                    Console.WriteLine(ex.Message);
                    Console.ReadKey();
    
                }
    
    
    
    }
    

    Then in the browser it will call that url which starts the program. The websocket will get created and send it to the localhost:port server program that is running.

    The windows program will do everything to get the data then send it as raw tcp/ip bytes to the terminal. Once the terminal sends data back to the windows program, the windows program will then forward that back to the websocket for the browser to handle the information.

    The windows program then will kill all connections and quit.

提交回复
热议问题