Web sockets how to make a connection by selecting an IP

2

I'm doing a project that involves connecting to an ESP8266 via websockets , but since ip of ESP is not static, you need to manually connect to the IP . But in the code below when I put connection methods inside a function, the other methods of websocket are not recognized as valid

  

( ONOPEN ONMESSAGE e ONERROR )

I want to know if there is a way to choose ip and make the connection.

var connection;
function Conectar(){
        var text = document.getElementById('ip').value;
        ipValue = "ws://"+text+":81/";
        connection = new WebSocket('ws://192.168.1.41:81/', ['arduino']);
        LerEstado();
};

connection.onopen = function () {
    console.log("CONECTADO");
    LerEstado();
};

connection.onerror = function (error) {
    console.log('WebSocket Error '+ error);
};

connection.onmessage = function (e) {
    LerEstado();
    var mensagem = e.data;
    console.log('Recebido ESP8266: '+ mensagem);
    var letra = mensagem.charAt(0);
};
    
asked by anonymous 16.01.2017 / 23:18

0 answers