• Stars
    star
    282
  • Rank 145,874 (Top 3 %)
  • Language
    Objective-C
  • Created about 10 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Embed tiny web server into Cordova with a plugin

CorHttpd: embeded httpd for cordova

Supported platform:

  • iOS
  • Android

You can browse (locally or remotely) to access files in android/ios phone/pad:

  • browse the files in mobile device with a browser in PC.
  • copy files from mobile device to PC quickly, just with Wifi.
  • use cordova webview to access the assets/www/ content with http protocol.

Why http access is good?

  • Use wifi instead of cable, more convenient.
  • For security reason, browser does not support local AJAX calls. It's big bottle neck to deploy HTML5 games to Cordova platform.
  • The most popular phaser.js game engine, a httpd is always required, as it use AJAX to load resource.

How to use CorHttpd?

Add the plugin to your cordova project:

cordova plugin add https://github.com/floatinghotpot/cordova-httpd.git

Quick start, copy the demo files, and just build to play.

cp -r plugins/com.rjfun.cordova.httpd/test/* www/

Javascript APIs

startServer( options, success_callback, error_callback );

stopServer( success_callback, error_callback );

getURL( success_callback, error_callback );

getLocalPath( success_callback, error_callback );

Example code: (read the comments)

    var httpd = null;
    function onDeviceReady() {
        httpd = ( cordova && cordova.plugins && cordova.plugins.CorHttpd ) ? cordova.plugins.CorHttpd : null;
    }
    function updateStatus() {
    	document.getElementById('location').innerHTML = "document.location.href: " + document.location.href;
    	if( httpd ) {
    	  /* use this function to get status of httpd
    	  * if server is up, it will return http://<server's ip>:port/
    	  * if server is down, it will return empty string ""
    	  */
    		httpd.getURL(function(url){
    			if(url.length > 0) {
    				document.getElementById('url').innerHTML = "server is up: <a href='" + url + "' target='_blank'>" + url + "</a>";
    			} else {
    				document.getElementById('url').innerHTML = "server is down.";
    			}
    		});
    		// call this function to retrieve the local path of the www root dir
    		httpd.getLocalPath(function(path){
    			document.getElementById('localpath').innerHTML = "<br/>localPath: " + path;
        	});
    	} else {
    		alert('CorHttpd plugin not available/ready.');
    	}
    }
    function startServer( wwwroot ) {
    	if ( httpd ) {
    	    // before start, check whether its up or not
    	    httpd.getURL(function(url){
    	    	if(url.length > 0) {
    	    		document.getElementById('url').innerHTML = "server is up: <a href='" + url + "' target='_blank'>" + url + "</a>";
	    	    } else {
	    	        /* wwwroot is the root dir of web server, it can be absolute or relative path
	    	        * if a relative path is given, it will be relative to cordova assets/www/ in APK.
	    	        * "", by default, it will point to cordova assets/www/, it's good to use 'htdocs' for 'www/htdocs'
	    	        * if a absolute path is given, it will access file system.
	    	        * "/", set the root dir as the www root, it maybe a security issue, but very powerful to browse all dir
	    	        */
    	    	    httpd.startServer({
    	    	    	'www_root' : wwwroot,
    	    	    	'port' : 8080,
    	    	    	'localhost_only' : false
    	    	    }, function( url ){
    	    	      // if server is up, it will return the url of http://<server ip>:port/
    	    	      // the ip is the active network connection
    	    	      // if no wifi or no cell, "127.0.0.1" will be returned.
        	    		document.getElementById('url').innerHTML = "server is started: <a href='" + url + "' target='_blank'>" + url + "</a>";
    	    	    }, function( error ){
    	    	    	document.getElementById('url').innerHTML = 'failed to start server: ' + error;
    	    	    });
    	    	}
    	    	
    	    });
    	} else {
    		alert('CorHttpd plugin not available/ready.');
    	}
    }
    function stopServer() {
    	if ( httpd ) {
    	    // call this API to stop web server
    	    httpd.stopServer(function(){
    	    	document.getElementById('url').innerHTML = 'server is stopped.';
    	    },function( error ){
    	    	document.getElementById('url').innerHTML = 'failed to stop server' + error;
    	    });
    	} else {
    		alert('CorHttpd plugin not available/ready.');
    	}
    }

Credits

This Cordova plugin is built based on following 2 projects, and thanks to the authors.

  • NanoHTTPD, written in java, for java / android, by psh.
  • CocoaHTTPServer, written in Obj-C, for iOS / Mac OS X, by robbiehanson.

You can use this plugin for FREE. Feel free to fork, improve and send pull request.

If need prompt support, please buy a license, you will be supported with high priority.

More Cordova/PhoneGap plugins by Raymond Xie, find them in plugin registry.

Project outsourcing and consulting service is also available. Please contact us if you have the business needs.

More Repositories

1

casino-server

🔥 An online poker game server powered by Redis, node.js and socket.io
JavaScript
965
star
2

cordova-admob-pro

🔥 Cordova Plugin for Google AdMob, DFP, ADX. Easy monetization using mobile Ad, with single line of JavaScript. Compatible with Cordova CLI, Inoic, PhoneGap Build, etc.
Java
716
star
3

socket.io-unity

socket.io client for Unity, power game client with node.js back-end
481
star
4

cordova-plugin-admob

Basic Cordova Plugin for AdMob
C#
264
star
5

cordova-plugin-nativeaudio

The low latency audio plugin is designed to enable low latency and polyphonic audio from Cordova/PhoneGap applications, using a very simple and basic API.
Objective-C
234
star
6

coding-to-monetization

📖 From Coding to Monetization:Programmer's Financial Freedom Approach
189
star
7

cordova-plugin-sms

Plugin to operate SMS, send / list / intercept / delete / restore
Java
169
star
8

cordova-plugin-facebookads

Cordova/PhoneGap plugin for Facebook Audience Network Ads
Objective-C
65
star
9

protogen

CLI tool to parse protobuf .proto to C#, based on protobuf-net, works on Windows/Mac/Linux
XSLT
65
star
10

LiteCsvParser

A lite CSV reader and writer in C#, without any heavy dependency
C#
49
star
11

ipa-deploy

Deploy .IPA package to iOS device with a command line tool
Python
43
star
12

cordova-plugin-iad

Cordova plugin to support iAd on iOS
Objective-C
38
star
13

cordova-plugin-iflyspeech

Cordova plugin to support speech recognizer and synthesizer with iFlyTek voice cloud service
Objective-C
33
star
14

cordova-iad-pro

Cordova/PhoneGap Plugin for iAd with Banner and Interstitial
Objective-C
20
star
15

cordova-plugin-flurry

Cordova plugin to support Flurry (analytics and advertisement)
Objective-C
19
star
16

cordova-plugin-wifi

Cordova plugin to access mobile device Wifi info and operation
Java
19
star
17

admob-demo-game-phaser

Demo game for Cordova AdMob plugin, using phaser game engine
JavaScript
18
star
18

cordova-plugin-mopub

Cordova/PhoneGap plugin for MoPub Ads
Objective-C
17
star
19

raymud

Web-based MUD powered by nodejs and socket.io
CSS
16
star
20

gomoku

Gomoku, a HTML5 game working on PC and mobile device
JavaScript
13
star
21

hiquant

Quatitative trading framework and out-of-box toolset for assisting stock/fund investment
Python
13
star
22

magpie

General plugin framework for Cocos2d-x to call Cordova plugins
Shell
13
star
23

cordova-plugin-appleiap

Cordova plugin to support In-App Purchase on iOS
Objective-C
11
star
24

cordova-plugin-qq

Cordova plugin for Tencent QQ OpenSDK
Objective-C
11
star
25

admob-demo-app-ionic

JavaScript
11
star
26

UnityBatchBuild

Batch build toolset for Unity
Python
10
star
27

cordova-plugin-mmedia

Cordova/PhoneGap for Millennial Media Ad
Objective-C
8
star
28

cordova-smart-adserver

Cordova/PhoneGap Plugin for Smart Ad Server
Objective-C
8
star
29

cordova-mobfox-pro

Enhanced MobFox plugin for Cordova/PhoneGap. Banner, Interstitial and Video Ad. Support many other Ad network with server-side integration.
JavaScript
8
star
30

cordova-plugin-lianlianpay

Cordova/PhoneGap plugin for LianLianPay SDK
Java
7
star
31

plugin-verify

A simple utility to verify a Cordova plugin with its own test sample index.html
JavaScript
5
star
32

hotjs

A HTML5/javascript game/app framework written in javascript, in object-oriented way.
JavaScript
5
star
33

charset-convert

A command line tool to convert text file from one charset to another
JavaScript
5
star
34

cordova-plugin-paypalmpl

Cordova Plugin for PayPal MPL Library, not for new SDK.
Objective-C
5
star
35

mixin-pro

Improved javascript mixin to realize multiple inheritance for code reuse
JavaScript
4
star
36

cordova-admobsdk

Google Mobile Ads SDK for Cordova
Objective-C
3
star
37

cordova-admob-adapters

Cordova plugin for AdMob adapters
Objective-C
3
star
38

gomoku2

Gomoku HTML5 Game, newly written with HotJS Framework
JavaScript
3
star
39

cordova-plugin-ext

Extend the Cordova plugin base class with adapter interface. Plugin written based on this interface, can also be used for Unity, Cocos2d-X, and other frameworks.
Java
3
star
40

cordova-plugin-easyad

The easiest way to add AdMob/iAd to Cordova Apps
Objective-C
2
star
41

cordova-plugin-chartboost

Cordova plugin for Chartboost Ads
Objective-C
2
star
42

MagpieDemo

Demo Project for Magpie Framework, Call Cordova Plugins in Cocos2d-X Game
C++
2
star
43

knowledge-management

An Illustrated Guide to Knowledge Management
2
star
44

brackets-eclipse-theme

Eclipse light theme for Brackets
CSS
2
star
45

google-admob-sdk

Add Google Ads SDK to Cordova/PhoneGap project
Objective-C
2
star
46

webrpc

A reusable API server/client framework, running on socket.io
JavaScript
2
star
47

cordova-admob-xdk

Cordova AdMob Plugin for Intel XDK
C++
1
star
48

chinafund

Python
1
star
49

phonegap-jumprope

Jumping rope is cheap, portable, and burns more calories than you might think, try this mobile app
JavaScript
1
star
50

handycorp

A hybrid APP and its back-end, developed with javascript
JavaScript
1
star
51

testpaypalmpl

Demo project for cordova-plugin-paypalmpl
Java
1
star
52

admob-demo-game-pixi

Demo game for Cordova AdMob plugin, using PIXI game engine
JavaScript
1
star
53

cordova-facebook-sdk

Facebook SDK for Cordova
Objective-C
1
star
54

jump-rope

Jumping rope is cheap, portable, and burns more calories than you might think, try this mobile app
Java
1
star
55

cordova-plugin-vungle

Cordova Plugin for Vungle
Objective-C
1
star
56

cordova-plugin-appgrade

Cordova plugin for AppGrade
1
star
57

admob-demo-xdk

AdMob Demo for Intel XDK and Cordova Project
JavaScript
1
star
58

ajax-local

Some workaround to allow AJAX to load local resources
JavaScript
1
star