Javascript 正确的错误处理和交叉引用
下面的代码返回“goN不是函数”错误。当出现错误时,我们需要删除旧对象并创建新对象,如何生成正确的错误处理程序 主模块udpSocket.js:Javascript 正确的错误处理和交叉引用,javascript,node.js,sockets,Javascript,Node.js,Sockets,下面的代码返回“goN不是函数”错误。当出现错误时,我们需要删除旧对象并创建新对象,如何生成正确的错误处理程序 主模块udpSocket.js: const udp = require('dgram'); const goN = require('./goNext').goNext; class udpSocket { constructor(config){ this.config = config; this.socket = udp.createSocket('udp4')
const udp = require('dgram');
const goN = require('./goNext').goNext;
class udpSocket {
constructor(config){
this.config = config;
this.socket = udp.createSocket('udp4');
this.socket.on('message', (buf) => {
this.socket.send(buf, this.config.outPort);
});
this.socket.on('error', (err) => {
console.log(err);
goN(this.socket, this.config.host);
});
}
start(){
this.socket.bind(this.config.port, this.config.host, (err) => {
if (err){
console.error(err);
goN(this.socket, this.config.host);
} else {
this.socket.send('test', this.config.outPort, this.config.host, (err) => {
if (err) {
console.error(err);
goN(this.socket, this.config.host);
} else {
console.log('UDP server up and running on '+this.config.port+' inPort, '+this.config.outPort+' outPort');
}
});
}
});
}
close(){
this.socket.close( () => {
ports.add({"in": this.config.port, "out": this.config.outPort});
delete this.socket;
});
}
}
module.exports = udpSocket;
goNext.js:
const udpSocket = require('./udpSocket');
module.exports.goNext = (socket, host) => {
if (socket != null){ delete socket; }
if (ports.length > 0){
let pp = ports.shift();
let server = new udpSocket({
port: pp.in,
outPort: pp.out,
host: host
});
sockets.set(pp.in, server);
server.start();
} else {
console.log('no sockets left');
process.exit(1);
}
}
wrapper.js
const config = require('./config').udp;
const goNext = require('./lib/goNext').goNext;
const List = require('collections/list');
global.ports = new List(config.ports);
global.sockets = new Map();
goNext(null, config.host);
goNext(null, config.host);
也许是因为这些文件相互需要