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://127.0.0.1:4444/’ 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?
Thanks
Server
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(4444);
System.out.println("Server has started on 127.0.0.1:4444.\r\nWaiting for a connection...");
Socket client = server.accept();
System.out.println("A client connected.");
}
}
Client
var connection = new WebSocket('ws://127.0.0.1:4444');
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: ' + e.data);
};
Server message
GET / HTTP/1.1
Host: 127.0.0.1:4444
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Origin: http://127.0.0.1:8080
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