Node net socket.end事件触发两次?

时间:2017-05-02 18:32:10

标签: node.js sockets

This event开了两次。我想弄明白为什么。

在一个客户端,我有:

import Net from 'net';
import Chalk from 'chalk';

const fallback = [2,5,10,25,50,100,250,500,1000,2000];

class LocalNetworkInterface {

    constructor({path}) {
        this._sock = new Net.Socket();
        this._pending = {};
        this._count = 0;
        this._retry = 0;

        const connect = () => {
            this._sock.connect({path});
        };
        this._sock.on('connect',() => {
            this._retry = 0;
            console.log(`Connected to ${Chalk.underline(path)}`);
        });
        this._sock.on('data',buffer => {
            let data = JSON.parse(buffer);
            this._pending[data.queryId].resolve(data);
            delete this._pending[data.queryId];
        });
        this._sock.on('end', () => {
            console.log(`Lost connection to ${Chalk.underline(path)}. Attempting to reconnect...`);
            connect();
        });
        this._sock.on('error', err => {
            if(err.code === 'ENOENT') {
                let ms = fallback[this._retry];
                if(this._retry < fallback.length - 1) ++this._retry;
                console.log(`Socket server unavailable. Trying again in ${ms}ms`);
                setTimeout(connect, ms);
            }
        });
        connect();
    }

    // ...
}

服务器:

const sockServer = Net.createServer(c => {
    c.on('data', buffer => {
        let data = JSON.parse(buffer);
        // log('Received',data);
        let ql = queryLogger();
        runQuery(Object.assign({}, data, {schema})).then(result => {
            ql(`${Chalk.magenta('socket')} ${print(data.query).trim()}`);
            let response = Object.assign({}, result, {queryId: data.queryId});
            c.write(JSON.stringify(response));
        });
    })
});

sockServer.on('error', serverError => {
    if(serverError.code === 'EADDRINUSE') {
        let clientSocket = new Net.Socket();
        clientSocket.on('error', clientError => {
            if(clientError.code === 'ECONNREFUSED') {
                FileSystem.unlink(SOCK_FILE, unlinkErr => {
                    if(unlinkErr) throw unlinkErr;
                    sockServer.listen(SOCK_FILE, () => {
                        log(`Sock server improperly shut down. Listening on '${sockServer.address()}'`)
                    });
                });
            }
        });
        clientSocket.connect({path: SOCK_FILE}, () => {
            throw new Error(`Server already running`);
        });
    }
});

['SIGTERM','SIGINT'].forEach(signal => process.on(signal, () => {
    console.log(`\rReceived ${Chalk.yellow(signal)}, shutting down ${Chalk.red('❤')}`);
    sockServer.close();
    process.exit();
}));

sockServer.listen(SOCK_FILE, () => {
    log(`Listening on ${Chalk.underline(sockServer.address())}`)
});

当我重新启动服务器时,我在客户端上看到“Lost connection”两次。为什么呢?

文档说:

  

当套接字的另一端发送FIN数据包时发出。

服务器是不是发送了两个“FIN”包呢?我可以用任何方式验证吗?

2 个答案:

答案 0 :(得分:1)

docs中就connect ...

看到了这一点

“...此函数是异步的。当发出'connect'事件时,套接字就会建立。如果连接有问题,则不会发出'connect'事件, 'error'事件将被发出异常。“

connect事件可能根本不会被解雇只是让它看起来像end事件被触发两次?就像@robertklep所说的那样,可能会扩展错误检查以获取更多特定代码。

答案 1 :(得分:0)

认为它是因为在end上,我立即尝试重新连接,然后再次捕获相同的事件。看起来有点奇怪,它可以做到这一点,但推迟到下一个滴答作用:

this._sock.on('end', () => {
    console.log(`${Chalk.yellow('Lost connection')} to ${Chalk.underline(path)}. Attempting to reconnect...`);
    process.nextTick(connect);
});