Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

253
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda