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 client side:

WebSocket connection to 'ws://' failed: Connection closed before receiving a handshake response

It maybe stays on OPENNING state because the connection.onopen function is never called. The console.log('Connected!') isn't being called.

Could someone let me know what is going wrong here?



public class Server {

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

        try (ServerSocket serverSocket = new ServerSocket(4444)) {
            GameProtocol gp = new GameProtocol();

            ServerThread player= new ServerThread(serverSocket.accept(), gp);

        } catch (IOException e) {
            System.out.println("Could not listen on port: 4444");





public class ServerThread extends Thread{

    private Socket socket = null;
    private GameProtocol gp;

    public ServerThread(Socket socket, GameProtocol gp) {
        this.socket = socket; = gp;

    public void run() {

        try (
                PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                BufferedReader in = new BufferedReader(
                        new InputStreamReader(
                ) {
            String inputLine, outputLine;

            while ((inputLine = in.readLine()) != null) {
                outputLine = gp.processInput(inputLine);
        } catch (IOException e) {


public class GameProtocol {

    public String processInput(String theInput) {

        String theOutput = null;

        theOutput = theInput;

        return theOutput;


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: ' +;

Show source
| javascript   | java   | websocket   | java-websocket   2017-01-04 19:01 1 Answers

Answers to Java server JavaScript client WebSockets ( 1 )

  1. 2017-01-05 09:01

    To start with, both your code looks identical the Java and JavaScript one. Both works for what they are design to, but the facts is that you are trying to connect a WebSocket client to a socket server.

    As I know they are two different things regarding this answer.

    From my experience I have never tried it your way. That said if I have a network application that use socket than it would be pure client/server socket, and if was a web application than I would use WebSocket on both side as well.

    So far so good :)

    To make your day with less pain as this answer says, I would suggest you to use any available WebSocket on server side and your problem is solved.

    I am using typically WebSocket for Java and here is a sample implementation that I have tested with your client code and it works, both on client and server side.

    import org.java_websocket.WebSocket;
    import org.java_websocket.handshake.ClientHandshake;
    import org.java_websocket.server.WebSocketServer;
    import java.util.HashSet;
    import java.util.Set;
    public class WebsocketServer extends WebSocketServer {
        private static int TCP_PORT = 4444;
        private Set<WebSocket> conns;
        public WebsocketServer() {
            super(new InetSocketAddress(TCP_PORT));
            conns = new HashSet<>();
        public void onOpen(WebSocket conn, ClientHandshake handshake) {
            System.out.println("New connection from " + conn.getRemoteSocketAddress().getAddress().getHostAddress());
        public void onClose(WebSocket conn, int code, String reason, boolean remote) {
            System.out.println("Closed connection to " + conn.getRemoteSocketAddress().getAddress().getHostAddress());
        public void onMessage(WebSocket conn, String message) {
            System.out.println("Message from client: " + message);
            for (WebSocket sock : conns) {
        public void onError(WebSocket conn, Exception ex) {
            if (conn != null) {
                // do some thing if required
            System.out.println("ERROR from " + conn.getRemoteSocketAddress().getAddress().getHostAddress());

    On your main method just:

    new WebsocketServer().start();

    You might need to manipulate your code to fit it with this implementation, but that should be part of the job.

    Here is the test output with 2 tests:

    New connection from
    Message from client: Ping
    Closed connection to
    New connection from
    Message from client: Ping

    here is WebSocket maven configuration, otherwise download the JAR file/s manually and import it in your IDE/development environment:

    <!-- -->

    WebSocket more info:

Leave a reply to - Java server JavaScript client WebSockets

◀ Go back