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

254
Visualizações
Send a message from client to server (Websocket)

I have a .php file, where I have 2 buttons, one to upload a file and the other to read it line by line.

(Bellow the image of buttons and the code)

Buttons

HTML

<div style="font-size:20px;" class="sub-title">File Upload</div>                                                                                        
<input class="buttonupload" type="file" name="fileToUpload" id="fileToUpload">                                                                                                                                   
<button id="uploadFileButton">Start</button>

Script to read the file line by line with delay

 // Read file line by line with delay
    function readFile(file, onLoadCallback){

        var reader = new FileReader();
        reader.onload = onLoadCallback;
        reader.readAsText(file);
    }
    
    $('#uploadFileButton').on('click', function(e){
        readFile(document.getElementById('fileToUpload').files[0], function(e) {  
            var content = e.target.result;
            var fileContentArray = content.split(/\r\n|\n/);
            let index = 0;
            const interval = setInterval(function() { 
            console.log(fileContentArray[index]);
            ++index;
        }, 1000)
        });
    });

I have a websocket server too.

Websocket.js

const WebSocket = require("ws");       
const server = new WebSocket.Server({  port: 8083 });                                         

let sockets = [];                           

server.on('connection', function(socket) {  
    sockets.push(socket);                   
    console.log("Connection established");

    socket.on('message', function(msg) {    
    console.log('Cliente: %s', msg);    
    sockets.forEach(s => s.send(msg.toString()));
    });

   socket.on('close', function() {       
   sockets = sockets.filter(s => s !== socket);  
   });
});

And here is the .php (websocket)

  function connect() {
        console.log("Trying to connect with websocket...");
        //const socket = new WebSocket("ws://" + ip + ":5656");
        const socket = new WebSocket('ws://localhost:8083');
            

My question is: How can I send the content of the file to the server?

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