4

My web application needs to connect to a local device on the network to get data.

The device has a static IP address and communicates via the P03 protocol. When I connect with Telnet, the device returns a plain text in my console.

I need to do the same thing with JavaScript in-browser. When a user clicks a button, it should connect to 192.168.0.1:8000 via TCP and display the received text data on the page.

The problem is that is a web application. Is it possible to do client-side?

(I'm also willing to accept other suggestions besides JavaScript.)

Brad
  • 159,648
  • 54
  • 349
  • 530
Vinicius H
  • 41
  • 1
  • 3

1 Answers1

3

Unfortunately, you cannot connect to just any TCP socket. The browser can make connections, but it has to be one of these protocols:

  • HTTP (HTTP[S] 1.0/1.1/2)
  • Web Socket (another application protocol actually running on top of HTTP)
  • WebRTC (intended for peer-to-peer, can be used for client-server communication as well, but not useful here)

One way to do this is to run a proxy of sorts. You need to make a proxy that accepts Web Socket connections from browsers, and then relays the sent/received data to this TCP server. The downside is that this requires a server.

Another way is to make a browser extension. See also: https://stackoverflow.com/a/17567373/362536

Brad
  • 159,648
  • 54
  • 349
  • 530
  • @user1283776 I rolled back your edit... I don't want to add confusion here, implying that somehow Web Sockets can be disconnected from the HTTP context. – Brad Nov 21 '18 at 14:15