有没有办法用javascript与IP建立tcp连接? [英] Is there a way to do a tcp connection to an IP with javascript?

查看:129
本文介绍了有没有办法用javascript与IP建立tcp连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我介绍一下我正在努力实现的目标.

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

我有一个具有本地 IP 地址的设备(芯片和引脚终端),它已被编程为接收某些数据并对其进行处理.

I have a device(chip and pin Terminal) that has a local IP address, It has been programmed to receive certain data and process it.

示例:我以十六进制"30 35"发送字符串"05",终端读取该字符串并将重新启动.

example: I send the string "05" in hex "30 35" and the terminal reads that and will restart.

我尝试使用 SockJS-Client 以及内置的 WebSockets.

I have tried using SockJS-Client as well as the built in WebSockets.

但是使用 Websockets 我注意到浏览器正在发送:

However using Websockets I notice that the browser is sending:

GET / HTTP/1.1
Host: IP:PORT
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Origin: MYIP
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Sec-WebSocket-Key: A1CeTMFnQCZv4GNZbLFnyQ==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits

当我的代码如下所示:

var exampleSocket = new WebSocket("ws://IP:PORT");

exampleSocket.send("05");

如果我把代码改成这样:

If I change the code to this:

var exampleSocket = new WebSocket("wss://IP:PORT");

exampleSocket.send("05");

我只收到 3 个发送的标签:SYN(0x0016) ETX(0x0003) SOH(0x0001)

I just get 3 tags that are sent: SYN(0x0016) ETX(0x0003) SOH(0x0001)

现在我不确定您是否需要一个 WebSocket 服务器才能解释传入的数据.

Now I'm not sure if you need to have a WebSocket Sever to be able to interpret the incoming data.

SockJS 通过发送关于自身和浏览器的额外信息来做同样的事情:

SockJS does the same thing by sending extra information about itself and the broswer:

GET /info?t=1452272641278 HTTP/1.1
Host: IP:PORT
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
Origin: MYIP
Accept: */*
Referer: MYIP
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8

所以我想我的问题是.有没有办法只发送我想要的东西?没有任何额外数据?

So I guess my question is. Is there a way to just send exactly what I want? without any extra data?

我已经在 Objective-C 和 C# 中完成了这项工作,我只是不确定 javascript 是否可以做到这一点?

I have completed this in Objective-C as well as C#, I'm just not sure if javascript can do this?

有不清楚的地方请追问,我会尽量澄清.

Please ask if anything is unclear and I will try to clarify.

推荐答案

我最终找到了解决方案.

I eventually found a solution.

我必须做的是创建一个 Windows 应用程序来充当 websocket 服务器.

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

安装程序时,它将创建一个 URI 方案,以便能够从像 myapp://start

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

这是编辑注册表以添加自定义 URI 方案的代码:

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();

            }



}

然后在浏览器中它会调用启动程序的那个 url.将创建 websocket 并将其发送到正在运行的 localhost:port 服务器程序.

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.

windows 程序将尽一切努力获取数据,然后将其作为原始 tcp/ip 字节发送到终端.一旦终端将数据发送回 windows 程序,windows 程序就会将其转发回 websocket 以供浏览器处理信息.

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.

Windows 程序将终止所有连接并退出.

The windows program then will kill all connections and quit.

这篇关于有没有办法用javascript与IP建立tcp连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