Is there a way to connect tcp to IP using javascript?

Let me tell you a little about what I'm trying to accomplish.

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

example: I send the string "05" in hexadecimal "30 35" , and the terminal reads this and reboots.

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

However, using Websockets, I notice that the browser sends:

 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 

When my code looks like this:

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

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

Now I'm not sure what you need for WebSocket Sever to interpret incoming data.

SockJS does the same, sending additional information about itself and the bracelet:

 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 think, my question. Is there a way to send exactly what I want? without any additional data?

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

Please ask if something is not clear, and I will try to clarify.

+5
source share
3 answers

In the end, I found a solution.

I needed to create a Windows application that will act as a websocket server.

When installing the program, he will create a URI scheme to be able to call himself on the link, for example myapp://start

Here is the code for editing 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 this url, which launches the program. A websocket will be created and sent to the localhost server: a port server program that works.

The Windows program will do everything to get the data, and then send it as raw tcp / ip bytes to the terminal. After the terminal sends the data back to the Windows program, the Windows program then redirects it back to the websocket so that the browser processes the information.

Then the Windows program will destroy all connections and close.

0
source

You cannot make a normal TCP connection from Javascript in a browser, which allows you to send data to your own data format or protocol. The browser simply does not support this.

This allows you to make Ajax requests and WebSocket connections. Both Ajax and WebSocket requests start as an HTTP request. In the case of a webSocket request, the HTTP request can then be β€œupgraded” to the webSocket protocol after both parties have agreed, but the original data sent to the server will be a legitimate HTTP request. You can see this MDN link for the whole scheme of how the webSocket protocol works from connecting to the actual packet format. Even if it is upgraded to the webSocket protocol, it should use the webSocket framework format for all the data that is described here .

Here is the outline of the webSocket data frame format:

 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-------+-+-------------+-------------------------------+ |F|R|R|R| opcode|M| Payload len | Extended payload length | |I|S|S|S| (4) |A| (7) | (16/64) | |N|V|V|V| |S| | (if payload len==126/127) | | |1|2|3| |K| | | +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - + | Extended payload length continued, if payload len == 127 | + - - - - - - - - - - - - - - - +-------------------------------+ | |Masking-key, if MASK set to 1 | +-------------------------------+-------------------------------+ | Masking-key (continued) | Payload Data | +-------------------------------- - - - - - - - - - - - - - - - + : Payload Data continued ... : + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | Payload Data continued ... | +---------------------------------------------------------------+ 

So, if your endpoint does not speak HTTP or the webSocket protocol, you cannot directly connect to it from the Javascript browser.

What you see in your question as the original HTTP request is the start of the webSocket connection. How it works.

If you use Javascript in a browser-free environment, such as node.js, you can use the "net" module to create a simple TCP socket, which then you can use whatever protocol you want.

+6
source

You tried to use it like this:

 var exampleSocket = new WebSocket('wss://IP:PORT', ['soap', 'xmpp']); // When the connection is open, send some data to the server exampleSocket.onopen = function () { exampleSocket.send('05'); }; // Log errors exampleSocket.onerror = function (error) { console.log('WebSocket Error ' + error); }; // Log messages from the server exampleSocket.onmessage = function (e) { console.log('Server: ' + e.data); }; 

hope i could be helpful!

0
source

Source: https://habr.com/ru/post/1240180/


All Articles