0

To send a simple string to a socket (not websocket), the following snippets works on node.js. Is it possible to do something similar on the client side?

    var net = require('net');
    var client = new net.Socket();
    client.connect(port, ip, function() {
        client.write(message);
    });

Basically I want to send a string to a simple socket (not a websocket) on the network from the browser. how would I do it?

Kuravi H
  • 101
  • 1
  • 8

1 Answers1

0

No, it's impossibile. The primitive you have is XMLHttpRequest, which is a specialized socket facade: you can customize the HTTP method, the URL (but you can't even change host!) and the HTTP body. You can set headers and cookies (which are headers), but it will alway look like a HTTP request. Maybe there are browser specific extensions, though.

However, unless you want to use an already existing protocol, there should be no problem in encapsulating your payload in HTTP. You should always design on your constraints: if the client must run in any standard browser, then it must use HTTP.

Raffaele
  • 20,627
  • 6
  • 47
  • 86