nativescript-socketio connect() issue after upgrading - socket.io

i recently upgraded one of my projects to NS8 and thus nativescript-socketio to #triniwiz/nativescript-socketio.
Everything seems to work, but connect() does nothing, at all. No exception, no error. Juste nothing. I tried a blank project with just this in app.js :
import { SocketIO } from '#triniwiz/nativescript-socketio';
const socketIO = new SocketIO("http://1**.**.***.***:3001");
// connect
socketIO.connect();
And nothing happens, the server doesn't react.
The server works perfectly, i can reach it with other socket.io clients and a curl request without any problem.
curl "http://1**.**.***.***:3001/socket.io/?EIO=4&transport=polling"
95:0{"sid":"wsR7LhpisXyHhQpxAAAB","upgrades":["websocket"],"pingInterval":2000,"pingTimeout":2000}2:40
Is there anything new i need to do in NS8 for nativescript-socketio to work properly ?
Thank you :)

Ok, i finally solved the problem, it was just because i fall back from https to http, and then having a cors policy issue.
Adding
android:usesCleartextTraffic="true"
To AndroidManifest.xml did the trick.
From
<application
android:name="com.tns.NativeScriptApplication"
android:allowBackup="true"
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/AppTheme">
To
<application
android:name="com.tns.NativeScriptApplication"
android:allowBackup="true"
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:usesCleartextTraffic="true">

Related

Cordova : How to diagnose ajax not working for UWP (windows store) application

I have a simple Cordova application, where when built, and running as a Windows UWP application, has ajax calls are somehow being blocked my work network.
I have asked this many times before, but thought would try to reword, as have never got any solutions.
The application ajax calls work fine on my home machine, or whats seems to be most other networks. When it works, I can see all the output in Wireshark
When on my work machine, connected to our work Network, I see absolutely nothing in Wireshark. If I point to a server running on localhost, the app does work (but I see nothing in Wireshark, but perhaps this is because it is localhost)
If I run the app outside of the UWP container, ie I just run on the same machine, on the same network, but via the desktop browser (as you do for Cordova to debugging), it also works fine.
So it appears to be blocked before it even gets to the Network, as we see nothing in Wireshark at all, so this rules out host not reachable, CORS etc as it hasn't even been sent to the server.
I can run this in debug via Visual Studio, and I run the following test code...
document.addEventListener('deviceready', callUrl, false);
function callUrl() {
console.log('callUrl');
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
var DONE = 4; // readyState 4 means the request is done.
var OK = 200; // status 200 is a successful return.
console.log(xhr.readyState);
if (xhr.readyState === DONE) {
if (xhr.status === OK)
console.log(xhr.responseText); // 'This is the returned text.'
} else {
console.log('Error: ' + xhr.status); // An error occurred during the request.
}
}
xhr.open('GET', 'https://httpbin.org/get');
xhr.send(null);
};
onreadystatechangeis called twice, with xhr.readyState first 2 and then 4.
status is always 0.
How can I diagnose what is blocking this?? I have absolutely no idea. This is something low level, but how to see what? I have also looked through Windows Event logs, but can find nothing.
The Ajax call just returns 0 with a blank description. Our Network administrator just says there is something wrong with my app (I have tried multiple Cordova apps, including just basic test apps)
Thanks in advance for any help.
[EDIT1]
In response to the comment from #Xavier, all I have in my AppxManifest.xml (that I extracted from my built .appxupload file) is
<Capabilities>
<Capability Name="internetClient" />
</Capabilities>
There is some documentation on the capabilities here, where we have the following (right at the bottom of the page)..
The following capabilities are unavailable when deploying your Remote Mode application to the Windows Store:
Enterprise Authentication (enterpriseAuthentication)
Shared User Certificates (sharedUserCertificates)
Documents Library (documentsLibrary)
Music Library (musicLibrary)
Pictures Library (picturesLibrary)
Videos Library (videosLibrary)
Removable Storage (removableStorage)
Internet client/server (internetClientServer) - note that internetClient is still permitted
Private network client/server (privateNetworkClientServer)
I am not sure were we even set these in Visual Studio (config.xml), but also the above seems to be saying some of these can't be used?
Should the internetClient be enough (I only want to call out to a server as a client, not act as a server).
Also, this seems to be enough to work on most Networks except for my Work..
[EDIT2]
In response to the reply on the CSP...
There is a discussion on this here. The test Cordova app I created in Visual Studio does have this :
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
Surprisingly, I don't see this in my Ionic index.html, but perhaps this is because it is using the white list plugin?
My Ionic application has the following in the config.xml
<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<allow-navigation href="http://localhost:8080/*" />
....
<plugin name="cordova-plugin-whitelist" spec="^1.3.1" />
It would be great if the ajax even had some error saying it was something to do with CSP if that is what it is, but we just get nothing.
Once again, it is weird that the problem only occurs on my work network (either on cable or our WIFI). If I run the exact machine (e.g. a Surface tablet)over another connection (e.g. tether it to my phones cell), then all works as expected. So it must be some setting to do with the network (or firewall maybe), which also I find strange as surely I would at least then see something in Wireshark.
Would be great to be able to "debug into" the ajax call, and see where it is failing.
[EDIT 3]
After reading one of the comments, I used fiddler to see if I could see anything, which I do...
It even reports 200 (which is not correct), my request still fails.
According to this, you need to request the privateNetworkClientServer capability in order to communicate with a local network.
Note that this capability only works if you app is configured for local mode (your app will be rejected from the store if using this capability in remote mode).
To enable local mode, you need to set <preference name="WindowsDefaultUriPrefix" value="ms-appx://" /> in your cordova config.xml.
Note that local mode might lead to other issues, as you cannot use e.g. inline scripts in local mode (CSP violation)

