HTTP interface / requests status and JQuery

All you've ever wanted to know about the ActiveX, Mozilla plugins, the web interface and various PHP extensions
cking2600
New Cone
New Cone
Posts: 4
Joined: 17 Feb 2014 05:37

HTTP interface / requests status and JQuery

Postby cking2600 » 17 Feb 2014 08:38

Hi, I am trying to get the status of the currently playing file. I want to use JQuery, so I can embed it in a webpage to build a custom remote control. If i use IE, i can get the status via http://myIP:8080/requests/status.json. I can also pass commands i.e. ?command=pl_pause. No problems when doing it via a web browser. However, if I try to use JQuery inside of Visual Studio, I have no luck. I have tried with the new version 2.1.3 (one that requires a password) and an older version 2.0.1.
I have connecting to 2 different machines. I have also uninstalled and re-installed numerous times. No problems if i put the command in the address bar of the web browser, though.
Here is the code I am using. I got it from another post a page back, but it isn't working for me.
Any help is appreciated.
Thanks.

checkConnection = function () {
//test user settings
$.ajax({
url: 'http://192.168.1.2:8080/requests/status.json',
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "Basic " + ":vlcplayer");
},
success: function (data, status, jqXHR) {
alert("yes");
},
error: function (data) {
alert("error");
}
});
};

cking2600
New Cone
New Cone
Posts: 4
Joined: 17 Feb 2014 05:37

Re: HTTP interface / requests status and JQuery

Postby cking2600 » 17 Feb 2014 19:38

I have also tried this, but it isn't working.

function getPage() {

if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else{
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
};

xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
document.getElementById('mediaTitle').innerHTML = xmlhttp.responseText;
};
};
xmlhttp.open('GET', 'http://192.168.1.2:8080/requests/status ... d=pl_pause', true);
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlhttp.setRequestHeader("Authorization", "Basic " + btoa(":vlcplayer"));
xmlhttp.send();
};


This does work in code behind. But, i want to do it all on the client side.
Uri requestUri = null;
Uri.TryCreate("http://192.168.1.2:8080/requests/status ... d=pl_pause", UriKind.Absolute, out requestUri);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUri);
string credentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes("" + ":" + "vlcplayer"));
request.Headers.Add("Authorization", "Basic " + credentials);
request.Method = WebRequestMethods.Http.Get;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(response.GetResponseStream());

I don't see what the difference is.
Any help is appreciated.
Thanks.

cking2600
New Cone
New Cone
Posts: 4
Joined: 17 Feb 2014 05:37

Re: HTTP interface / requests status and JQuery

Postby cking2600 » 17 Feb 2014 22:48

After a little more troubleshooting, it seems I can connect and control playback with the old version. The problem I am having with that is the Get command always returns as an error. It never performs the success function. For this reason I can't see the returned JSON.
When I have Fiddler running, I can see that the connection was successful and even see the proper JSON response. VLC even performs the command that was sent.

Any idea what I am doing wrong with my JQuery?

Working with old version of VLC:

function GetData() {
$.ajax({
type: "GET",
url: "http://192.168.1.2:8080/requests/status ... d=pl_pause",
success: function (msg) {
alert("good");
},
error: function (e) {
alert("error");;
}

});
};

When I try to control the new version of VLC, Fiddler is showing 401 - Unauthorized or 501 - Not implemented errors, depending on the type of headers I try to pass.

Any ideas?

Thanks.

cking2600
New Cone
New Cone
Posts: 4
Joined: 17 Feb 2014 05:37

Re: HTTP interface / requests status and JQuery

Postby cking2600 » 19 Feb 2014 06:27

