Websocket in java and javascript

Asked

Viewed 246 times

1

I have a single server done in JAVA that complete a form of a particular website web. I use the java application as a server to send the information and a extension made for Chrome who acts like client, receiving this information and placing it on site form.

My problem is when close the page and open again or update, the server stops sending new information to her, would like a solution if possible, thank you.

I am running the application on JAVA through another main class, by the command: new Servidorext(8080). start();

Server JAVA:

/*
 * 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 {

//Pega as informações contidas em outra classe

    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) {

    //Verifica se há novas mensagens para continuar ou dar inicio ao fluxo

        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!");
    }

}

Client Javascript:

/* global websocket, ws */

var ws = new WebSocket('ws://localhost:8080');
var itens = [];

ws.onopen = function ()
{
   ws.send("");
   // alert("Mensagem enviada...");
};

ws.onerror = function (erro)
{
    alert('Ocorreu um erro!' + erro);
};

ws.onmessage = function (evt)
{   

//Recebe as mensagens do servidor, as guarda em uma Array e distribui entre 
//as partes do form

    itens.push(evt.data);

    document.getElementById("NI").value = itens[0].toString();

    document.getElementById("CodigoAcesso").value = itens[1].toString();

    document.getElementById("Senha").value = itens[2].toString();

    itens.splice(0,itens.length);
};

ws.onclose = function (evt)
{
    websocket.close();
    alert('Conexão fechada...' + evt);
};

window.onbeforeunload = function (evt) {
    alert('Conexão fechada...' + evt);
    websocket.close();
};

Ps: I’m not getting any bug in the app or Chrome extension.

No answers

Browser other questions tagged

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