/// <summary>
        /// Handle Chat Message button click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSendChat_Click(object sender, RoutedEventArgs e)
        {
            //// create the chat message
            var chatMessageBody = new ChatMessage() { From = Username, MessageText = txtUserChatMessage.Text };
            var messageBody = new SocketServiceMessage() { Action = SocketServiceAction.ChatMessage, Message = chatMessageBody };
            var msg = JsonMessageSerializer.Serialize(messageBody);

            ////send the message to the service topic
            _svcClient.Receive(msg);
        }
        public ChatRoom(string username)
        {
            InitializeComponent();
            Username = username;

            // create the callback handler and the service client
            var callbackContext = new InstanceContext(new CallbackHandler(this));
            var factory = new DuplexChannelFactory<ISocketChatService>(callbackContext, "SocketChatService");
            _svcClient = factory.CreateChannel();

            // Create the custom logon message and send in order to logon
            var loginMessageBody = new SocketServiceMessage() { Action = SocketServiceAction.Login, Username = username };
            var loginMessage = JsonMessageSerializer.Serialize(loginMessageBody);
            _svcClient.Receive(loginMessage);
        }
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            // retrieve the username from the payload
            _username = (string)e.Parameter;

            try
            {
                // Make a local copy to avoid races with Closed events.
                MessageWebSocket webSocket = messageWebSocket;

                // Have we connected yet?
                if (webSocket == null)
                {
                    Uri server = new Uri("ws://vdev-pc/svc/SocketChatService.svc");

                    webSocket = new MessageWebSocket();
                    // MessageWebSocket supports both utf8 and binary messages.
                    // When utf8 is specified as the messageType, then the developer
                    // promises to only send utf8-encoded data.
                    webSocket.Control.MessageType = SocketMessageType.Utf8;
                    // Set up callbacks
                    webSocket.MessageReceived += MessageReceived;

                    await webSocket.ConnectAsync(server);
                    messageWebSocket = webSocket; // Only store it after successfully connecting.
                    messageWriter = new DataWriter(webSocket.OutputStream);
                }

                // create the main message and serialize to string
                SocketServiceMessage msgBody = new SocketServiceMessage() { Action = SocketServiceAction.Login, Username = _username };
                string msgBodyString = JsonConvert.SerializeObject(msgBody);

                // Buffer any data we want to send.
                messageWriter.WriteString(msgBodyString);

                // Send the data as one complete message.
                await messageWriter.StoreAsync();
            }
            catch (Exception ex) // For debugging
            {
                WebErrorStatus status = WebSocketError.GetStatus(ex.GetBaseException().HResult);
            }
        }
        /// <summary>
        /// Handle the user clicking the send button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void btnSendChatMessage_Click(object sender, RoutedEventArgs e)
        {
            // create the chat message and send it across the wire
            var chatMessage = new ChatMessage() { From = _username, MessageText = txtUserMessage.Text };

            // create the wrapper message and serialize it to string
            var wrapperMsg = new SocketServiceMessage() { Action = SocketServiceAction.ChatMessage, Message = chatMessage };
            string msg = JsonConvert.SerializeObject(wrapperMsg);

            // buffer the message
            messageWriter.WriteString(msg);

            // send the message
            await messageWriter.StoreAsync();

            //_svcClient.SendMessageAsync(msg);

            // clear out the message text box
            txtUserMessage.Text = "";
        }