ARTICLE

Silverlight Interaction with php

Posted by Yvan Articles | Silverlight using VB.NET April 29, 2010
Silverlight run on client side (Win/Mac/MoonLight) Php on LAMP server. Communication use HTTP protocol.
Download Files:
 
Reader Level:

Introduction

Silverlight run on client side (Win/Mac/MoonLight)
Just copy and paste on LAMP server.
Communication use HTTP protocol.

Interface view

General description


Make two php script :

write_msg.php
 
?php if (isset($_POST["user"]))
{
list($usec, $sec) = explode(" ", microtime());
$ts = $sec.substr($usec,2,4);
// On crée le fichier message dans le dossier msg
    $fichier = fopen('msg/'.$ts, 'w+');
fwrite($fichier, stripslashes($_POST['data']));
fclose($fichier);
echo "0";
$dossier = opendir('msg');
while ($fichier = readdir($dossier))
{
if (!preg_match("#^[.]{1,2}[a-z0-9 -.]*#i",$fichier))
{
// On en profite pour purger les msg plus vieux que 15 secondes
 if ($fichier < ($ts - 150000))
{
unlink('msg/'.$fichier);
}
}
}
closedir($dossier);
}
?>  read_msg.php 
<?php
session_start();
// ouvrir le dosssier msg
$dossier = opendir('msg');
while ($fichier = readdir($dossier))
{
if (!preg_match("#^[.]{1,2}[a-z0-9 -.]*#i",$fichier))     
{ 
$files_array[] = $fichier;       
}
}
closedir($dossier);
sort($files_array);
foreach ($files_array as $key => $val)
{
if ($_SESSION['last_msg_ts'] < $val)
{
// Lit le message non lu
        readfile('msg/'.$val,'r');
// Indique le dernier message lu
        $_SESSION['last_msg_ts'] = $val;
exit;        
} 
}
?>

Post Message with WebClient

Private Sub SendMsg(ByVal Data As String)

    ReadMsgTmr.Stop()

    Try
        Dim Client As New WebClient()
        Client.Headers("Content-Type") = "application/x-www-form-urlencoded"
        AddHandler Client.UploadStringCompleted, AddressOf SendMsgCompleted
        Client.UploadStringAsync(New Uri(String.Format("{0}/write_msg.php", BASEPATH)), "Post",
"user=" & UsrTbx.Text & "&data=" & Data)

    Catch ex As Exception

    End Try

End Sub

Private Sub SendMsgCompleted(ByVal sender As Object, _
                             ByVal e As UploadStringCompletedEventArgs)

    Try
        Dim Msg As String = e.Result
        Select Case Msg
            Case Nothing
                ReadMsgTmr.Interval = System.TimeSpan.FromMilliseconds(1)
                ReadMsgTmr.Start()
                Exit Sub
            Case "0"
                ReadMsgTmr.Interval = System.TimeSpan.FromMilliseconds(1)
                ReadMsgTmr.Start()
        End Select
    Catch ex As Exception

    End Try

End Sub

Read Message with WebClient

Private Sub ReadMsgTmr_Tick(ByVal sender As Object, _
                             ByVal e As System.EventArgs) Handles ReadMsgTmr.Tick

    ReadMsgTmr.Stop()

    Try

        Dim Client = New WebClient
        AddHandler Client.DownloadStringCompleted, AddressOf ReadMsgComplete
        Client.DownloadStringAsync(New Uri(Config.BASEPATH & "/read_msg.php", UriKind.Absolute), 0)

        Client = Nothing

    Catch ex As Exception
        'HtmlPage.Window.Alert(ex.Message)
    End Try

End Sub

Private Sub ReadMsgComplete(ByVal sender As Object, _
                            ByVal e As DownloadStringCompletedEventArgs)
    Try
        ' Si il y a un message
        Dim Msg As String = e.Result
        If Msg <> "" Then
            DisplayTextinChatBox(Msg)
            ' raffale
            ReadMsgTmr.Interval = System.TimeSpan.FromMilliseconds(150)
        Else
            ' idle
            ReadMsgTmr.Interval = System.TimeSpan.FromMilliseconds(1500)
        End If

    Catch ex As Exception

    End Try
    ReadMsgTmr.Start()

End Sub

 

Deploy


Just Copy and Paste on your server. Adjust CHMOD to 777

Online trying

Here

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor