注意我解决了自己的问题。解决方案在下面的评论中...
我正在使用nodejs和Max / MSP中的ws库。我正在编写一个脚本,该脚本应根据其过去成功连接的内容尝试连接到一些服务器IP,并且如果找不到正在运行的服务器来启动其自己的服务器,我希望能够手动设置尝试的连接超时(当在指定的IP上找不到服务器时,ws会返回错误代码“ ETIMEDOUT”,这是所希望的,但是周期为〜60,我想将其设置为短得多)
我一直在寻找有关堆栈溢出和github的解决方案,但没有发现与我的问题有关的任何问题
Max.addHandler('isRemoteServer?', (IPs) =>{
IPCounter = 0
IPs = IPs.split(" ")
Max.post('attempting to connect to ' + IPs[IPCounter])
function tryConnect(){
let url = 'ws://' + IPs[IPCounter] + ':8080'
connection = new WebSocket(url)
connection.onopen = () => {
connection.send('hello from ' + os.hostname)
Max.post('connected to remote at IP ' + IPs[IPCounter])
}
connection.onerror = (error) => {
errorCode = error.error.code
//console.log()
switch(errorCode){
case "ECONNREFUSED":
case "ETIMEDOUT":
Max.post('remote server not detected at ' + IPs[IPCounter] )
IPCounter++
if(IPCounter > IPs.length){
// no server dected running at any stored IPs, so run a server on this machine
runServer();
} else{
//try another IP
Max.post('attempting to connect to ' + IPs[IPCounter])
tryConnect()
}
break;
default:
Max.post('default ',JSON.stringify(error.error))
}
}
connection.onmessage = (e) => {
Max.post(e.data)
}
}
tryConnect()
})
该脚本运行良好,除了新尝试和ETIMEDOUT响应之间的时间间隔约为60秒,这对我来说太长了。我希望约10。