I have a C#/Winforms application using PHP webservices. A first PHP webservice uploadFile.php is used to upload a blob file (by a unix application for example) on a mysql database and another webservice, getFile.php (code below), is used to get the file if a file is available for the client.
For the moment the client calls every 5 seconds the webservice. Obviously Apache cannot deal with all the connections and gets slow and is often down or timeout, Today I have counted 2000 connections in 2 minutes, and 100 000 in 25 minutes. 99% of them return no file.
So instead of calling every 5 (or 10 or 20 or else) seconds the webservice, I would like the webservice uploadFile to notify the client when a result is available. I'm not familiar with SOAP/REST or any other kind of webservices, and for practical reasons (old versions of the application still running) I could not replace the current webservice with REST. Here is what currently does the webservice getOK.php :
if(getJobsForUser($login, $serveur)){
//data is BLOB content
$query = "SELECT idjob,fileName,data FROM JobSpool WHERE idcompte=$idcompte LIMIT 1" ;
$stmt = $pdo->query($query);
$res = $stmt->fetch(PDO::FETCH_BOTH);
$idjob = $res[0] ;
$nomfic = $res[1] ;
$data = $res[2] ;
//send datas to client
echo $nomfic."
" ;
echo $data ;
}
function checkJobsforUser($login, $serveur){
global $pdo, $idcompte;
$query = "SELECT count(*),c.idcompte FROM Compte as c,Job as j WHERE c.login='$login' AND c.serveur = '$serveur' AND j.idcompte = c.idcompte" ;
$stmt = $pdo->query($query);
$results = $stmt->fetch(PDO::FETCH_BOTH);
//print_r($results);
$count = $results[0];
$idcompte = $results[1];
//echo "count : $count, idcompte : $idcompte <br/>";
if($count > 0)
return true;
return false;
}
C# code :
public String getMyFile(){
String nomFicFinal = String.Empty;
try
{
HttpWebRequest hwr;
String request = "http://myWebServiceDomain.com/getFile.php?login=myLogin&otherId=myOtherId";
hwr = (HttpWebRequest)HttpWebRequest.Create(request);
using (HttpWebResponse hwrep = (HttpWebResponse)hwr.GetResponse())
{
//read datas
br = new BinaryReader(hwrep.GetResponseStream());
Byte[] tmp = new Byte[256];
int b = br.Read();
while (b != -1)
{
if (b == 10) break;
tmp[indNomFic++] = (Byte)b;
b = br.Read();
}
//get filename
nomFicFinal = System.Text.Encoding.ASCII.GetString(tmp, 0, indNomFic);
//if parsed data == "0" nothing returned
if (nomFicFinal.Length == 1 && (nomFicFinal.CompareTo("0") == 0 || nomFicFinal.CompareTo("1") == 0))
{
br.Close();
nomFicFinal = "";
}
else //we parse the content of the request and we create the file with the name nomFicFinal in ApplicationData
{
nomFicFinal = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\MyApp\" + nomFicFinal;
bw = new BinaryWriter(File.Open(nomFicFinal, FileMode.Create));
tmp = new Byte[1024];
while ((tmp = br.ReadBytes(1024)) != null)
{
if (tmp.Length == 0) break;
bw.Write(tmp);
bw.Flush();
}
br.Close();
bw.Close();
}
}
}catch(Exception ex){
//do something
}
return nomFicFinal;
}
I have been searching with "callback PHP C#", etc, but I still do not have a clear idea of what to do for the moment.