Jquery ajax doesn't work on phonegap app

I have been working on a phonegap app and it was working perfectly fine until today when the Ajax post request would get stuck loading and the success or failure callbacks are not being called but when I try it on the browser it works perfectly fine.
Has anyone came across this problem and if so how did you manage to fix it?
Thank you in advance
EDIT:
This is my first hybrid app and i am not using desktop app. for now I am only targeting iOS and the target version is 3 and I am using build
The newer version of the phonegap automatically blocks all the requests made to any server. You need to follow the instructions to make it work.
Add the "Cordova Whitelist Plugin" in your config.xml
<gap:plugin name="cordova-plugin-whitelist" spec="1.1.0" />
Or simply
<plugin name="cordova-plugin-whitelist" spec="1.1.0" />
After that try putting the following in your config.xml
<content src="index.html" />
above mentioned is the path to your HTML file inside the source directory.
<access origin="*" />
will allow you to make request to any of the server using ajax. If you want to restrict your app to just a single domain then use something like this.
<access origin="http://yourdomain.com" />
At the end, put the following intents
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
For Android users who have this (I know that's not the original question):
I had the same problem, and spent a lot of time flailing unsuccessfully with the various whitelist directives as described above. Actually, my problem was different: my .apk file had not been correctly signed. Bizarrely, my Android phone did not kick it out at the point of trying to install the .apk: it simply failed as soon as I attempted to make an ajax call. I found the problem because Google Play refused to accept the upload.
I'm using Adobe PhoneGap Build to create the .apk: the problem was fixed when I added my Android keystore to the build (and disabled debug, which I don't think was relevant, but I'm not sure).
its work for me :
in path platforms/android update file AndroidManifest.xml :
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

Keep CanvasWatchFaceService Watchface From Sleeping?

Is this possible? I have tried inflating and adding a layout that has android:keepScreenOn="true" and I have tried creating a WakeLock during onCreate() like this:
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"WatchFaceWakelockTag");
wakeLock.acquire();
These seem to have no effect. I do have the permission set in my wearable app manifest:
<uses-permission android:name="android.permission.WAKE_LOCK" />
There is (at the moment) no way of keeping the screen on. Believe me I've tried... And even asked people at Google working on Android Wear.

Ajax error load Url from remote server in phonegap

I have been develop Phonegap android App. I put folder www in remote server, and in MainActivity(DroidGap) i loadUrl from server. I had config in Phonegap and App running in device ( load and show page from server). But problem when i send request by Ajax to server then it fail. It could not connect to any host. Please help me? Thanh you so much.
- MainActivity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("http://172.16.3.198:8080/ServerMail/index.html");
}
- Config.xml
<access origin="*"/>
<content src="http://172.16.3.198:8080/ServerMail/index.html" />
the app does not have the internet permission. you can edit AndroidManifest.xml, and something like below:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.petro"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
Usually you have your 'site' files local to the app, not on a remote server, otherwise there is little point to Phonegap.
Here you might find that your URL is simply not reachable. A 172.16.x.x address is local to the network the server is hosted on, perhaps your phone is not on that network, especially if you are using 3G to connect.
Also, bare in mind that depending on the phone you use, you may be using an old version of webkit. As I've developed Phonegap apps, I have run into problems with jQuery 2 and older Android releases (2.1 for example). It seems to me that what you're doing is not a good use of Phonegap, and you might be better off using a shortcut instead.

How do I prevent IIS 7.5 from caching symlink content?

I have set up IIS 7.5 to statically serve some files, and some of these files are actually symbolic links (created by mklink).
Even if I disabled both kernel and user caching, these files seems to be cached somehow by IIS. And IIS is still serving old versions after the files are modified.
To be sure that it is not caused by ASP.NET, I've created a dedicated unmanaged AppPool. I have also checked that these file are not cached by browsers.
My web.config is following:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="true" />
<caching enabled="false" enableKernelCache="false" />
<urlCompression doStaticCompression="false" doDynamicCompression="false" />
<staticContent>
<clientCache cacheControlMode="DisableCache" />
</staticContent>
</system.webServer>
</configuration>
There are several people mentioning this problem:
http://forums.iis.net/t/1166077.aspx
http://forums.iis.net/t/1171204.aspx
Any hints how to solve this problem?
This problem drove me nuts for like a month a while back. You have to disable IIS caching in the registry, as far as I know this isn't documented anywhere for IIS 7 but instead is an old IIS 5 trick that still works. You can either turn the below into a .reg file and import it or you can just navigate to the section and add it manually. I recommend rebooting after changing this parameter, I'm not sure if IIS picks it up after just an iisreset.
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\InetInfo\Parameters]
"DisableMemoryCache"=dword:1
I was previously able to fix this issue on IIS7 with Banin's fix. Since then, I have moved to Windows 10 with IIS 10, and suffered the same problem again. DisableMemoryCache did not help.
I then disabled kernel caching, and for now, that seems to fix the issue (I'm sorry for the Dutch in the screenshot):
Banin's solution worked for me. The issue was resolved after changing registry parameter and resetting IIS. The C# program below (you can use LINQPad to run it) will help you reproduce the issue:
using System.IO;
using System.Net;
void Main()
{
var virtualPath = "JunctionPoint/sample.js";
var physicalPath = $#"C:\IISROOT\JunctionPoint\{virtualPath}";
for (int i = 0; i < 100; i++) {
File.WriteAllText(physicalPath, i.ToString());
Console.Write(i + "=");
var client = new WebClient();
string html = client.DownloadString($"http://localhost/{virtualPath}");
Console.WriteLine(html);
if (i.ToString() != html) {
Console.WriteLine("Issue reproduced!!!");
}
}
}

Resources