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
562 views
in Technique[技术] by (71.8m points)

amazon web services - Add CORS options to AWS websocket API Gateway

I have deployed an AWS websocket API Gateway, backed by lambda functions. I can successfully connect and exercise the websocket service from websocketking.com

When I try connecting from my client app (Angular, socket.io v3) I get the following error:

Access to XMLHttpRequest at 'https://sockets.example.com/socket.io/?EIO=4&transport=polling&t=NRSfmDM' from origin 'http://www.example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

How do I configure an AWS websocket API gateway to support CORS?

Thanks.


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

1 Answer

0 votes
by (71.8m points)

I initially thought it can be done in same way as for HTTP API - via CLI - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-cors.html

However it seems there is no option to allow CORS for WebSocket protocol. I am seeing the message An error occurred (BadRequestException) when calling the UpdateApi operation: Cors is not supported for WEBSOCKET protocolType

Based on that I assume there is no support for the CORS at the moment for WebSocket protocol.

If it is an option for you, you can use custom domain for the API https://docs.aws.amazon.com/apigateway/latest/developerguide/websocket-api-custom-domain-names.html, so that both your web client and API will have same origin.


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

2.1m questions

2.1m answers

60 comments

57.0k users

...