I am developing an app in ionic and I can't connect to the socket server when running on a device.
I have managed to connect my app (port:8100) to a node server(port:9000) locally when developing (via ionic serve), I don have a cors issue, everything runs fine.
Client looks something like this :
socket = new io.connect('http://192.168.1.106:9000');
socket.on('reconnect_attempt',function() {
alert('crap');
});
P.S: the above code is from my own memory, but I know I have provided the ip and port
If the server is working or not I still get the alert...
I have also checked the manifest of the app and it has the appropriate permissions:
android.permission.INTERNET
I also added a js error handler to check if there is a js error:
window.onerror = function (errorMsg, url, lineNumber) {
alert(errorMsg);
}
var MyApp = angular.module('scotch-todo', ['ionic']);
...
I had a small error and fixed it (so the handler works) but now nothing is catched...
If I understand correctly, you can connect when developing on your PC, but now when testing on your physical device.
Since Cordova v4, they changed the default behavior so it won't connect to any address outside its own WebView pages. There exists a cordova whitelist plugin that lets you connect to a list of address in the whitelist
http://docs.ionic.io/docs/cordova-whitelist
Related
My app is trying to lock desktop's mouse and keyboard using a mobile app, which is already working.
I am already using the desktop IP but now trying to use http://desktopName as GET URL for my android app but it is not working.
I am using Framework7 app.request.get for my client.
CLIENT
// var server = "http://192.168.31.147:8089" // working in mobile browser and app
var server = "http://desktop-s5n5hiq:8089" // not working in mobile browser and app
$$("#server").html(server)
app.request.get(server+"/connect").then((res) => {
var data = res.data
I am using node http module for my server.
SERVER
This server is already working without issue.
var http = require('http');
var host = "desktop-s5n5hiq"
var port = 8089
http.createServer(function (req, res) {
...
}).listen(port,host,()=>{
console.log("running server: http://"+host+":"+port)
});
My server IP is constantly changing because my router has no IP lock or IP reservation option, that's why I want to use the desktop name instead (http://desktopName), however, I am not getting any response from my server when I use the desktopName
These are already working:
Connect to http://desktopName using desktop and laptop browser
Connect to IPs http://192.168.1.* using desktop, laptop and mobile browser
Not working:
Connect to http://desktopName using mobile browser. (No "Desktop is connected" log)
Any comment is much appreciated.
Edited:
I forgot to mention that we have 2 routers one is an extender. The main one has this 192.168.1.1 DNS server, however, the other has 192.168.31.1. During desktop boot, it sometimes connects to the main router and sometimes to the extender.
I have tried this solution https://kb.netgear.com/27476/How-do-I-set-a-static-IP-address-in-Windows (added 192.168.1.5) but I think it will cause an issue if the PC connected to the extender.
I have to improve existing Ionic app using firebase as auth method for users login.
For my purpose i cannot touch existing list of previously registered users in production firebase app, I have to create new list locally using firebase emulator.
After adding and setup local auth & DB emulator extensions, I have added this into app.module.ts:
if (window.location.hostname === 'localhost') {
firebase.auth().useEmulator('http://localhost:9099');
}
After continuing with adding new UI and logical features, using a web browser to test my work, I switch to the Android platform. After running the app and executing
firebase.auth().signInWithEmailAndPassword(email, password);
I got the following error:
{
"code": "auth/network-request-failed",
"message": "A network error (such as timeout, interrupted connection, or unreachable host) has occurred."
}
It worked fine in the browser, but not on real device.
Whereas on Android device
window.location.hostname === 'localhost
this will be executed
firebase.auth().useEmulator('http://localhost:9099');
so Firebase searching auth service on device on port 9099, that not exists, and produce such error.
To fix this just run in terminal
adb reverse tcp:9099 tcp:9099
I'm trying to write a mobile app via Capacitor that makes use of PouchDB. When I run the app in the emulator via Android Studio the connection to the remote CouchDB instance fails. I've tracked this down to a failure in the fetch API for certain URLs when running on Android.
To debug I made a minimal web application and wrapped it using Capacitor to run on Android. The app includes the following code
const testFetch = (url) => {
console.log("Testing fetch", url)
fetch(url)
.then((response) => response.text())
.then((t) => {
console.log("Respose from fetch:", url)
console.log(t)
console.log("that was it")
})
.catch((reason) => {
console.log("FETCH FAILED", url, reason)
})
}
I then have three tests:
testFetch("https://jsonplaceholder.typicode.com/todos/1"); // just some JSON
testFetch("http://10.0.2.2:5984/simple"); // local pouchdb instance
testFetch("http://10.0.2.2:8080/sample.json"); // local http server + CORS
The second two use the IP address that is an alias for the development machine when running in the Android emulator. I confirmed that I can access all of these URLs from the browser on the emulator but the app succeeds on the first and fails on the second two (error: TypeError: Failed to fetch). When running the base web app in the browser, all succeed (with localhost instead of 10.0.2.2).
CORS headers are in place on all URLs. As far as I can see the app doesn't even try to access the two servers that fail - no HEAD requests for example. I've also tried various other URLs and can't see a pattern to the failures -- eg. it's not the port number != 80.
Any clues as to what is going on would be appreciated.
So the thing I didn't notice that the failing URLs had in common was http rather than https. It turns out that fetch silently fails to work for any http URL, just giving the error 'Failed to fetch'.
I'm not sure whether this is a feature of the Android web view or of Capacitor itself. The Capacitor docs suggest that using https is a good idea but not that http won't work.
This policy doesn't get altered by setting a Content Security Policy in the main page header.
The original goal was to connect a local PouchDB database to a remote CouchDB instance. This now works as long as the CouchDB instance is served via https. Without that you just get silent failure to sync.
I'm new on node.js and azure.
I want to develop a mobile app which insert and select data in a table.
For the android part - client side I use the code from here.
For the node.js part - server side I use the code from the same repository this code
When I deploy the node.js project on azure I give an error:
I have this message if a use any code for mobile node.js.
In Container diag, the problem is at PORT:
Check your Application Settings to make sure that the PORT setting of your container is correct. You can also view Application Logs to determine if there was a wrong PORT set.
In application logs the port is 8080:
ERROR - Container antaresmobilejs_0_7b069d42 for site antaresmobilejs
has exited, failing site start ERROR - Container
antaresmobilejs_0_7b069d42 didn't respond to HTTP pings on port: 8080,
failing site start. See container logs for debugging.
In my node.js code, I set the port to 8080, but I don't find the port settings in application settings.
Actually, the Node.js backend port for Azure Mobile Apps should be set as below if using Express.js.
app.listen(process.env.PORT || 3000);
It was introduced in the offical document How to use the Mobile Apps Node.js SDK, as the figure below.
It also be coded in the offcal sample code https://github.com/Azure/azure-mobile-apps-quickstarts/blob/master/backend/node/TodoSample/app.js#L35
The code process.env.PORT will read the default port specified by Azure from the environment, and the web.config file will help node backend app to start up by IIS.
The real port of API endpoint for client is 80, because the node app hosted in IIS.
I'm a novice in app development. I'm using the Intel XDK to make an Android app.
The app works by connecting to a database in a backend done with Symfony, and is behaving fine in the emulator. I want to test now from a real device, using my PC as localhost, through WiFi. I read that you have to retrieve the localhost IP address with ipconfig, and then use that IP for the connection. However, when I test the app it doesn't connect. The strange thing is that if I use the mobile browser with the same URL, the connection works: I can see the XML files that I use to fetch the database information.
The code for the connection in the app:
xmlhttp=new XMLHttpRequest();
xmlhttp.onerror=transferFailed;
xmlhttp.open("GET","http://192.xxx.x.x/symfony/web/app_dev.php/api/v1/reportes/reportes.xml",false);
The onerror function:
function transferFailed(e) {
alert("Error al conectar:"+e.target.status);
}
As I said, the connection doesn't work and I get the alert message: "Error al conectar: 0"
If I put the address http://192.xxx.x.x/symfony/web/app_dev.php/api/v1/reportes/reportes.xml in the mobile browser I get the correct XML file.
Edit: Well it seems the problem was the sycnhronus request. Later i had to parse the response xml. I dont' know why in the emulator and in an app that i made in phonegap some time ago it worked without parse. The code:
function mapIni() {
xmlhttp=new XMLHttpRequest();
xmlhttp.onerror=transferFailed;
xmlhttp.open("GET","http://localhost/symfony/web//app_dev.php/api/v1/reportes/reporte.xml",true);
xmlhttp.onload= function (event) {
alert("onload:"+xmlhttp.response);
};
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
xmlDoc=xmlhttp.responseXML;
xmlDoc=xmlhttp.responseXML;
xmlDoc = new DOMParser().parseFromString(xmlhttp.responseText,'text/xml');
x=xmlDoc.getElementsByTagName("entry");
alert("x[0]:"+x[0].getElementsByTagName("tipo")[0].childNodes[0].nodeValue);
}
}
xmlhttp.send();
}
It may be failing because of Cross-Origin request, add this script tag: <script src="xhr.js></script>" and try on device. You don't need the actual xhr.js file, just add the script tag in index.html, the Intel XDK build will include it to workaround the Cross-Origin policy.
I can see two possible issues. Synchronous use of XHR is often a problem if you are expecting the rest of your javascript runtime to be responsive while the network request is executing.
I would first try asynchronous requests (which really should be preferred anyway):
xmlhttp.open("GET","...",true);
You may have to send the request after opening it (this is certainly true for non-GET requests, but maybe GETs work without sending, but I'm suspicious)
xmlhttp=new XMLHttpRequest();
xmlhttp.onerror=transferFailed;
xmlhttp.open("GET","...",true);
xmlhttp.send();
Finally, presumably you have some handler attached to the onload event for when this does work for you :)
xmlhttp.addEventListener('onload', function (event) {
console.log(xmlhttp.response);
});
Additionally, while the simulator is on the same machine and recognizes localhost, your actual Android device cannot interpret where is localhost. For the device build, change localhost to the server name, under windows this the computer name that was assigned when you first installed windows, and test through a browser ensuring that you can access the files via the computer name. Once verified, than change localhost to your computer name. You also have to ensure CORS is permitted and enabled.