connector.js 837 Bytes
/**
 * 连接器
 */
import net from 'net';
import orders from './interface';
import util from './util';
var log = util.log;

//连接到服务端
export default function (config, send, receive) {
    var client = new net.Socket();

    //初始化
    client.connect(config.port, config.host, function () {
        log.info(config.host + ':' + config.port + ':读写器连接成功!');
        send(client);
    });

    //接收数据
    client.on('data', function (data) {
        var arr = [];
        arr = util.toHex(data);
        receive(client, arr);
    });

    //异常处理
    client.on('error', function (error) {
        log.error('错误:' + error.code);
        client.destroy();
        receive(client, null, error);
    });

    client.on('close', function () {
        log.info('读写器连接断开');
    });
};