3

I have a java client and I need to rewrite it in (client-side) javascript. I open the java Socket like this:

Socket socket = new Socket("127.0.0.1", 5015);

So I tried to use websocket in javascript: let socket = new WebSocket("http://127.0.0.1:5015");

but here I have a js error:

Uncaught DOMException: Failed to construct 'WebSocket':

The URL's scheme must be either 'ws' or 'wss'. 'http' is not allowed.

I tried also to use the 'ws' or 'wss' protocol but the server didn't want to handshake with such protocols.

Is there a way to make such socket connection in client-side javascript or it's definitely prohibited?

3 Answers 3

5

The answer is a little more complicated than "no you can't do it".

Javascript in a regular web page running in a web browser cannot open a plain socket. The fundamental reason is that it is a security risk for the user. So it is intentionally not allowed.

WebSockets are the secure way to do this. In conjunction with other browser security mechanisms, they limit what a web page is permitted to connect to.

However, that is not the end of the story. It is possible (at least in theory) for trusted code to send and receive TCP and UDP traffic. The problem is that the APIs for doing this are non-standard (e.g. browser specific). In some cases are themselves implemented as 3rd-party browser extensions.

So if you really wanted to pursue this for your application, you are going to have to distribute your code as a trusted browser plugin / extension AND deal with a range of browser portability issues.

It is worth noting that there was a W3C Working Group that was trying to standardize raw socket APIs, but they have officially abandoned their efforts. Their last working draft can be found at:

Finally, there is the problem that a trusted browser extension / plugin requires the user's consent to install. Getting informed consent for something like this is difficult, given the deep and subtle security issues associated with embedding this kind of functionality in the user's browser.

Sign up to request clarification or add additional context in comments.

Comments

2

No, you can't make an arbitrary TCP connection from a web page in any browser.

Web Sockets are fundamentally different than TCP sockets... they're essentially unrelated. They're a thin layer on top of HTTP along with a client API which allows bidirectional communication between a Web Socket client and a server supporting Web Sockets.

There are proxy servers you can run that allow connecting through them to make TCP connections, but this of course is a server feature and not something you can do in-browser alone.

Comments

0

The opening handshake is intended to be compatible with HTTP-based server-side software and intermediaries, so that a single port can be used by both HTTP clients talking to that server and WebSocket clients talking to that server. To this end, the WebSocket client's handshake is an HTTP Upgrade request:

    GET /chat HTTP/1.1
    Host: server.example.com
    Upgrade: websocket
    Connection: Upgrade
    Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
    Origin: http://example.com
    Sec-WebSocket-Protocol: chat, superchat
    Sec-WebSocket-Version: 13

https://www.rfc-editor.org/rfc/rfc6455

WebSockets server must be able to handle HTTP requests!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.