AWS websocket 在连接时抛出 CORS 错误
AWS websocket throwing CORS error while connecting
我正在使用无服务器框架开发 AWS WebSocket 聊天应用程序。
我已经按照this article开发了websocketapi
但是当我尝试在我的 angualar 应用程序中使用这些 websocket 时出现抛出错误
from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
这是我的聊天模块
mport { SocketIoModule, SocketIoConfig } from 'ngx-socket-io';
import { ChatService } from './chat.service';
const config: SocketIoConfig = { url: 'wss://xyz.execute-api.us-east-1.amazonaws.com/dev', options: {} };
const routes: Routes = [
{ path: 'chat', component: ChatComponent }
];
@NgModule({
declarations: [ChatComponent],
imports: [
CommonModule, SocketIoModule.forRoot(config),
RouterModule.forRoot(routes),
],
providers: [ChatService],
exports: [ChatComponent]
})
export class ChatModule { }
这是我的聊天服务:
export class ChatService {
constructor(private socket: Socket) { }
sendMessage(msg: string) {
this.socket.emit("sendMessage", msg);
}
}
我已经使用了我在 sls deploy
之后得到的 url
这是浏览器错误:
只需在 function.events
下添加 cors:true
即可让所有事件通过 serverless.yml
文件中的交叉来源访问它们,例如:-
events:
- websocket:
route: $connect
authorizer: authorizerFunc
cors : true
并重新部署 lambda。
我正在使用无服务器框架开发 AWS WebSocket 聊天应用程序。
我已经按照this article开发了websocketapi
但是当我尝试在我的 angualar 应用程序中使用这些 websocket 时出现抛出错误
from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
这是我的聊天模块
mport { SocketIoModule, SocketIoConfig } from 'ngx-socket-io';
import { ChatService } from './chat.service';
const config: SocketIoConfig = { url: 'wss://xyz.execute-api.us-east-1.amazonaws.com/dev', options: {} };
const routes: Routes = [
{ path: 'chat', component: ChatComponent }
];
@NgModule({
declarations: [ChatComponent],
imports: [
CommonModule, SocketIoModule.forRoot(config),
RouterModule.forRoot(routes),
],
providers: [ChatService],
exports: [ChatComponent]
})
export class ChatModule { }
这是我的聊天服务:
export class ChatService {
constructor(private socket: Socket) { }
sendMessage(msg: string) {
this.socket.emit("sendMessage", msg);
}
}
我已经使用了我在 sls deploy
之后得到的 url这是浏览器错误:
只需在 function.events
下添加 cors:true
即可让所有事件通过 serverless.yml
文件中的交叉来源访问它们,例如:-
events:
- websocket:
route: $connect
authorizer: authorizerFunc
cors : true
并重新部署 lambda。