void _websocket_MessageReceived(Windows.Networking.Sockets.MessageWebSocket sender, Windows.Networking.Sockets.MessageWebSocketMessageReceivedEventArgs args)
        {
            var ev = OnMessageReceived;

            if (ev != null)
            {
                try {
                    using (var reader = args.GetDataReader()) {
                        reader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
                        var text = reader.ReadString(reader.UnconsumedBufferLength);
                        //System.Diagnostics.Debug.WriteLine(":: recived " + text);
                        Task.Factory.StartNew(() => ev(text));
                    }
                } catch {}
            }
        }
Esempio n. 2
0
 private void WebSocket_MessageReceived(Windows.Networking.Sockets.MessageWebSocket sender, Windows.Networking.Sockets.MessageWebSocketMessageReceivedEventArgs args)
 {
     try
     {
         using (DataReader dataReader = args.GetDataReader())
         {
             dataReader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
             string message = dataReader.ReadString(dataReader.UnconsumedBufferLength);
             Debug.WriteLine("Message received from MessageWebSocket: " + message);
             this.messageWebSocket.Dispose();
         }
     }
     catch (Exception ex)
     {
         Windows.Web.WebErrorStatus webErrorStatus = Windows.Networking.Sockets.WebSocketError.GetStatus(ex.GetBaseException().HResult);
         // Add additional code here to handle exceptions.
     }
 }
Esempio n. 3
0
        //使用了 WebSocket.org 回显服务器 — 将服务回显到向其发送任一消息的发送方。
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            this.messageWebSocket = new Windows.Networking.Sockets.MessageWebSocket();

            // In this example, we send/receive a string, so we need to set the MessageType to Utf8.
            this.messageWebSocket.Control.MessageType = Windows.Networking.Sockets.SocketMessageType.Utf8;

            this.messageWebSocket.MessageReceived += WebSocket_MessageReceived;
            this.messageWebSocket.Closed          += WebSocket_Closed;

            try
            {
                Task connectTask = this.messageWebSocket.ConnectAsync(new Uri("wss://echo.websocket.org")).AsTask();
                connectTask.ContinueWith(_ => this.SendMessageUsingMessageWebSocketAsync("Hello, World!"));
            }
            catch (Exception ex)
            {
                Windows.Web.WebErrorStatus webErrorStatus = Windows.Networking.Sockets.WebSocketError.GetStatus(ex.GetBaseException().HResult);
                // Add additional code here to handle exceptions.
            }
        }
        //private Windows.Storage.Streams.DataWriter writer;

        public void Connect(string url)
        {
            Uri uri = null;

            string connectionId = Strings.RandomString(8);
            int    serverId     = Strings.RandomNumber(1, 1000);

            try
            {
                uri = new Uri(url);
            }
            catch (Exception)
            {
                throw new OrtcEmptyFieldException(String.Format("Invalid URL: {0}", url));
            }

            string prefix = uri != null && "https".Equals(uri.Scheme) ? "wss" : "ws";

            Uri connectionUrl = new Uri(String.Format("{0}://{1}:{2}/broadcast/{3}/{4}/websocket", prefix, uri.DnsSafeHost, uri.Port, serverId, connectionId));

            //
            // NOTE: For wss connections, must have a valid installed certificate
            // See: http://www.runcode.us/q/c-iphone-push-server
            //

            _websocket = new Windows.Networking.Sockets.MessageWebSocket();

            _websocket.Control.MessageType = Windows.Networking.Sockets.SocketMessageType.Utf8;
            _websocket.MessageReceived    += _websocket_MessageReceived;
            _websocket.Closed += _websocket_Closed;


            _websocket.ConnectAsync(connectionUrl).Completed = new Windows.Foundation.AsyncActionCompletedHandler(
                (Windows.Foundation.IAsyncAction source, Windows.Foundation.AsyncStatus status) =>
            {
                //System.Diagnostics.Debug.WriteLine(":: connecting status " + status);
                if (status == Windows.Foundation.AsyncStatus.Canceled)
                {
                    _reconnectTimer.ActionDone(false);
                }
                else if (status == Windows.Foundation.AsyncStatus.Completed)
                {
                    //writer = new Windows.Storage.Streams.DataWriter(_websocket.OutputStream);
                    //writer.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
                    messageWriter = new Windows.Storage.Streams.DataWriter(_websocket.OutputStream);
                    var ev        = OnOpened;
                    if (ev != null)
                    {
                        Task.Factory.StartNew(() => ev());
                    }
                    //_reconnectTimer.ActionDone(true);
                }
                else if (status == Windows.Foundation.AsyncStatus.Error)
                {
                    var ev = OnError;
                    if (ev != null)
                    {
                        Task.Factory.StartNew(() => ev(new OrtcNotConnectedException("Websocket has encountered an error.")));
                    }
                    _reconnectTimer.ActionDone(true);
                }
                else if (status == Windows.Foundation.AsyncStatus.Started)
                {
                }
                else
                {
                    //unknown state
                }
            });


            /*
             * _websocket = new WebSocket(connectionUrl.AbsoluteUri);
             *
             * _websocket.Opened += new EventHandler(websocket_Opened);
             * _websocket.Error += new EventHandler<SuperSocket.ClientEngine.ErrorEventArgs>(websocket_Error);
             * _websocket.Closed += new EventHandler(websocket_Closed);
             * _websocket.MessageReceived += new EventHandler<MessageReceivedEventArgs>(websocket_MessageReceived);
             *
             * _websocket.Open();
             */
        }