Web sockets how to make a connection by selecting an IP

Asked

Viewed 155 times

2

I’m doing a project that involves connection to a ESP8266 through websockets, but as the ip of ESP no and static need to make manual connection with the IP. But in the code below when I put the connection methods inside a function, the other methods of the websocket are not recognised as valid

(ONOPEN ONMESSAGE e ONERROR)

I want to know if there’s a way to choose the 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);
};
No answers

Browser other questions tagged

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