0
I’m studying about socket. and I have a problem with the backend. I am developing a system that searches for files with certain extensions and shows them on a real-time screen.
To solve this I created a script by scanning all the files so that the same one to each file sends to my server this information. The problem is that I have already tried to connect these two files in several ways but could not. There is some way to do this?
Server.js
// Realiza o require do express, http, e socketio
var app = require('express')();
// passa o express para o http-server
var http = require('http').Server(app);
// passa o http-server par ao socketio
var io = require('socket.io')(http);
// cria uma rota para fornecer o arquivo index.html
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
var name = "";
// sempre que o socketio receber uma conexão vai devoltar realizar o broadcast dela
io.on('connection', function(socket){
socket.on('data_bind', msg => {
console.log('data_bind');
io.emit('data_bind', msg);
})
});
// inicia o servidor na porta informada, no caso vamo iniciar na porta 3000
http.listen(3000, function(){
console.log('Servidor rodando em: http://localhost:3000');
});
Index.html
var socket = io();
socket.on('data_bind', msg => {
let ul = document.getElementById("messages");
let li = document.createElement('li');
let br = document.createElement('br');
li.appendChild(document.createTextNode(msg));
ul.appendChild(li);
document.getElementById('typing').innerHTML = "";
document.getElementById('typing').hidden = true;
})
index js.
const testFolder = 'C://Users/user';
function Extension(tipo,quantidade){
this.tipo = tipo;
this.quantidade = quantidade;
}
var socket = require('socket.io')(3000)
var files = [];
const fs = require('fs');
var quantidade = 0;
var dataAtual = new Date();
dataAtual = Date.parse(dataAtual);
function readDir(dir){
let struct = {}
fs
.readdirSync(dir)
.sort((a, b) => {
try{
fs.statSync(dir +"/"+ a).mtime.getTime() - fs.statSync(dir +"/"+ b).mtime.getTime()
} catch(e){
}
})
.forEach(file => {
try{
if( fs.lstatSync(dir+"/"+file).isFile() ){
struct[file] = null
}
else if( fs.lstatSync(dir+"/"+file).isDirectory() ){
struct[file] = readDir(dir+"/"+file)
}
var arquivo = file.split('.')
var isNewFile = true;
if(files.length == 0){
var arq = new Extension(arquivo[arquivo.length - 1], 1);
files.push(arq);
} else {
files.forEach((valor,chave) => {
if(valor.tipo == arquivo[arquivo.length - 1]){
valor.quantidade = valor.quantidade + 1;
isNewFile = false;
}
});
if(isNewFile){
var arq = new Extension(arquivo[arquivo.length - 1], 1);
files.push(arq);
}
}
var valor = dir + "/" + file + " date: " + fs.lstatSync(dir+"/"+file).mtime + " tipo: " + arquivo[1];
quantidade++;
var dataLimite = new Date('2019-06-08');
var dataArquivo = new Date(fs.lstatSync(dir+"/"+file).mtime)
if(Date.parse(dataLimite) < Date.parse(dataArquivo)){
valor += ": data posterior"
} else {
valor += ": data inferior"
}
socket.emit('data_bind', valor);
console.log(valor);
} catch(e){
return
}
})
return struct
}
readDir(testFolder);
If I try to send some information through the front end (creating an Emit) it enters the server normally, but nothing I send from the backend to the server runs.