Websocket is sending only one time

Hello,

i have a websocketserver running on a ESP32 which works perfect.

My problem is that if i send a message to the server over a website the socketserver on the ESP32 executes the .send(message) only the first time and is not sending anymore.

If i send a message from the console in chrome it works every time.

so my problem seem not to be on the server-side but on the client-side.

This is my code running inside chrome:

<!DOCTYPE html>

<head>
    <title>WH_Quiz</title>
    <style>
        body {
            background: url(Images/SET_1/A1_INTRO.png) no-repeat center center fixed;
            -webkit-background-size: cover;
            background-size: cover;
        }
    </style>
    
</head>

<body>


    <script>
        // clear localStorage
        window.localStorage.clear();

        // open websocket
        var url = "ws://192.168.4.1:1337";
        websocket = new WebSocket(url);
        websocket.onopen = function (event) {
            console.log('websocket is open');
            websocket.send("L");
        };
        
        
        // Keep track of clicked keys
        var isKeyPressed = {
            'a': false, // ASCII code for 'a'
            'b': false, // ASCII code for 'b'
            // ... Other keys to check for custom key combinations
        };

        document.onkeydown = (keyDownEvent) => {

            // Prevent default key actions, if desired
            keyDownEvent.preventDefault();

            // Track down key click
            isKeyPressed[keyDownEvent.key] = true;

            // Check described custom shortcut
            if (isKeyPressed['a']) { //for example we want to check if a is clicked
                //do something 
                console.log('a has been pressed');

                websocket.onopen = function (event) {
                    console.log('a has been pressed and wesocket is open');
                    window.location.href = '1_1.html'; // öffne Fragenkatalog 1
                    websocket.send("4G");
                };

            };
            // Check described custom shortcut
             if (isKeyPressed['b']) { //for example we want to check if b is clicked
                //do something 
                console.log('b has been pressed');
                                
                websocket.onopen = function (event) {
                    window.location.href = '2_1.html'; // öffne Fragenkatalog 2
                    websocket.send("4R");
                };
            };
        };
    </script>
</body>

</html>

Does anybody could explain me where the problem is?
Thanks to all.

@petemanzel, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project :wink: See About the Installation & Troubleshooting category.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.