A
A
Andrey Yelpaev2014-03-27 06:35:20
MySQL
Andrey Yelpaev, 2014-03-27 06:35:20

Why does socket drop connection, nodejs?

Good morning, next question. The server is written in nodejs using the Net. The GPS tracker sends one packet to the server (1 information packet = 42 bytes), if the server was turned off, then packets are accumulated on the tracker, which are then sent in a pack of 10 information packets, that is, 10 * 42 = 420 bytes a pack. The package is then parsed and stored in MySQL.

When it sends 1 packet, it works stably. And when there are 10 packets, it breaks the connection after a while. What could be the problem?

UPD: I noticed a pattern in the error, it always knocks out bytesRead: 6966

Data reception code:

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

            var data = new Buffer(data, "hex");
            data = data.toString("hex");

            var gpsData = ServerCore.decodePackage(data);

            ServerCore.emit('track', gpsData);
            ServerCore.emit('encodeTrack', data);

        });


Database save code:
for(var i = 0; i < packages.length; i++) {
                    var sql = {
                        id: data["packages"][packages[i]].id,
                        software: data["packages"][packages[i]].software,
                        status: data["packages"][packages[i]].status,
                        latitude: data["packages"][packages[i]].latitude,
                        longitude: data["packages"][packages[i]].longitude,
                        course: data["packages"][packages[i]].course,
                        speed: data["packages"][packages[i]].speed,
                        acc: data["packages"][packages[i]].acc,
                        height: data["packages"][packages[i]].height,
                        hdop: data["packages"][packages[i]].hdop,
                        glonass: data["packages"][packages[i]].glonass,
                        gps: data["packages"][packages[i]].gps,
                        time: data["packages"][packages[i]].timestamp,
                        voltage: data["packages"][packages[i]].voltage
                    }
            pool.getConnection(function(err, connection) {
                    connection.query("INSERT INTO trackerData SET ?", sql ,function (err, result) {
                        if(err) console.log(err);
                        console.log(result);
                        connection.release();
                    });

            });
        }


socket error:
{ [Error: Socket error]
  reason: 'Socket error',
  socket:
   { _connecting: false,
     _handle: null,
     _readableState:
      { highWaterMark: 16384,
        buffer: [],
        length: 0,
        pipes: null,
        pipesCount: 0,
        flowing: false,
        ended: false,
        endEmitted: false,
        reading: true,
        calledRead: true,
        sync: false,
        needReadable: true,
        emittedReadable: false,
        readableListening: false,
        objectMode: false,
        defaultEncoding: 'utf8',
        ranOut: false,
        awaitDrain: 0,
        readingMore: false,
        decoder: null,
        encoding: null },
     readable: false,
     domain: null,
     _events:
      { end: [Object],
        finish: [Function: onSocketFinish],
        _socketEnd: [Function: onSocketEnd],
        error: [Function],
        data: [Function],
        readable: [Function] },
     _maxListeners: 10,
     _writableState:
      { highWaterMark: 16384,
        objectMode: false,
        needDrain: false,
        ending: false,
        ended: false,
        finished: false,
        decodeStrings: false,
        defaultEncoding: 'utf8',
        length: 0,
        writing: false,
        sync: true,
        bufferProcessing: false,
        onwrite: [Function],
        writecb: null,
        writelen: 0,
        buffer: [],
        errorEmitted: true },
     writable: false,
     allowHalfOpen: false,
     onend: null,
     destroyed: true,
     bytesRead: 6966,
     _bytesDispatched: 0,
     _pendingData: null,
     _pendingEncoding: '',
     server:
      { domain: null,
        _events: [Object],
        _maxListeners: 10,
        _connections: 0,
        connections: [Getter/Setter],
        _handle: [Object],
        _usingSlaves: false,
        _slaves: [],
        allowHalfOpen: false,
        _connectionKey: '4:0.0.0.0:12300',
        maxConnections: 0 },
     _peername: { address: '85.26.233.161', family: 'IPv4', port: 17032 },
     pipe: [Function],
     addListener: [Function],
     on: [Function],
     pause: [Function],
     resume: [Function],
     read: [Function],
     _consuming: true,
     _idleNext: null,
     _idlePrev: null,
     _idleTimeout: -1 },
  settings:
   { ip: '0.0.0.0',
     port: 12300,
     connections: 0,
     maxConnections: 1000 } }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Yelpaev, 2014-04-01
@andreyelpaev

The problem was solved, the Megafon SIM card was installed, the MTS SIM card was installed, the gaps disappeared.

O
Oleg Serykh, 2014-07-10
@seryh

I can assume that the problem was that more than 10 (max limit by default in mysql) connections to the database were created, since you call pool.getConnection in a loop, and even when receiving data.
Calling pool.getConnection would look better in the event: createServer->function(socket), i.e. there would be a maximum of one connection to the database per socket connection from the tracker.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question