Socket Disconnect Nodejs

Asked

Viewed 737 times

0

Hello I have a socket in which when I run the command socket.disconnect() it return me Disconnect is not a Function, what could it be ? I have tried several ways to disconnect and none solved my problem

var net = require('net');
var clientIn = new net.Socket();
var clientOut = new net.Socket();


clientIn.connect(configPort, configHost, function () {
  console.log('Conectado');
 
});


clientIn.on('data', function (data) {

  console.log(data);

}

clientOut.connect(configPort, configHost, function () {
  console.log("Conectado");

}

clientOut.on('data', function (data) {
 console.log(data);
}
   
   clientOut.disconnect();

  • 2

    clientOut.destroy(); or clientOut.pause could solve. One shutdown forever and the other can be activated to return.

  • clientOut.destroy() and clientOut.pause() , all two worked. Thanks a lot for the help @Maurydeveloper

  • can inform me how I would restart the clientIn @Maurydeveloper ?

  • 1

    The net module is very limited, I use frameworks to help, but to reset I would use clientIn.Destroy().

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

  • 1

    I use socket io.

Show 1 more comment

1 answer

0


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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.