Silverlight Interaction with php in VB.NET

Introduction

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

Interface view

ChatSL2phpVB_screen.png

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
ChatSL2phpVB_fichiers.png

Online trying

Here


Similar Articles