Esempio n. 1
0
        private void Initialize(string uri, string subProtocol, List <KeyValuePair <string, string> > cookies, List <KeyValuePair <string, string> > customHeaderItems, string userAgent, string origin, WebSocketVersion version, EndPoint httpConnectProxy, int receiveBufferSize)
        {
            if (version == WebSocketVersion.None)
            {
                NotSpecifiedVersion = true;
                version             = WebSocketVersion.Rfc6455;
            }

            Version           = version;
            ProtocolProcessor = GetProtocolProcessor(version);

            Cookies = cookies;

            Origin = origin;

            if (!string.IsNullOrEmpty(userAgent))
            {
                if (customHeaderItems == null)
                {
                    customHeaderItems = new List <KeyValuePair <string, string> >();
                }

                customHeaderItems.Add(new KeyValuePair <string, string>(UserAgentKey, userAgent));
            }

            if (customHeaderItems != null && customHeaderItems.Count > 0)
            {
                CustomHeaderItems = customHeaderItems;
            }

            var handshakeCmd = new Command.Handshake();

            m_CommandDict.Add(handshakeCmd.Name, handshakeCmd);
            var textCmd = new Command.Text();

            m_CommandDict.Add(textCmd.Name, textCmd);
            var dataCmd = new Command.Binary();

            m_CommandDict.Add(dataCmd.Name, dataCmd);
            var closeCmd = new Command.Close();

            m_CommandDict.Add(closeCmd.Name, closeCmd);
            var pingCmd = new Command.Ping();

            m_CommandDict.Add(pingCmd.Name, pingCmd);
            var pongCmd = new Command.Pong();

            m_CommandDict.Add(pongCmd.Name, pongCmd);
            var badRequestCmd = new Command.BadRequest();

            m_CommandDict.Add(badRequestCmd.Name, badRequestCmd);

            m_StateCode = WebSocketStateConst.None;

            SubProtocol = subProtocol;

            Items = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase);

            m_HttpConnectProxy = httpConnectProxy;

            TcpClientSession client;

            if (uri.StartsWith(m_UriPrefix, StringComparison.OrdinalIgnoreCase))
            {
                client = CreateClient(uri, receiveBufferSize);
            }
            else if (uri.StartsWith(m_SecureUriPrefix, StringComparison.OrdinalIgnoreCase))
            {
#if !NETFX_CORE
                client = CreateSecureClient(uri, receiveBufferSize);
#else
                throw new NotSupportedException("WebSocket4Net still has not supported secure websocket for UWP yet.");
#endif
            }
            else
            {
                throw new ArgumentException("Invalid uri", "uri");
            }

            client.Connected    += new EventHandler(client_Connected);
            client.Closed       += new EventHandler(client_Closed);
            client.Error        += new EventHandler <ErrorEventArgs>(client_Error);
            client.DataReceived += new EventHandler <DataEventArgs>(client_DataReceived);

            Client = client;

            //Ping auto sending is enabled by default
            EnableAutoSendPing = true;
        }
Esempio n. 2
0
        private void Initialize(string uri, string subProtocol, List<KeyValuePair<string, string>> cookies, List<KeyValuePair<string, string>> customHeaderItems, string userAgent, string origin, WebSocketVersion version)
        {
            if (version == WebSocketVersion.None)
            {
                NotSpecifiedVersion = true;
                version = WebSocketVersion.Rfc6455;
            }

            Version = version;
            ProtocolProcessor = GetProtocolProcessor(version);

            Cookies = cookies;

            Origin = origin;

            if (!string.IsNullOrEmpty(userAgent))
            {
                if (customHeaderItems == null)
                    customHeaderItems = new List<KeyValuePair<string, string>>();

                customHeaderItems.Add(new KeyValuePair<string, string>(UserAgentKey, userAgent));
            }

            if (customHeaderItems != null && customHeaderItems.Count > 0)
                CustomHeaderItems = customHeaderItems;

            var handshakeCmd = new Command.Handshake();
            m_CommandDict.Add(handshakeCmd.Name, handshakeCmd);
            var textCmd = new Command.Text();
            m_CommandDict.Add(textCmd.Name, textCmd);
            var dataCmd = new Command.Binary();
            m_CommandDict.Add(dataCmd.Name, dataCmd);
            var closeCmd = new Command.Close();
            m_CommandDict.Add(closeCmd.Name, closeCmd);
            var pingCmd = new Command.Ping();
            m_CommandDict.Add(pingCmd.Name, pingCmd);
            var pongCmd = new Command.Pong();
            m_CommandDict.Add(pongCmd.Name, pongCmd);
            var badRequestCmd = new Command.BadRequest();
            m_CommandDict.Add(badRequestCmd.Name, badRequestCmd);

            m_StateCode = WebSocketStateConst.None;

            SubProtocol = subProtocol;

            Items = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);

            TcpClientSession client;

            if (uri.StartsWith(m_UriPrefix, StringComparison.OrdinalIgnoreCase))
            {
                client = CreateClient(uri);
            }
            else if (uri.StartsWith(m_SecureUriPrefix, StringComparison.OrdinalIgnoreCase))
            {
                client = CreateSecureClient(uri);
            }
            else
            {
                throw new ArgumentException("Invalid uri", "uri");
            }

            client.Connected += new EventHandler(client_Connected);
            client.Closed += new EventHandler(client_Closed);
            client.Error += new EventHandler<ErrorEventArgs>(client_Error);
            client.DataReceived += new EventHandler<DataEventArgs>(client_DataReceived);

            Client = client;

            //Ping auto sending is enabled by default
            EnableAutoSendPing = true;
        }
Esempio n. 3
0
        private void Initialize(string uri, string subProtocol, List <KeyValuePair <string, string> > cookies, List <KeyValuePair <string, string> > customHeaderItems, string userAgent, string origin, WebSocketVersion version)
        {
            if (version == WebSocketVersion.None)
            {
                NotSpecifiedVersion = true;
                version             = WebSocketVersion.Rfc6455;
            }

            Version           = version;
            ProtocolProcessor = GetProtocolProcessor(version);
            CommandReader     = ProtocolProcessor.CreateHandshakeReader(this);

            Cookies = cookies;

            Origin = origin;

            if (!string.IsNullOrEmpty(userAgent))
            {
                if (customHeaderItems == null)
                {
                    customHeaderItems = new List <KeyValuePair <string, string> >();
                }

                customHeaderItems.Add(new KeyValuePair <string, string>(UserAgentKey, userAgent));
            }

            if (customHeaderItems != null && customHeaderItems.Count > 0)
            {
                CustomHeaderItems = customHeaderItems;
            }

            var handshakeCmd = new Command.Handshake();

            m_CommandDict.Add(handshakeCmd.Name, handshakeCmd);
            var textCmd = new Command.Text();

            m_CommandDict.Add(textCmd.Name, textCmd);
            var dataCmd = new Command.Binary();

            m_CommandDict.Add(dataCmd.Name, dataCmd);
            var closeCmd = new Command.Close();

            m_CommandDict.Add(closeCmd.Name, closeCmd);
            var pingCmd = new Command.Ping();

            m_CommandDict.Add(pingCmd.Name, pingCmd);
            var pongCmd = new Command.Pong();

            m_CommandDict.Add(pongCmd.Name, pongCmd);
            var badRequestCmd = new Command.BadRequest();

            m_CommandDict.Add(badRequestCmd.Name, badRequestCmd);

            State = WebSocketState.None;

            SubProtocol = subProtocol;

            Items = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase);

            TcpClientSession client;

            if (uri.StartsWith(m_UriPrefix, StringComparison.OrdinalIgnoreCase))
            {
                client = CreateClient(uri);
            }
            else if (uri.StartsWith(m_SecureUriPrefix, StringComparison.OrdinalIgnoreCase))
            {
#if SILVERLIGHT
                throw new ArgumentException("WebSocket4Net (Silverlight/WindowsPhone) cannot support wss yet.", "uri");
#else
                client = CreateSecureClient(uri);
#endif
            }
            else
            {
                throw new ArgumentException("Invalid uri", "uri");
            }

            client.Connected    += new EventHandler(client_Connected);
            client.Closed       += new EventHandler(client_Closed);
            client.Error        += new EventHandler <ErrorEventArgs>(client_Error);
            client.DataReceived += new EventHandler <DataEventArgs>(client_DataReceived);

            Client = client;
        }