Monday, November 18, 2013

page load asynchronous code after UI rendering (execute code after page render complete using script manager)

problem :
i have some events ( functions) should executed in page load but it consume some time to executed so i need to load the page first and silently load these funcitons to load page in little time.

solution :
using ajax script manager with EnablePageMethods="true"  to execute asynchronous functions
using javascipt PageMethods function.

copy and past the following code in new web site to test
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Async="true" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">


        function CallServerSideFunction2() {
            //calling our page method 'CallFromClient1'
            debugger;
            PageMethods.CallFromClient2(OnSucceeded2, OnFailed2);
            return false;
        }


        function OnSucceeded2(result) {
            var div1 = $get("Div1");
            var oNewNode = document.createElement("LI");
            div1.appendChild(oNewNode);
            oNewNode.innerText = result;
        }


        function OnFailed2(error) {
            // Alert user to the error.
            alert(error.get_message());
        }
    </script>
</head>
<body>
    <form id="form2" runat="server">
           <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
        <div runat="server" id="Div1"></div>

        <div>

        You page Html  goes here 
        </div>
    </form>
</body>
</html>

vb file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "getprice", "CallServerSideFunction2();", true);

   

    }


    [WebMethod]
    //This is our page method!
    //This function can carry out the server processing like fetching the values from DB
    public static string CallFromClient2()
    {
        Thread.Sleep(5000);
        return "$1120";
    }
}
--------------------------
the idea is using static web method to handle the event consuming time and register startup script using script manager then get value from web method and bind it to page control using javascript handlers for the script manager .
this manner let the page loads and then call web service function to get long time operation and bind result value silently to page control using normal javascript.

problem : the web method must be static to be accessed by script manager javascript function PageMethods
so if you need to use session inside the web method you can use :HttpContext.Current.Session[""].ToString

Tuesday, November 5, 2013

SMPP Client Program Using Jamaa SMPP Client

the following code working for SMPP request using jamaa smpp library.
download dss from the following link
https://jamaasmpp.codeplex.com/releases/view/65964

then create new windows application or console application and paste the following code and change you SMSC provider data
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using JamaaTech.Smpp.Net.Client;
using JamaaTech.Smpp.Net.Lib;
using JamaaTech.Smpp.Net.Lib.Protocol;

namespace SMPP_Test
{
    public partial class Form1 : Form
    {
        SmppClient mmclient = new SmppClient();
        SmppConnectionProperties mmproperties;
        TextMessage mymsg = new TextMessage();
        public Form1()
        {
            InitializeComponent();
            mmproperties = mmclient.Properties;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            mmproperties.SystemID = "YourSystemID";
            mmproperties.Password = "YourPassword";
            mmproperties.Port = 23651; //IP port to use
            mmproperties.Host = "xxx.net"; //SMSC host name or IP Address
            mmproperties.SystemType = "";
         

            //Resume a lost connection after 30 seconds
            mmclient.AutoReconnectDelay = 3000;
            //Send Enquire Link PDU every 30 seconds
            mmclient.KeepAliveInterval = 30000;
            //Start smpp client
          
           // mmclient.Connect
            mmclient.Properties.InterfaceVersion = InterfaceVersion.v34;
            mmclient.Properties.DefaultEncoding = DataCoding.SMSCDefault;
            mmclient.Properties.SourceAddress = "MadinaWater";
            mmclient.Properties.AddressNpi = NumberingPlanIndicator.Unknown;
            mmclient.Properties.AddressTon = TypeOfNumber.Unknown;
            mmclient.Properties.SystemType = "transceiver";
            mmclient.Properties.DefaultServiceType = ServiceType.DEFAULT;// "transceiver";      
            
            mmclient.MessageReceived += mmclient_MessageReceived;
            mmclient.MessageDelivered += mmclient_MessageDelivered;
            mmclient.MessageSent += mmclient_MessageSent;


            mymsg.DestinationAddress = "+966562824865"; //Receipient number
            mymsg.SourceAddress = "ShortCodeRelated Your Provider"; //Originating number
            mymsg.Text = "Hello, this is my simple client test message!";
            mymsg.RegisterDeliveryNotification = true; //I want delivery notification for this message
         
            mmclient.Start();
            if(mmclient.ConnectionState != SmppConnectionState.Connected )
                mmclient.ForceConnect(5000);
            mmclient.SendMessage(mymsg,1000);
           

            //Provide a handler for the SmppClient.MessageReceived event
          
            mmclient.Shutdown();
           
            ////Resume a lost connection after 30 seconds
            //client.AutoReconnectDelay = 3000;
            ////Send Enquire Link PDU every 15 seconds
            //client.KeepAliveInterval = 15000;

            ////Start smpp client
            //client.Start();
            //TextMessage msg = new TextMessage();
            //msg.DestinationAddress = "+966562824865";
            //msg.Text = "تجربة الإرسال من ال SMPP";
            //msg.SourceAddress = "MWater";
            //client.SendMessage(msg,3000);
           


        }

        void mmclient_MessageSent(object sender, MessageEventArgs e)
        {

            JamaaTech.Smpp.Net.Client.TextMessage textMsg = e.ShortMessage as JamaaTech.Smpp.Net.Client.TextMessage;
            MessageBox.Show(textMsg.Text);

            TextMessage msg = e.ShortMessage as TextMessage;
            string SegID = msg.SegmentID.ToString(); //Gives the message ID from the SMPP on Send 
            string Seg = msg.SequenceNumber.ToString(); //Give the status of the message - Enroute

            MessageBox.Show(msg.Text); //Display message

        }

        void mmclient_MessageDelivered(object sender, MessageEventArgs e)
        {
            JamaaTech.Smpp.Net.Client.TextMessage textMsg = e.ShortMessage as JamaaTech.Smpp.Net.Client.TextMessage;
            MessageBox.Show(textMsg.Text);
           
        }

        void mmclient_MessageReceived(object sender, MessageEventArgs e)
        {
            //The event argument e contains more information about the received message       

            JamaaTech.Smpp.Net.Client.TextMessage textMsg = e.ShortMessage as JamaaTech.Smpp.Net.Client.TextMessage; //This is the received text message                  

           //richTextBox1.Text = richTextBox1.Text + textMsg + " Step 1";
            
            string msgInfo = string.Format("Sender: {0}, Receiver: {1}, Message content: {2}", textMsg.SourceAddress, textMsg.DestinationAddress, textMsg.Text);

            //Display message             MessageBox.Show(msgInfo);       

            //     richTextBox1.Text = richTextBox1.Text + msgInfo;

        }
    }
}