使用socket.io进行轮询时出现404错误,没有进行轮询则显示200 OK

时间:2019-04-08 14:33:45

标签: spring-boot socket.io cors angular7

我正在使用(位于localhost:8680上的tomcat 9上)的spring boot服务器作为启用了CORS的纸牌游戏的后端。每当有人演出时,我都希望保持所有客户的最新状态。我用socket.io轮询了我从后端得到的响应。(在localhost:4200上)

没有轮询,代码可以正常工作,并且浏览器控制台没有显示CORS错误。但是当我使用socket.io进行轮询时会抛出错误

"Access to XMLHttpRequest at 'http://localhost:8680/socket.io/?EIO=3&transport=polling&t=MdzLHJ0' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."

Spring Boot API

@CrossOrigin
@GetMapping(value = "/currentState", produces ={MediaType.APPLICATION_JSON_VALUE})
    public ResponseDataModel currentState() {
        return testObjects.dummyResponseModel();
    }

Angular 7组件

ngOnInit() {
    this.socket = io.connect('http://localhost:8680/cards/currentState');
    this.socket.on('event',(data : any) =>{
      this.refreshPage();
    })
  }

refreshPage(){
    this.httpClient.get('http://localhost:8680/cards/currentState').subscribe((res : ResponseDataModel)=>{
      this.responseDataModel = res;
    });

  }

当我不使用socket.io时,会在调用“ http://localhost:8680/cards/currentState”时收到此响应标题,并且应用程序正常运行。

Access-Control-Allow-Origin: *
Content-Type: application/json;charset=UTF-8

我在角度组件中缺少什么吗? 以及为什么socket.io试图访问

'http://localhost:8680/socket.io/?EIO=3&transport=polling&t=MdzLHJ0'

0 个答案:

没有答案