1

I'm trying to create a terminal emulator with pty.js and xterm.js. I've never tried something like this before so If I say something wrong, I apologize, its because I didn't understand it correctly. Anyway, The terminal should be displayed inside a JavaFX WebView and all the GUI is ready and tested. From what I've found, I understood that there must be a server running on localhost in order for pty.js to spawn a shell there, and communicate the results through xterm.js on the WebView. Now, I'm trying to create a server using ServerSocket in a new Thread but when I test the application and click on the Terminal button, the application freezes. I've tested the server whether it is running or not by visiting my localhost and it was up and running. So, what I get is that the main Thread of the application takes a lower priority the moment the Server thread starts. Am I right? And if so, how can I solve this?

The below function is called in the initialize method of the Terminal scene which is loaded from the main controller. Can somebody please explain why is this happening?

    private void createServerToSpawnPty() {

        Thread thread = new Thread(() -> {
            try {
                ServerSocket server = new ServerSocket(8080);
                System.out.println("Created Java localhost server on port: 8080");
                while (true) {
                    final Socket client = server.accept( );
                    System.out.println("client connected:+
                    client.isConnected( ));
                }

             }catch (IOException exception) {
                exception.printStackTrace( );
             }
         });
        thread.setDaemon(true);
        thread.run();
    }

1 Answers1

1

Both links lead to very useful information so thank both of you. Since this server won't be waiting for connections from any other client than the WebView, I ended up implementing the method in another way, more specific I removed the while loop and wrapped everything in a Task which runs anyway in the background and can also be passed to the ExecutorService.

    private void startServerToSpawnPty() {

        Task<Void> serverTask = new Task<Void>( ) {
            @Override
            protected Void call() throws Exception {
                ServerSocket server = new ServerSocket(8080);
                System.out.println("Created Java localhost server on port: 8080");
                final Socket client = server.accept( );

               if (client.isConnected( )) {
                    System.out.println("client connected: " + client.isConnected( ));
                    System.out.println("You just connected with the Java localhost" +               
                    "server on port: 8080");
                } else 
                    System.out.println("No connection found");

                return null;
            }
        };
        ExecutorService service = Executors.newFixedThreadPool(1);
        service.execute(serverTask);
        service.shutdown();
    }