Problems with Sockets

0

I'm creating an application that needs to send and receive information from a web page through Sockets , the Java server I was able to do, but I do not understand much of JS , could you help me make the client that sends and receives messages in JavaScript for web and that connects to this server in Java I did?

Server code: Server.java

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

    System.out.println("Inicia servidor.");

    ServerSocket server = new ServerSocket(3000);

    System.out.println("Aguardando conexão.");

    Socket socket = server.accept();

    System.out.println("Conexão estabelecida.");

    InputStream input = socket.getInputStream();
    OutputStream output = socket.getOutputStream();

    BufferedReader in = new BufferedReader(new InputStreamReader(input));
    PrintStream out = new PrintStream(output);

    while (true) {
        String mensagem = in.readLine();

        System.out.println(
                "Mensagem recebida do cliente [" +
                        socket.getInetAddress().getHostName() +
                        "]: " +
                        mensagem);
        if ("FIM".equals(mensagem)){
            break;
        }

        out.println(mensagem);
    }

    System.out.println("Encerrando conexão.");

    in.close();

    out.close();

    socket.close();

    System.out.println("Encerrando servidor.");

    server.close();

}
    
asked by anonymous 19.12.2017 / 21:32

1 answer

1

There are several ways to do this and libs ready. I'll put here a js native example.

function WebSocketTest()
         {
            if ("WebSocket" in window)
            {
               alert("WebSocket is supported by your Browser!");

               // Let us open a web socket
               var ws = new WebSocket("ws://localhost:3000");

               ws.onopen = function()
               {
                  // Web Socket is connected, send data using send()
                  ws.send("Message to send");
                  alert("Message is sent...");
               };

               ws.onmessage = function (evt) 
               { 
                  var received_msg = evt.data;
                  alert("Message is received...");
               };

               ws.onclose = function()
               { 
                  // websocket is closed.
                  alert("Connection is closed..."); 
               };

               window.onbeforeunload = function(event) {
                  socket.close();
               };
            }

            else
            {
               // The browser doesn't support WebSocket
               alert("WebSocket NOT supported by your Browser!");
            }
         }

In this function it checks if your browser supports WebSocket. Then it opens the connection in the url that you should configure.

ws.onopen is the event that happens as soon as you log in to the socket.

ws.onmessage is the event of when you receive a message

ws.onclose is the event when the connection is closed

    
19.12.2017 / 22:47