Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
103 views
in Technique[技术] by (71.8m points)

javascript - Manage Rcon remote web rust server

I am trying to develop a web application that allows me to type commands in the Rcon console via the web Browser. The problem is that every time I send a command I get “[Rcon] Failed to parse message, incorrect format”.

Error message rcon

File log server

Code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebSocket</title>
</head>
<body>
    <h1>WebSockets Rust Server</h1>
    <input type="button" value="Send" onclick="webSocketTest();">

    <script>
        function webSocketTest() {
            // Create the WebSocket
            const rcon = new WebSocket('ws://localhost:28016/1234');
        
            rcon.onopen = function(e) {
                // This line causes the problem
                rcon.send('status');
            }

            rcon.onmessage = function(e) {
                // Code
            }

            rcon.onerror = function(e) {
                // Code
            }

            rcon.onclose = function(e) {
                // Code
            }
        }
    </script>
</body>
</html>

EDIT:

Finally I fix the problem. I was making the mistake of trying to collect the data in the wrong function.

<<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebSocket</title>
</head>
<body>
    <h1>WebSockets Rust Server</h1>
    <input type="button" value="Send" id="btnSend"><br>
    <textarea id="response" rows="10" cols="60"></textarea>

    <script>
            const rcon = new WebSocket('ws://localhost:28016/1234');
            console.log(rcon);

            rcon.onopen = function() {
                console.log('Connected');
            };

            /* In this funcion have the server response */
            rcon.onmessage = function(e) {
                const msg = JSON.parse(e.data);
                document.getElementById('response').innerHTML = msg.Message;
            }

            rcon.onerror = function (e) {
                console.log(e);
            }

            rcon.onclose = function(e) {
                console.log('Connection closed');
                console.log(e);
            };

            /* Click Event on send btn that calls anonymous function to send the data */
            const btnSend = document.getElementById("btnSend");
            btnSend.addEventListener('click', function() {
                /* Data to send */
                const data = {
                    Message: "status", // rcon command
                    Identifier: 1, // +server.identity
                    Name: "totalgamer" // +server.hostname
                };
                /* Need to use JSON.stringify before send the data */
                rcon.send(JSON.stringify(data));
            });
    </script>
</body>
</html>

Now it's works fine.

Result

question from:https://stackoverflow.com/questions/65896128/manage-rcon-remote-web-rust-server

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...