An attempt to make a simple silverlight client for the 01 ConsoleApp sample

Mar 21, 2011 at 6:55 PM
Edited Mar 21, 2011 at 6:57 PM

Hi anybody,

I was trying to make a very simple silverlight app over the 01 sample app provided with the source of Nugget. Silverlight app should have the same functionality as sample 01. I even used the same javascript calls, but it always fails on connecting to server. I tried putting breakpoints into javascript in Visual studio, but it seems like none of them ever trigger, even when it is obvious the code is being run. So I have absolutely no idea why it is not working. The code for connecting to websocket server is 100% identical...so why it won't connect? I suspect that it will have something to do with some unthinkable security restriction, but thats just a bad guess. Please at least point me to something.

The whole VS2010 project can be downloaded HERE.

I will paste the most important bits of code here also for anyone who doesn't want to go through downloading and opening the whole solution.

MainPage.xaml.cs:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows;
using System.Windows.Browser;

namespace SilverlightApplication1
{
   
    [ScriptableType]
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new System.Windows.RoutedEventHandler(Page_Loaded);
        }

        void Page_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            // Register this object in the page as Scriptable
            // so it can be access from within JavaScript
            HtmlPage.RegisterScriptableObject("myObject", this);
        }

        [ScriptableMember]
        public void UpdateText(string text)
        {
            myTextbox.Text += text;
        }

        private void button1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            HtmlPage.Window.Invoke("websocketfunction");
        }
   }
} 

 

and javascript in aspx file used for websockets:

 

<script type="text/javascript">
             function websocketfunction() {
                var pluginObject = $find("<%=Xaml1.ClientID%>");
                var plugin = pluginObject.get_element();
            plugin.Content.myObject.UpdateText("connecting to server ..\n");

            // create a new websocket and connect
            var ws = new WebSocket('ws://localhost:8181/consoleappsample');

            // when data is comming from the server, this metod is called
            ws.onmessage = function (evt) {
                plugin.Content.myObject.UpdateText(evt.data + '\n');
            };

            // when the connection is established, this method is called
            ws.onopen = function () {
                plugin.Content.myObject.UpdateText('.. connection open' + '\n');
                
            };

            // when the connection is closed, this method is called
            ws.onclose = function () {
                plugin.Content.myObject.UpdateText('.. connection closed' + '\n');
            }
        }
</script>

 

Mar 23, 2011 at 10:37 AM

Ok, this is me again, I've found the solution.

Instead of running the whole page from aspx, I am now using html and DOM bridge to acces silverlight methods and it works.

I'll post the updated javascript soon.