コード例 #1
0
        public void Close()
        {
            try
            {
                if (_client != null)
                {
                    _client.ReceivedMessage -= _client_ReceivedMessage;
                    _client.WebSocketClosed -= _client_WebSocketClosed;
                    _client.WebSocketFailed -= _client_WebSocketFailed;
                    _client.WebSocketOpened -= _client_WebSocketOpened;

                    if (_client.ReadyState == ReadyState.Open)
                    {
                        _client.Close();
                    }

                    _client.Dispose();
                    _client = null;

                    var ev = OnClosed;
                    if (ev != null)
                    {
                        ev();
                    }
                }
            }
            catch (Exception ex)
            {
                OnError(ex.Message);
            }
        }
コード例 #2
0
		partial void ConnectButtonClicked (UIButton sender)
		{
			var url = locationTextField.Text;

			if (webSocket == null || webSocket.ReadyState == ReadyState.Closed) {
				webSocket = new WebSocket (new NSUrl (url));
				webSocket.ReceivedMessage += (_, e) => {
					logTextField.Text += string.Format ("Received message: '{1}'{0}", Environment.NewLine, e.Message);
				};
				webSocket.ReceivedPong += (_, e) => {
					logTextField.Text += string.Format ("Received pong.{0}", Environment.NewLine);
				};
				webSocket.WebSocketClosed += (_, e) => {
					logTextField.Text += string.Format ("Disconnected: '{1}' ({2}).{0}", Environment.NewLine, e.Reason, e.Code);
					UpdateUI (webSocket.ReadyState);
				};
				webSocket.WebSocketFailed += (_, e) => {
					logTextField.Text += string.Format ("Failed to connect: {1}.{0}", Environment.NewLine, e.Error);
					UpdateUI (webSocket.ReadyState);
				};
				webSocket.WebSocketOpened += (_, e) => {
					logTextField.Text += string.Format ("Connected to '{1}'.{0}", Environment.NewLine, url);
					UpdateUI (webSocket.ReadyState);
				};
				logTextField.Text += string.Format ("Connecting to '{1}'...{0}", Environment.NewLine, url);
				webSocket.Open ();
			} else if (webSocket.ReadyState == ReadyState.Open) {
				logTextField.Text += string.Format ("Disconnecting...{0}", Environment.NewLine);
				webSocket.Close ();
			}
			UpdateUI (webSocket.ReadyState);
		}
コード例 #3
0
        public void Open(string url, string protocol = null, string authToken = null)
        {
            try
            {
                if (_client != null)
                    Close();

                NSUrlRequest req = new NSUrlRequest(new NSUrl(url));
                if (!string.IsNullOrEmpty(authToken))
                {
                    NSMutableUrlRequest mutableRequest = new NSMutableUrlRequest(new NSUrl(url));
                    mutableRequest["Authorization"] = authToken;
                    req = (NSUrlRequest)mutableRequest.Copy();
                }

                if (string.IsNullOrEmpty(protocol))
                    _client = new WebSocket(req);
                else
                    _client = new WebSocket(req, new NSObject[] { new NSString(protocol) });

                _client.ReceivedMessage += _client_ReceivedMessage;
                _client.WebSocketClosed += _client_WebSocketClosed;
                _client.WebSocketFailed += _client_WebSocketFailed;
                _client.WebSocketOpened += _client_WebSocketOpened;

                _client.Open();
            }
            catch (Exception ex)
            {
                OnError(ex.Message);
            }
        }