floatinghotpot / cordova-httpd

Embed tiny web server into Cordova with a plugin
284 stars 149 forks source link

Remote request #58

Closed arjunmenon closed 7 years ago

arjunmenon commented 7 years ago

Hey Is it possible to use this server as a middle ware to communicate with a Raspberry Pi server in the same network?

Sending a XmlHttpRequest directly from cordova app to a server in Pi is not possible, reason being it becomes a preflight request. And the server in Pi does not support cors.

So I was thinking to use this library to act as a gateway between the mobile app and the Pi. But I am not clear on the workflow? How do you run it that way. Like if I have this XHR request

var xmlHTTP = new XMLHttpRequest();
xmlHTTP.onreadystatechange = function() {
  if(xmlHTTP.readyState === xmlHTTP.DONE) {
    if(xmlHTTP.status === 200) {
        console.log(xmlHTTP.responseText)
    } else {
        console.log("not OK: " + xmlHTTP.status)
    }
  }
};
xmlHTTP.open('POST', 'http://192.168.1.6:49152/ctl/RenderingControl', true);
xmlHTTP.setRequestHeader("SOAPAction", "urn:schemas-upnp-org:service:RenderingControl:1#GetVolume");
xmlHTTP.setRequestHeader( "Content-Type","text/xml; charset=utf-8");

// build SOAP request
var sr = '<?xml version="1.0"?>'+
'<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'+
' <s:Body>'+
  '<u:GetVolume xmlns:u="urn:schemas-upnp-org:service:RenderingControl:1">'+
   '<InstanceID>0</InstanceID>'+
   '<Channel>Master</Channel>'+
  '</u:GetVolume>'+
 '</s:Body>'+
'</s:Envelope>';

xmlHTTP.send(sr);

How do you communicate now with this server and ask the server to pass on the post request to Pi?