如何解决nodejs-websocket模块上的“写ECONNABORTED”错误?

时间:2017-10-09 15:38:18

标签: javascript node.js

我在index.js 中有一个名为C:\users\usuario\>的文件:

obj={}
require("http").createServer((i,o)=>{
o.writeHead(200,{"content-type":"text/html"})
o.write('<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/87/three.js"></script>')
o.write('<script>net=new WebSocket("ws://"+location.hostname+":9999");net.onmessage=(e)=>{eval(e.data)}</script>')
o.end()
},1).listen("8888",()=>{console.log("http protocol listening")})
require("nodejs-websocket").createServer((w)=>{
w.i=Math.floor(Math.random()*Math.pow(10,10))
obj[w.i]={x:0,y:0,z:10,xs:0,ys:0,zs:0,r:250,g:200,b:150,s:4}
w.sendText('k=[]')
w.sendText('m=[]')
w.sendText('obj={}')
w.sendText('document.title="hypogenix"')
w.sendText('document.body.style.margin="0"')
w.sendText('scene=new THREE.Scene()')
w.sendText('renderer=new THREE.WebGLRenderer()')
w.sendText('onmousedown=onmouseup=(e)=>{k[e.button]=e.type=="mousedown"}')
w.sendText('onkeydown=onkeyup=(e)=>{k[e.keyCode]=e.type=="keydown"}')
w.sendText('setInterval(()=>{'+
'net.send("update");'+
'for(i=0;i<500;i++){if(k[i]){net.send(i)}}'+
'for(i=0;i<5;i++){if(m[i]){net.send(i)}}'+
'},1)')
w.sendText('camera=new THREE.PerspectiveCamera(75,1,0.1,10000)')
w.sendText('document.body.appendChild(renderer.domElement)')
w.sendText('render=()=>{'+
'camera.aspect=window.innerWidth/window.innerHeight;'+
'renderer.setSize(innerWidth,innerHeight);'+
'camera.updateProjectionMatrix();'+
'requestAnimationFrame(render);'+
'renderer.render(scene,camera);'+
'}')
w.sendText('gen=(i,x,y,z,r,g,b,s)=>{obj[i]={'+
'shape:new THREE.Mesh(new THREE.BoxGeometry(s,s,s),'+
'new THREE.MeshBasicMaterial({color:"rgb("+r+","+g+","+b+")"}))};'+
'scene.add(obj[i].shape)}')
w.sendText('render()')
w.on("error",(e)=>{if(e.code!=='ECONNRESET'){throw e}})
w.on("text",(e)=>{
if(e==37){obj[w.i].xs-=0.001}
if(e==38){obj[w.i].ys+=0.001}
if(e==39){obj[w.i].xs+=0.001}
if(e==40){obj[w.i].ys-=0.001}
if(e==17){obj[w.i].zs-=0.001}
if(e==32){obj[w.i].zs+=0.001}
w.sendText('camera.position.x='+obj[w.i].x)
w.sendText('camera.position.y='+obj[w.i].y)
w.sendText('camera.position.z='+obj[w.i].z)
for(i in obj){w.sendText('if(obj['+i+']){'+
'obj['+i+'].shape.position.set('+obj[i].x+','+obj[i].y+','+obj[i].z+')}'+
'else{gen('+i+','+obj[i].x+','+obj[i].y+','+obj[i].z+','+obj[i].r+','+obj[i].g+','+obj[i].b+','+obj[i].s+')}')
}})}).listen("9999",()=>{console.log("ws protocol listening")})
setInterval(()=>{
for(i in obj){
obj[i].x+=obj[i].xs
obj[i].y+=obj[i].ys
obj[i].z+=obj[i].zs
obj[i].xs=obj[i].xs*99/100
obj[i].ys=obj[i].ys*99/100
obj[i].zs=obj[i].zs*99/100
}},1)
gen=(x,y,z)=>{obj[Math.floor(Math.random()*Math.pow(10,10))]={x:x,y:y,z:z,xs:0,ys:0,zs:0,r:200,g:200,b:200,s:1}}
gen(4,4,4)
gen(-4,4,4)
gen(4,-4,4)
gen(-4,-4,4)
gen(4,4,-4)
gen(-4,4,-4)
gen(4,-4,-4)
gen(-4,-4,-4)

我执行了node index.js命令,重新加载http://localhost:8888后不超过5次,服务器停止了,并给了我这个错误:

Error: write ECONNABORTED
at exports._errnoException (util.js:1018:11)
at Socket._writeGeneric (net.js:715:26)
at Socket._write (net.js:734:8)
at doWrite (_stream_writable.js:334:12)
at writeOrBuffer (_stream_writable.js:320:5)
at Socket.Writable.write (_stream_writable.js:247:11)
at Socket.write (net.js:661:40)
at Connection.sendText (C:\Users\usuario\node_modules\nodejs-websocket\Connection.js:142:23
at Connection.w.on (C:\Users\usuario\index.js:50:17)
at emitOne (events.js:96:13)

我想运行index.js文件,以便它正常工作并且永远不会停止。有帮助吗?不管怎样,谢谢。

0 个答案:

没有答案