コード例 #1
0
        public void Connect(Uri uri)
        {
            _connection = new WebSocketClientSSLConnection(_cacert, _wsMessageHandler);
            _connection.ConnectionClose += _wsMessageHandler.OnClose;
            _connection.ConnectionClose += delegate { OnDisconnect(EventArgs.Empty); };
            _connection.ConnectionOpen  += _wsMessageHandler.OnOpen;
            _connection.ConnectionOpen  += delegate { OnConnect(EventArgs.Empty); };
            // we no longer do it this way, but process it via a called function on full text being read
            //_connection.ConnectionRead += _wsMessageHandler.onMessage;

            // this whole starting of another thread to run the connection is because sometimes the connection hangs. We have set the read timeout
            // on the connection to 5 mins, but lets watch it here as well to make sure that the whole connection system doesn't hang.
            bool   connected   = false;
            Thread startThread = new Thread(() =>
            {
                try
                {
                    if (!_connection.Start(uri.Host, uri.Port.ToString(), uri.PathAndQuery, true, "", Protocol))
                    {
                        throw new IOException("Unknown error connecting to " + uri.ToString());
                    }
                    connected = true;
                }
                catch (Exception e)
                {
                    Logger.Error("Failed to connect to server [" + uri + "] : " + e.Message);
                }
            })
            {
                Name = "ConnectionStarter"
            };

            startThread.Start();
            if (!startThread.Join(360000))// 6 minute timeout
            {
                startThread.Abort();
                // connect didn't come back within the timeout period
                throw new Exception("Failed to start connection within timeout");
            }
            if (!connected)
            {
                throw new IOException("Failed to connect to server [" + uri + "]");
            }
            Logger.Debug("Connection Complete");
            //OnConnect(new EventArgs());
        }
コード例 #2
0
 public void Connect()
 {
     _messageHandler                 = new CoupledWebSocketMessageHandler(this);
     _connection                     = new WebSocketClientSSLConnection(_cert, _messageHandler);
     _connection.ConnectionClose    += _messageHandler.OnClose;
     _connection.ConnectionClose    += delegate { OnDisconnected(); };
     _connection.ConnectionOpen     += _messageHandler.OnOpen;
     _connection.ConnectionOpen     += delegate { OnConnected(); };
     _connection.ConnectionReadFull += ProcessStream;
     try
     {
         if (!_connection.Start(_uri.Host, _uri.Port.ToString(), _uri.PathAndQuery, true, "", "message"))
         {
             throw new IOException("Unknown error connecting to " + _uri);
         }
         _connection.SendText(_uniqueId);
     }
     catch (Exception e)
     {
         Logger.Error("Failed to connect to server [" + _uri + "] : " + e.Message);
         throw new IOException("Failed to connect to server [" + _uri + "] : " + e.Message, e);
     }
 }