floatinghotpot / cordova-httpd

Embed tiny web server into Cordova with a plugin
285 stars 148 forks source link

wkwebview-engine-localhost VS cordova-httpd #47

Open roblav96 opened 8 years ago

roblav96 commented 8 years ago

Would it be possible to only use cordova-httpd and then in my config.xml set the

<content src="localhost:8080/index.html"/>

I'm going to try using both.

knobo commented 8 years ago

I would guess you should include http://

<content src="http://localhost:8080/index.html"/>
roblav96 commented 8 years ago

Ah good point! :)

Also how do you get around the ios9 ATS:

App Transport Security policy requires the use of a secure connection
knobo commented 8 years ago

But I guess it will not work as the index.js file sometimes contains initiation code, that is run before http server is started.

roblav96 commented 8 years ago

So far I'm having no luck lol

roblav96 commented 8 years ago

This is how I got it working on iOS and Android:

            cordova.plugins.CorHttpd.stopServer()
            cordova.plugins.CorHttpd.startServer( {
                'www_root': cordova.file.dataDirectory.replace( 'file://', '' ),
                'port': 8080,
                'localhost_only': true
            }, function ( url ) {
                this.$href = url
                console.warn( 'this.$href >', this.$href )
            }.bind( this ), function ( err ) {
                console.error( 'startServer >', err )
            } )

HERE:

cordova.file.dataDirectory.replace( 'file://', '' )
grattepoil commented 8 years ago

Hi @roblav96. Did you succeed to run the webserver using config.xml settings like you describe ?

roblav96 commented 8 years ago

Yes I did. I'm on mobile currently, so reply to this so I get a notification later and I'll post my full source

grattepoil commented 8 years ago

Hi rob. If you can share your source it would be very helpful. Thanks a lot. Le lun. 4 juil. 2016 à 08:24, Rob Laverty notifications@github.com a écrit :

Yes I did. I'm on mobile currently, so reply to this so I get a notification later and I'll post my full source

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/floatinghotpot/cordova-httpd/issues/47#issuecomment-230214019, or mute the thread https://github.com/notifications/unsubscribe/ACUQw1YK_iuIyVSi2ExupkKQ0Dtd8cSQks5qSKcKgaJpZM4IiLGs .

roblav96 commented 8 years ago

This is what you want:

document.addEventListener( 'deviceready', function () {
    let www_root = cordova.file.dataDirectory.replace( 'file://', '' )
    cordova.plugins.CorHttpd.startServer( {
        'www_root': www_root,
        'port': 8080,
        'localhost_only': true
    }, function ( url ) {
        if ( $globals_platform == 'Android' ) {
            url = url + '/'
        }
        $content_href = url
        console.log( '$content_href >', $content_href )
    }, function ( err ) {
        console.error( '$content_startServer >', err )
    } )
}, false )

So now you want to do something like:

let img = $content_href + 'image-file.png'

then bind the img to src of the img tag in HTML.

grattepoil commented 8 years ago

where do you write this code to apply it before the app launch the webview using config.xml setting <content src="http://localhost:8080/index.html"/> ? In the app.js file ? (i'm using ionic platform based on angularjs)

Thanks

roblav96 commented 8 years ago

You don't need to touch anything in the config.xml file.

<content src="index.html"/>

EDIT

Are you trying to use this instead of cordova's http-server for use with WK-WebView?

grattepoil commented 8 years ago

Ok thanks.

guyromb commented 7 years ago

Any idea how to use cordova-httpd instead of wkwebview-engine-localhost? So I can have the localhost server for both android and iOS (wkwebview-engine-localhost is only for iOS)? @roblav96

m0nstering commented 7 years ago

@roblav96 I'm not using Ionic, just Cordova. Where do I place your code? Thanks!

sunilrawat123 commented 6 years ago

How to replace the Origin : file:// , to something like Origin: http://192.168.10.152:8080

In case of android device.

I am using ionic v1. Please help.

MTTTM commented 5 years ago

now,it is 2019 year.anybody here?do it work?or have else resolution?