Wednesday, July 06, 2011

WebSockets, WCF & Silverlight 5

Running The Application

 If you run the application you will be present with a log in screen (below), where you just enter a unique name - the name is not validated here - but that is something that can be easy implemented - all we are after is a unique name that can be later used to indicate who pushed a an update from the client on the GUI.

LoginScreen.JPG
Once logged into the application, the main screen will be display. It contains a grid, with cell foreground colors converted, based on their cell values. A number of gauges, are displayed below the grid, that reflect the values within the grid itself. Below the gauges, is a  textbox and button to update the 'Fan' gauge (push the value to the server and then onto each connected session). At the bottom is a textblock that will display all the transactions from other stores\users or auto generated by the server and pushed to all the clients.

...
...

Code Explanation
 Client (Silverlight) Code

namespace Client
{
    [ScriptableType]
    public partial class ClientPage : Page
    {
        private ObservableCollection<ThermoTemps> thermoCollection;
      
        public ClientPage()
        {
            InitializeComponent();
            ThermoCollection = new ObservableCollection<ThermoTemps>();
            this.gridThermo.ItemsSource = ThermoCollection;
            HtmlPage.RegisterScriptableObject("myObject", this);
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            HtmlPage.Window.Invoke("sendMessage", this.txtFan.Text);
        }

        [ScriptableMember]
        public void UpdateText(string result)
        {
            try
            {
                string jsonString = result.Substring(result.IndexOf('{'));
                ThermoTemps myDeserializedObj = new ThermoTemps();

                DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(ThermoTemps));
                MemoryStream memoryStream = new MemoryStream(Encoding.Unicode.GetBytes(jsonString));
                myDeserializedObj = (ThermoTemps)dataContractJsonSerializer.ReadObject(memoryStream);
                ThermoCollection.Add(myDeserializedObj);

Read more: Codeproject
QR: WebSocketsSilverlight.aspx

Posted via email from Jasper-Net