Have you tried using the socket.io library along with the express ?
It is very easy to understand and use the process.
socket documentation.io
The example socket server: socket.js, then Node socket.js is in the air.
const port = process.env.PORT || 3001; //porta padrão
var app = require('express')();
var http = require('http').createServer(app);
const socketio = require('socket.io');
var io = socketio(http, {perMessageDeflate: false});
http.listen(port, () => {
console.info(`Servidor socket inicializado usando express na porta ${port}`);
});
io.set('transports', ['websocket']);
io.on("connection", (socket) => {
console.info(`Client entrou [id=${socket.id}]`);
socket.on("disconnect", () => {
console.info(`Client saiu [id=${socket.id}]`);
});
socket.on("mensagem-enviada", dados => {
socket.broadcast.emit('mensagem-repassada', dados);
console.log(`mensagem recebida e repassada: ${JSON.stringify(dados)}`)
});
});
On the client side, you can use it like this:
<script type="text/javascript">
var socket = io('http://localhost:3001', {transports: ['websocket']});
socket.on('connect', () => {
console.log("socket conseguiu conectar ao servidor");
});
socket.on('mensagem-repassada', data => {
console.log("mensagem recebida que foi repassada:");
console.log(data);
});
// para enviar ao socket para basta chamar a funçao enviarMensagem passando os dados que vai enviar o servidor
function enviarMensagem(dados){
socket.emit("mensagem-enviada", dados);
}
</script>
clientOut.destroy();
orclientOut.pause
could solve. One shutdown forever and the other can be activated to return.– Maury Developer
clientOut.destroy()
andclientOut.pause()
, all two worked. Thanks a lot for the help @Maurydeveloper– Zé Reis M. Olliver
can inform me how I would restart the
clientIn
@Maurydeveloper ?– Zé Reis M. Olliver
The net module is very limited, I use frameworks to help, but to reset I would use clientIn.Destroy().
– Maury Developer
Ok @Anittadeveloper I will be checking this very much thanks for the help :).... Could you tell me some libraries I might be using to improve the performance of my application ?
– Zé Reis M. Olliver
I use socket io.
– Maury Developer