How do I kill a websocket?

Asked

Viewed 49 times

1

I’m having a hard time kill the websocket that runs inside my java app, I can’t find answers anywhere.

I need this, because I’m using it with a Chrome extension to fill out a website’s Forms, but when I refresh the page or close it and open the server again to stop sending new information to the form, then I want to restart it when necessary.

Class Servidorext:

/*
 * 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 triggered by the command new ServidorExt(8080).start(); in another class in the app.

1 answer

2


According to the class Javadoc Websocketserver it seems to me that you are using, just use the method stop on your server.

The problem is, 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 to be able to call stop() in it after. 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.

  • You don’t know how much I searched for this, thank you very much for the help, otherwise, you can tell me why the server stops sending new information to the client in javascript when I close the browser and open again, since it is connected to the server?

  • I’m sorry, I don’t understand much on the Javascript side. But feel free to ask another question here on the site, putting the code next to Javascript and explaining the situation.

  • All right, I’ve done it, with all the explanation of my problem, but unfortunately no one answered, but you’ve been a great help, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.