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)
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?