How do I kill a websocket?

1

I'm having a hard time killing the websocket that runs within my application in java, I can not find answers anywhere.

I need this, because I'm using it with a chrome extension to fill a website's forms, but when I refresh the page or close it and open the server again to send new information to the form, then I want to restart it when necessary.

Class ServerExt :

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package servidor;

import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
import view.painelUsuarios;

/**
 *
 * @author Neto
 */
public class ServidorExt extends WebSocketServer {

    public static String msg1;
    public static String msg2;
    public static String msg3;

    public void Verificador() {
        painelUsuarios pu = new painelUsuarios();
        msg1 = pu.getMsg1();
    }

    public void PegaMsg1() {
        msg1 = painelUsuarios.getMsg1();
    }

    public void PegaMsg2() {
        msg2 = painelUsuarios.getMsg2();
    }

    public void PegaMsg3() {
        msg3 = painelUsuarios.getMsg3();
    }

    public ServidorExt(int porta) throws UnknownHostException {
        super(new InetSocketAddress(porta));
        System.out.println("Recebendo conexões da porta: " + porta);
    }

    @Override
    public void onMessage(WebSocket webSocket, String mensagem) {

        String msgAnterior = "";
        String msgNova;

        while (true) {

            Verificador();
            msgNova = msg1;

            while (!msgNova.equals(msgAnterior)) {
                Verificador();
                msgAnterior = msg1;

                PegaMsg1();
                webSocket.send(msg1);
                System.out.println("msg 1 = "+ mensagem);

                PegaMsg2();
                webSocket.send(msg2);
                System.out.println("msg 2 = "+ mensagem);

                PegaMsg3();
                webSocket.send(msg3);
                System.out.println("msg 3 = "+ mensagem);
            }
        }
    }

    @Override
    public void onClose(WebSocket arg0, int arg1, String arg2, boolean arg3) {
        System.out.println("A conexão foi encerrada.");
    }

    @Override
    public void onError(WebSocket arg0, Exception e) {
        System.out.println("Erro de conexão." + e);
    }

    @Override
    public void onOpen(WebSocket webSocket, ClientHandshake arg1) {
        System.out.println("Foi iniciado uma nova conexão.");
    }

    @Override
    public void onStart() {
        System.out.println("Servidor iniciado com sucesso!");
    }

}

This class is thrown by the new ServidorExt(8080).start(); command in another class in the application.

    
asked by anonymous 03.02.2018 / 02:38

1 answer

2

According to the JavaDoc class WebSocketServer which I think you're using, just use the stop method on your server.

The problem is that when you do new ServidorExt(8080).start(); , you are throwing away the reference to the server you are starting. Save the reference in a variable so you can call stop() on it later. For example:

public class MinhaOutraClasse {

    private ServidorExt servidor;

    // .....

    public void iniciarServidor() {
        this.servidor = new ServidorExt(8080);
        this.servidor.start()
    }

    public void pararServidor() {
        this.servidor.stop()
    } 

    // ....

}

In this example, you call iniciarServidor() when you want to start and pararServidor() when you want to stop.

    
03.02.2018 / 03:03