Quantcast
Channel: Ionic Framework - Ionic Forum
Viewing all articles
Browse latest Browse all 48980

Socket.io in android throws a CORS error

$
0
0

Hi!

Im trying to conect my ionic app with my api with socket.io.

Im using this library:

https://github.com/rodgc/ngx-socket-io

And this is my configuration:

Server side:

const conexion = require("./connect");
let app = require('express')();
let server = require('http').createServer(app);
let io = require('socket.io')(server,{ 'cors': { 'methods': ['GET', 'PATCH', 'POST', 'PUT'], 'origin': true}});


app.get('/',function(req,res){
//res.status(200).send(conexion.hi());
console.log("eoo");
conexion.conectar();
});

io.on('connection', (socket) => {
 socket.on('intra',function(){
  console.log("dentro");
});

});

var port = process.env.PORT || 3001;

server.listen(port, function(){
   console.log('listening in http://localhost:' + port);
});

app.module.ts

import { NgModule } from '@angular/core';

import { BrowserModule } from '@angular/platform-browser';

import { RouteReuseStrategy } from '@angular/router';

import { IonicModule, IonicRouteStrategy } from '@ionic/angular';

import { SplashScreen } from '@ionic-native/splash-screen/ngx';

import { StatusBar } from '@ionic-native/status-bar/ngx';

import { AppComponent } from './app.component';

import { AppRoutingModule } from './app-routing.module';

import {HTTP} from '@ionic-native/http/ngx';

import {GPSService} from './gps.service';

import {HttpService} from './services/http.service';

import {BLE} from '@ionic-native/ble/ngx';

import { Geolocation } from '@ionic-native/geolocation/ngx';

import { BackgroundGeolocation} from '@ionic-native/background-geolocation/ngx';

import { BackgroundMode } from '@ionic-native/background-mode/ngx';

import { SocketIoModule, SocketIoConfig } from 'ngx-socket-io';

const config: SocketIoConfig = { url: 'http://just4goals.com/:3001', options: {} };

/*extra*/

import { LocalNotifications } from '@ionic-native/local-notifications/ngx';

@NgModule({

  declarations: [AppComponent],

  entryComponents: [],

  imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule, SocketIoModule.forRoot(config)],

  providers: [GPSService,

    BLE, 

    HTTP,

    Geolocation,

    StatusBar,

    SplashScreen,

    HttpService,

    BackgroundMode,

    BackgroundGeolocation,

    LocalNotifications,

    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }

  ],

  bootstrap: [AppComponent]

})

export class AppModule {}

And here is the problem.
When I launch the app in my android phone with:

ionic cordova run android --device

and I check if this is working in the Chrome developer console I recieve this message:

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

And if I try to connect with the socket, this doesnt work…

I check some optiones in this forum, but doesnt work.

¿Do u know how to solve it?

Thanks!

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 48980

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>