簡體   English   中英

將 CORS 選項添加到 AWS websocket API 網關

[英]Add CORS options to AWS websocket API Gateway

我已經部署了一個 AWS websocket API 網關,由 lambda 功能支持。 我可以從 websocketking.com 成功連接並使用 websocket 服務

當我嘗試從我的客戶端應用程序(Angular,socket.io v3)連接時,我收到以下錯誤:

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.

如何配置 AWS websocket API 網關以支持 CORS?

謝謝。

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

但是,似乎沒有選項允許 CORS 用於 WebSocket 協議。 An error occurred (BadRequestException) when calling the UpdateApi operation: Cors is not supported for WEBSOCKET protocolType

基於此,我假設目前 WebSocket 協議不支持 CORS。

如果它是您的一個選項,您可以為 API https://docs.aws.amazon.com/apigateway/latest/developerguide/websocket-api-custom-domain-names.ZFC35FDC70D5FC22C7A5E3 70D5FC62C7A5E3 web 客戶端和 API 將具有相同的來源。

As answered by Milan, the AWS Websocket API Gateway protocol does not support CORS, and you would need to deploy your client-facing application to the same domain as your Websocket API.

I found that you will not need to specify CORS if you use WebSocket API on your client-side instead: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket on your client-side.

基本上,默認情況下,WebSocket 協議不會強制您的瀏覽器檢查 CORS 策略: https://www.freecodecamp.org/65socket56/how-to-be09

但是從 v3 開始,socket.io 明確要求您啟用它們: https://socket.io/docs/v3/handling-cors/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM