Comments (9)
After the event deviceready all plugins have been loaded. So if the event is fired you have to load up the webserver and start it. Create a function onDeviceReady and put the webserver code in there. So the webserver will start up after everything have been loaded.
Example:
function onDeviceReady() {
webserver.onRequest(
function(request) {
console.log("O MA GAWD! This is the request: ", request);
document.getElementById("server").innerHTML = "O MA GAWD! This is the request: " + request;
webserver.sendResponse(
request.requestId,
{
status: 200,
body: '<html>Hello World</html>',
headers: {
'Content-Type': 'text/html'
}
}
);
}
);
webserver.start(
function() {
alert('Success!');
},
function() {
alert('Error!');
},
1337
);
}
from cordova-plugin-webserver.
Hey
Thanks. That is working.
It takes some time to see the request. Any reason for that?
Also, is it possible for it to listen to other types of incoming methods, like a NOTIFY
request?
That request is send by a Upnp
eventing server when a client wants to be updated of events or changes in the Upnp
server.
A client sends a SUBSCRIBE
request (similar to a GET
request) with a callback url, like this webserver address and in return the Upnp sends a NOTIFY
response to the webserver, to tell if there are any changes.
I can see that it does not receive any response.
In python/ruby I can simply define a new do_POST
function and make it equal to a or listen to a do_NOTIFY
responses as well, when you start a simple HTTP webserver, to begin listening for responses, of that nature.
How can I do that here. Tell the webserver to listen to NOTIFY
responses as well.
EDIT -
NanoHTTPD says that
You can easily implement/customize any HTTP method, though.
And GCDWebServer implements handlers
to process incoming web requests and generating responses. Handlers are implemented with GCD blocks which makes it very easy to provide your own.
How can we do that?
from cordova-plugin-webserver.
Hi, why you should use a "NOTIFY" header? Can't you use just a default header like POST or GET?
from cordova-plugin-webserver.
I am listening to and interacting with a Upnp/Openhome server. They communicate with NOTIFY/SUBSCRIBE HTTP methods.
So the plugin can't receive any responses if they are set to default methods.
Since the core android/ios server gives customization, how can we use them.
from cordova-plugin-webserver.
Have you looked in the request object in the onRequest function for the "method" property?
webserver.onRequest(
function(request) {
console.log("Request method: ", request.method);
webserver.sendResponse(
request.requestId,
{
status: 200,
body: '<html>Hello World</html>',
headers: {
'Content-Type': 'text/html'
}
}
);
}
)
The library should listen to any HTTP Request method. Please try it out and tell me if it doesn't
from cordova-plugin-webserver.
Hey
Since it's part of the request object, nothin changes. I tried it though.
Simple curl GET/POST requests are shown. Doing a SUBSCRIBE request does nothing.
If it helps, you can check page 64(under eventing) inside this PDF document.
It details how to send a request. It mentions to send a SUBSCRIBE request. And it will get back a NOTIFY. Not a POST or GET.
I have a simple python server listening in on NOTIFY request to it.
It works there.
EDIT - Doing a cimple curl request with various HTTP methods
>$ curl -X POST http://192.168.1.2:1337
^C
>$ curl -X PUT http://192.168.1.2:1337
^C
>$ curl -X DELETE http://192.168.1.2:1337
^C
>$ curl -X NOTIFY http://192.168.1.2:1337
BAD REQUEST: Syntax error. HTTP verb NOTIFY unhandled.
>$ curl -X SUBSCRIBE http://192.168.1.2:1337
BAD REQUEST: Syntax error. HTTP verb SUBSCRIBE unhandled.
The server isn't configured to handle any customized HTTP methods.
This is what the simple python server shows when it listens a NOTIFY request
>$ python reflect.py
Listening on localhost:8081
----- Request Start ----->
/
HOST: 192.168.1.11:8081
CONTENT-TYPE: text/xml; charset="utf-8"
CONTENT-LENGTH: 1432
NT: upnp:event
NTS: upnp:propchange
SID: uuid:517b90a6-eefb-11e7-a56b-b70c55c1e054
SEQ: 0
<e:propertyset xmlns:e="urn:schemas-upnp-org:event-1-0">
<e:property>
<IdArray>AAAAAQAAAAIAAAADAAAABAAAAAUAAAAGAAAABwAAAAgAAAAJAAAACgAAAAsAAAAMAAAADQAAAA4AAAAPAAAAEA==</IdArray>
</e:property>
<e:property>
<TracksMax>16384</TracksMax>
</e:property>
<e:property>
<ProtocolInfo>http-get:*:audio/L16:DLNA.ORG_PN=LPCM,http-get:*:application/flac:*,http-get:*:application/x-flac:*,http-get:*:audio/flac:*,http-get:*:audio/x-flac:*,http-get:*:audio/aac:*,http-get:*:audio/x-aiff:*,http-get:*:audio/aif:*,http-get:*:audio/aiff:*,http-get:*:audio/dff:*,http-get:*:audio/x-dff:*,http-get:*:audio/dsd:*,http-get:*:audio/x-dsd:*,http-get:*:audio/dsf:*,http-get:*:audio/x-dsf:*,http-get:*:audio/m4a:*,http-get:*:audio/x-m4a:*,http-get:*:audio/mp1:*,http-get:*:audio/mp4:*,http-get:*:audio/mpeg:*,http-get:*:audio/x-mpeg:*,http-get:*:audio/ogg:*,http-get:*:audio/vorbis:*,http-get:*:audio/x-ape:*,http-get:*:audio/x-monkeys-audio:*,http-get:*:audio/wav:*,http-get:*:audio/x-wav:*,http-get:*:audio/wave:*,http-get:*:audio/x-ms-wma:*,http-get:*:audio/x-ogg:*,http-get:*:audio/x-scpls:*,http-get:*:audio/x-vorbis+ogg:*,http-get:*:audio/x-vorbis:*,http-get:*:audio/x-wavpack:*,http-get:*:video/mp4:*</ProtocolInfo>
</e:property>
<e:property>
<Id>16</Id>
</e:property>
<e:property>
<Shuffle>0</Shuffle>
</e:property>
<e:property>
<Repeat>1</Repeat>
</e:property>
<e:property>
<TransportState>Stopped</TransportState>
</e:property>
</e:propertyset>
<----- Request End -----
192.168.1.4 - - [01/Jan/2018 19:24:42] "NOTIFY / HTTP/1.1" 200 -
from cordova-plugin-webserver.
I have no time to improve the code. Sorry.
I think it's possible to retrieve other HTTP Methods than the usual ones.
from cordova-plugin-webserver.
Hey
Can you give example how to test that.
Because I think by default both of them handle only basic HTTP methods. Custom handlers have to be written.
So what did I miss in getting info on other HTTP methods? That would be helpful.
from cordova-plugin-webserver.
Hey @bykof
I just checked with the author. It is not possible to extend Nanohttpd. - NanoHttpd/nanohttpd#478
I guess for this task some other server is the only solution, like Jetty, which is truly extensible.
But since you are tied up with your studies, hope one day you can take a look.
from cordova-plugin-webserver.
Related Issues (20)
- Can't init `webserver` - Can't set a GET path.
- Could not send response to the client HOT 1
- 'GCDWebServer.h' file not found HOT 4
- [bug] Headers are not presented in case of file serving
- Feature request: From base64 to response HOT 5
- Unclear way to set up the java files to use with javascript
- Wait for response to be finished and return possible error
- 1.0.4 does not install properly HOT 1
- 'webserver' is not defined HOT 2
- Point webserver to dataDirectory? HOT 1
- "webserver" or "window.webserver" always undefined...
- Capacitor Support
- Websocket response HOT 2
- Unable to install HOT 1
- Binary Responses
- not working HOT 1
- Can you provide a license file please? HOT 2
- Website accessible from a browser, but not from fetch? HOT 1
- Build fails for ios. GCDWebServer is not configured correctly? HOT 1
- Function documentation in README not correct
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cordova-plugin-webserver.