Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

193
Visualizações
Assigning value to a variable and sending it to NodeJS server

I am trying to assign a value to a variable by getting the input from the html form and then sending it to my node js server, but the problem is that when the value is fixed the server displays it bash on the html page from where it came. When I trie to get the value from the form it just gets an empty string. here is the code for html and node js server.

// Node.js WebSocket server script
const http = require('http');
const WebSocketServer = require('websocket').server;

const server = http.createServer();
console.log('Server is on port 3000')
server.listen(3000);


const wsServer = new WebSocketServer({
    httpServer: server
});

wsServer.on('request', function(request) {
    const connection = request.accept(null, request.origin);

    connection.on('message', function(message) {
        var test=message.utf8Data;
        console.log('Received Message:', test);
        connection.sendUTF(test);
    });
    connection.on('close', function(reasonCode, description) {
        console.log('Client has disconnected.');
    });
});
<?php ob_start() ?>
    <?php $titel="Chat"; ?>

    <div class="chat-main" id="text"></div>

    <form name='form' method='post' class="form">
        <div class="form-group">
            <input type="text" class="form-control" id="textbox" name="chat" placeholder="Chat">
            <button type="submit" class="btn btn-primary" name="gumb" id="gumb">Send</button>
        </div>
        </form>
        <script>

            const ws = new WebSocket('ws://localhost:3000'); 
            var message="This is the client speaking.";
            console.log(message);
            ws.onopen = function() {
                console.log('WebSocket Client Connected');
                ws.send(message);
            };
        
            ws.onmessage = function(e) {
                console.log("Received: '" + e.data + "'");
                document.getElementById('text').innerHTML=e.data;
            };
    </script>
<?php 
    $content=ob_get_clean();
    require "layout.html.php";
?>

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda