private void OnConnected() { CommandReader = ProtocolProcessor.CreateHandshakeReader(this); if (Items.Count > 0) { Items.Clear(); } ProtocolProcessor.SendHandshake(this); }
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; }