Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
497 views
in Technique[技术] by (71.8m points)

reactjs - api gateway websocket can't connect to AWS EC2 socket.io

Locally everything works, nodejs:

io = require('socket.io')(server, {
  cors: {
    origin: '*',
  }
});

Next 10.0.3 client:

import { io } from 'socket.io-client';
...  
const [socket, setSocket] = useState<SocketIOClient.Socket>();
  useEffect(() => {
    (async () => {
      if (!socket) {
        setSocket( io('ws://localhost:3030') );
      } else {
        socket.on('connection', () => {
          console.log('connected_ ');
        });
        socket.on('disconnect', () => {
          socket.disconnect();
        });
        socket.on('message', (message: any) => {
           ...
        });
      }
    })();
    return () => {
      if (socket) {
        socket.disconnect();
      }
    }
  }, [socket]);

Deployed my REST Api Gateway: /{proxy+}, client certs, API keys provides SSL endpoints all working.

Whatever config -> Api Gateway for Websocket, my React client logs every 1 sec:

Access to XMLHttpRequest at 'https://aby8ysdpcfg.execute-api.ap-southeast-2.amazonaws.com/production/?EIO=4&transport=polling&t=NT5V7jb' from origin 
'https://abcvds4mhutxa9a.cloudfront.net' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: 
No 'Access-Control-Allow-Origin' header is present on the requested resource.

React Client: tried params to io() all combinations of:

{
            extraHeaders: {
              Authorization: `Bearer ${user.token}`,
              "x-api-key": `${process.env.API_GATEWAY_KEY}`
            },
            path: '/production',
            port: 80
          }

Api Gateway: tried Routes, Stages, Settings, but doesn't hit the server or show traffic on the gateway Dashboard. (Routes $connect, $default as HTTP and AWS Service -> EC2 (subdomain?)). wscat connects to the wss://.../production and shows traffic on the Dashboard.

question from:https://stackoverflow.com/questions/65929179/api-gateway-websocket-cant-connect-to-aws-ec2-socket-io

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...