Java server javascript client websockets

I’m trying to do a connection between a server in Java and a Javascript client but I’m getting this error on server side:

WebSocket connection to ‘ws://’ failed: Error during WebSocket handshake: net::ERR_INVALID_HTTP_RESPONSE

If I print out the server side message I got the message below.

Could someone help me please?




public class Server {

    public static void main(String[] args) throws IOException {

        ServerSocket server = new ServerSocket(4444);

        System.out.println("Server has started on\r\nWaiting for a connection...");

        Socket client = server.accept();

        System.out.println("A client connected.");




var connection = new WebSocket('ws://');

    connection.onopen = function () {
    connection.send('Ping'); // Send the message 'Ping' to the server

    // Log errors
    connection.onerror = function (error) {
    console.log('WebSocket Error ' + error);

    // Log messages from the server
    connection.onmessage = function (e) {
    console.log('Server: ' +;

Server message

GET / HTTP/1.1
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: pt-PT,pt;q=0.8,en-US;q=0.6,en;q=0.4
Sec-WebSocket-Key: FKSKBRGnEJjNvinHWh+rLQ==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.