Reputation: 1588
this is one is very very aggravating. I cannot connect to my local host socket io server after compiling and running on a device. HOWEVER I can connect if I launch it through a browser...
so to recap:
$ ionic serve
will launch on my browser and I connect to the socketio server running on localhost port 8080
$ ionic cordova run android --device --livereload
gives the error:
zone-evergreen.js:2952 GET http://localhost:8080/socket.io/?EIO=3&transport=polling&t=MyU0Jo6 net::ERR_CONNECTION_REFUSED
thusly I am unable to connect to localhost running on port 8080. Between launching on my browser or device I change no connection
<script src="http://localhost:8080/socket.io/socket.io.js"></script>
import { SocketIoModule, SocketIoConfig } from 'ngx-socket-io';
const config: SocketIoConfig = { url: 'localhost:8080', options: { secure: true } };
@ngModule({
imports: [
SocketIoModule.forRoot(config),
]
})
require('dotenv').config({ path: __dirname + '/.env' });
const app = require('express')();
const PORT = process.env.CHAT_PORT || 8080;
const server = require('http').createServer(app);
const io = require('socket.io')(server);
server.listen(PORT, (): void => console.log('listening', PORT));
io.on('connection', socket => console.log('connected'));
if there is any other information you need let me know and I'll update
Upvotes: 0
Views: 1399
Reputation: 9235
So here is what I think is happening:
When you are running ionic serve on your PC, your index.html code points at the same local PC's running "web server" (points at "self" basically):
<script src="http://localhost:8080/socket.io/socket.io.js"></script>
When you are running live reload, the index.html is run on your mobile device attempting to point at..."localhost" (self again) which is NOT your PC but your mobile device, which is not running socket.io...
To fix you need to serve socket.io from your PC but your code should point at your PC via absolute address (not via localhost). For example if your local PC has local network address of 192.168.0.5 -> you should point the init code to that address and ensure your mobile device when connected to the same network has its own 192.168.0.X address.
Hope this makes sense
Upvotes: 1