colyseus / proxy

🔀⚔ Proxy and Service Discovery for Colyseus 0.10 ~ 0.14 (Not recommended on 0.15+)
https://docs.colyseus.io/scalability/
MIT License
37 stars 25 forks source link

Socket hang up #2

Open boygiandi opened 4 years ago

boygiandi commented 4 years ago

When I run this proxy, I get this error

Error: socket hang up at createHangUpError (_http_client.js:330:15) at Socket.socketOnEnd (_http_client.js:431:23) at Socket.emit (events.js:202:15) at Socket.EventEmitter.emit (domain.js:446:20) at endReadableNT (_stream_readable.js:1129:12) at processTicksAndRejections (internal/process/next_tick.js:76:17)

I tried to fix it and I found this, if I replace the target in original code, from this

  const proxy = httpProxy.createProxy({
    agent: http.globalAgent,
    target: { host, port },
    ws: true
  });

to this

  const proxy = httpProxy.createProxy({
    agent: http.globalAgent,
    target: `http://localhost:${port}`,
    ws: true
  });

It works. I don't really understand why. Maybe you can help me explain it. I fixed the code in my case, and I also replace the backend to localhost if it's the same with proxy IP.

MateusMendesSantana commented 4 years ago

Hi. I am having the same problem in an AWS EC2 environment

2020-03-27T14:51:50: PM2 log: Launching in no daemon mode
2020-03-27T14:51:50: PM2 log: [Watch] Start watching smadol
2020-03-27T14:51:50: PM2 log: App [smadol:0] starting in -fork mode-
2020-03-27T14:51:50: PM2 log: App [proxy:1] starting in -fork mode-
2020-03-27T14:51:50: PM2 log: App [smadol:0] online
2020-03-27T14:51:50: PM2 log: App [proxy:1] online
@colyseus/proxy listening at {"address":"::","family":"IPv6","port":8081}
Running on ws://localhost:3000
LISTEN add { processId: 'yOncuSTFq', address: '172.31.26.198:3000' }
Using proxy 0 /matchmake/joinOrCreate/place
PlaceRoom created!
Room is at proxy 0
place closed and saved
Using proxy 0 /matchmake/joinOrCreate/place
PlaceRoom created!
Room is at proxy 0
Using proxy 0 /matchmake/joinOrCreate/place
Room is at proxy 0
Proxy error during: /yOncuSTFq/5d7f7dbb7fa1810ad880b62c?sessionId=Wyrx5AICu
Error: socket hang up
    at connResetException (internal/errors.js:604:14)
    at Socket.socketCloseListener (_http_client.js:400:25)
    at Socket.emit (events.js:323:22)
    at TCP.<anonymous> (net.js:668:12)
place closed and saved
Proxy error during: /yOncuSTFq/5db44ac9d2303724dcebc330?sessionId=YRc1NPxo0
Error: socket hang up
    at connResetException (internal/errors.js:604:14)
    at Socket.socketCloseListener (_http_client.js:400:25)
    at Socket.emit (events.js:323:22)
    at TCP.<anonymous> (net.js:668:12)
Proxy error during: /yOncuSTFq/5db44ac9d2303724dcebc330?sessionId=aEBXNdal6
Error: socket hang up
    at connResetException (internal/errors.js:604:14)
    at Socket.socketCloseListener (_http_client.js:400:25)
    at Socket.emit (events.js:323:22)
    at TCP.<anonymous> (net.js:668:12)
MateusMendesSantana commented 4 years ago

For production envirioment the HTTP Proxy has no response for OPTIONS requests, so its will give us Cors error. See it there: https://github.com/http-party/node-http-proxy/issues/872

My temp solution:

const reqHandler = (req: http.IncomingMessage, res: http.ServerResponse) => {
  if (req.method === 'OPTIONS') {
    res.setHeader('access-control-allow-origin', '*');
    res.setHeader('access-control-allow-credentials', 'true');
    res.setHeader('access-control-allow-methods', '*');
    res.setHeader('access-control-allow-headers', '*');
    res.setHeader('access-control-max-age', 60 * 60 * 24 * 30);
    res.statusCode = 200;
    res.end();
    return;
  }
  const proxy = getProxy(req.url!);

  if (proxy) {
    proxy.web(req, res);

  } else {
    console.error('No proxy available!', processIds);
  }
};

After resolve this, i found another problem. image the increment_var in ecosystem.config.js does not increment. now I must pm2 kill then pm2 start every time. https://github.com/Unitech/pm2/issues/4502

[EDIT] Another problem. After rerun, the old instance data keep in Redis. image So the proxy try to access the old instance, then we will had the ECCONREFUSED error

[EDIT 2] to clean Redis cache in CI i used:

zgz682000 commented 3 years ago

Hi. I am having the same problem in an AWS EC2 environment

2020-03-27T14:51:50: PM2 log: Launching in no daemon mode
2020-03-27T14:51:50: PM2 log: [Watch] Start watching smadol
2020-03-27T14:51:50: PM2 log: App [smadol:0] starting in -fork mode-
2020-03-27T14:51:50: PM2 log: App [proxy:1] starting in -fork mode-
2020-03-27T14:51:50: PM2 log: App [smadol:0] online
2020-03-27T14:51:50: PM2 log: App [proxy:1] online
@colyseus/proxy listening at {"address":"::","family":"IPv6","port":8081}
Running on ws://localhost:3000
LISTEN add { processId: 'yOncuSTFq', address: '172.31.26.198:3000' }
Using proxy 0 /matchmake/joinOrCreate/place
PlaceRoom created!
Room is at proxy 0
place closed and saved
Using proxy 0 /matchmake/joinOrCreate/place
PlaceRoom created!
Room is at proxy 0
Using proxy 0 /matchmake/joinOrCreate/place
Room is at proxy 0
Proxy error during: /yOncuSTFq/5d7f7dbb7fa1810ad880b62c?sessionId=Wyrx5AICu
Error: socket hang up
    at connResetException (internal/errors.js:604:14)
    at Socket.socketCloseListener (_http_client.js:400:25)
    at Socket.emit (events.js:323:22)
    at TCP.<anonymous> (net.js:668:12)
place closed and saved
Proxy error during: /yOncuSTFq/5db44ac9d2303724dcebc330?sessionId=YRc1NPxo0
Error: socket hang up
    at connResetException (internal/errors.js:604:14)
    at Socket.socketCloseListener (_http_client.js:400:25)
    at Socket.emit (events.js:323:22)
    at TCP.<anonymous> (net.js:668:12)
Proxy error during: /yOncuSTFq/5db44ac9d2303724dcebc330?sessionId=aEBXNdal6
Error: socket hang up
    at connResetException (internal/errors.js:604:14)
    at Socket.socketCloseListener (_http_client.js:400:25)
    at Socket.emit (events.js:323:22)
    at TCP.<anonymous> (net.js:668:12)

I got the same error in my production env. I deploy the @colyseus/proxy service in a docker swarm cluster after a nginx reverse proxy.

@MateusMendesSantana have you solved your problem and do you has the same deployment with me?