Solution found... sort of.
I was never able to get a successful response when passing credentials from the web page. I was however able to get one from code behind. So, I created a web service with the working code behind. So now I call my webservice from the web page. The web service connects to VLC HTTP server of the selected room. The VLC HTTP server then passes back the JSON response. The web service then parses that response and pulls out only the attributes that I want. It will then serialize those attributes back into JSON and send it back to the web page.
I think I like this solution better, because it decouples the video player from the web page. If I want to use a different media player (don't know why I would), I just need to create a web service for that player and my web page doesn't have to change. The media player is defined in the settings of the web page.
I just wanted to post this in case other people were having similar issues.

Hope this helps.

using System.Net;
using System.Text;
using System.Windows.Forms;
using Newtonsoft.Json;
using System.Web.Script.Serialization;
using System.IO;

//Pass the IP of the VLC server you are connecting to
public Object GetPlayingMedia(string strServer) {
//File attributes
string strFullscreen = "";
string strState = "";
string strTime = "";
string strLength = "";
string strAspectratio = "";
string strVolume = "";
string strFileName = "";
string strFrameRate = "";
string strFormat = "";
string strVideoCodec = "";
string strVideoType = "";
string strResolution = "";
string strBitRate = "";
string strAudioType = "";
string strChannels = "";
string strSampleRate = "";
string strAudioCodec = "";

//Request attributes
string my_strServer = strServer;
string strResponse = "";
string strPassword = "vlcplayer";
string strUser = "";
string strCredentials = "";
HttpWebRequest request = null;
HttpWebResponse response = null;
Uri requestUri = null;
string my_strJSON = @"{""vlc"":[";

//Connect to server
Uri.TryCreate("http://" + my_strServer + ":8080/requests/status.json", UriKind.Absolute, out requestUri);
request = (HttpWebRequest)HttpWebRequest.Create(requestUri);
strCredentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(strUser + ":" + strPassword));
request.Headers.Add("Authorization", "Basic " + strCredentials);
request.Method = WebRequestMethods.Http.Get;
response = (HttpWebResponse)request.GetResponse();

using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
strResponse = reader.ReadToEnd();
dynamic objFile = JsonConvert.DeserializeObject(strResponse);

strFullscreen = objFile.fullscreen;
strState = objFile.state;
strTime = objFile.time;
strAspectratio = objFile.aspectratio;
strLength = objFile.length;
strVolume = objFile.volume;

//Get stats
foreach (var obj in objFile["stats"])
{
string objValue = (string)obj.Value;
string objName = (string)obj.Name;
}

//Get information
foreach (var obj in objFile["information"])
{
string objName = (string)obj.Name;
switch (objName)
{
case "category":
//Get category
foreach (var objCategory in obj.Value)
{
string objCatName = (string)objCategory.Name;
switch (objCatName)
{
//Get Meta
case "meta":
foreach (var objMetaName in objCategory.Value)
{
string objMetadataName = (string)objMetaName.Name;
switch (objMetadataName)
{
//Get filename
case "filename":
strFileName = objMetaName.Value;
break;
}
}
break;
case "Stream 0": //video
foreach (var objStream0Name in objCategory.Value)
{
string objStreamName = (string)objStream0Name.Name;
switch (objStreamName)
{
//Get Stream 0
case "Frame_rate":
strFrameRate = objStream0Name.Value;
break;
case "Decoded_format":
strFormat = objStream0Name.Value;
break;
case "Codec":
strVideoCodec = objStream0Name.Value;
break;
case "Type":
strVideoType = objStream0Name.Value;
break;
case "Resolution":
strResolution = objStream0Name.Value;
break;
}
}
break;
case "Stream 1": //audio
foreach (var objStream1Name in objCategory.Value)
{
string objStreamName = (string)objStream1Name.Name;
switch (objStreamName)
{
//Get Stream 1
case "Bitrate":
strBitRate = objStream1Name.Value;
break;
case "Channels":
strChannels = objStream1Name.Value;
break;
case "Codec":
strAudioCodec = objStream1Name.Value;
break;
case "Type":
strAudioType = objStream1Name.Value;
break;
case "Sample_rate":
strSampleRate = objStream1Name.Value;
break;
}
}
break;
}
}
break;
}
}
}

//Build custom JSON formatted response
my_strJSON = my_strJSON + @"{""strState"":""" + strState + @""",
""strFileName"":""" + strFileName + @""",
""strFullscreen"":""" + strFullscreen + @""",
""strVideoCodec"":""" + strVideoCodec + @""",
""strAudioCodec"":""" + strAudioCodec + @""",
""strAspectratio"":""" + strAspectratio + @""",
""strSampleRate"":""" + strSampleRate + @""",
""strTime"":""" + strTime + @""",
""strLength"":""" + strLength + @""",
""strFormat"":""" + strFormat + @""",
""strFrameRate"":""" + strFrameRate + @""",
""strVolume"":""" + strVolume + @""",
""strVideoType"":""" + strVideoType + @""",
""strResolution"":""" + strResolution + @""",
""strBitRate"":""" + strBitRate + @""",
""strAudioType"":""" + strAudioType + @""",
""strChannels"":""" + strChannels + @"""}";
my_strJSON = my_strJSON + @"]}";

return my_strJSON;
}


Return to “Web and scripting”

Who is online

Users browsing this forum: No registered users and 21 guests