On 2/8/2010 5:36 AM, Animesh Sonkar wrote:
> Looked around all the forums for a well working example for Comet Client.

Here is some code I wrote to test a CometProcessor:

Using HttpURLConnection:

URL url = new URL( "http://...."; );
HttpURLConnection connection = (HttpURLConnection)
  url.openConnection();
connection.setChunkedStreamingMode( 1 );
connection.setDoOutput( true );
connection.setDoInput( true );
connection.connect();
OutputStream os = connection.getOutputStream();

while ( true ) {
  os.write( 1 );
  os.flush();
  Thread.sleep( 1000 );
}

os.write( 0 );
os.flush();

os.close();

connection.disconnect();

Using sockets:

try {
  Socket socket;
  try {
    socket = new Socket( "1.2.3.4", 8911 );
  } catch ( Exception e ) {
    log.error( e.getMessage(), e );
    return;
  }

  OutputStream out = null;
  PrintWriter pw = null;
  BufferedReader in = null;
  try {
    out = socket.getOutputStream();
    pw = new PrintWriter( out, true );
    in = new BufferedReader(
        new InputStreamReader(
          socket.getInputStream() ) );

    pw.print( "POST /cometProcessor HTTP/1.1" );
    pw.print( CRLF );
    pw.print( "Host: example.com" );
    pw.print( CRLF );
    pw.print( "Accept: image/png,image/*;q=0.8,*/*;q=0.5" ); // fixme
    pw.print( CRLF );
    pw.print( "Accept-Language: en-us,en;q=0.5" );
    pw.print( CRLF );
    pw.print( "Accept-Encoding: gzip,deflate" ); // fixme
    pw.print( CRLF );
    pw.print( "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" );
    pw.print( CRLF );
    pw.print( "Connection: keep-alive" );
    pw.print( CRLF );

    pw.print( "Content-Type: text/plain" );
    pw.print( CRLF );

    pw.print( "Transfer-Encoding: chunked" );
    pw.print( CRLF );

    pw.print( CRLF );

    pw.flush();
    out.flush();

    // send a byte every second.
    log.debug( "Send a byte every second. . ." );
    while ( true ) {
      pw.print( "01" ); // chunk size
      pw.print( CRLF );
      pw.print( "A" );
      pw.print( CRLF );
      pw.flush();
      Thread.sleep( 1000 );
    }

    pw.print( "0" ); // end of chunks
    pw.print( CRLF );

    pw.print( CRLF ); // end of request.
    pw.flush();

    Thread.sleep( 1000 );
  } finally {
    if ( in != null ) {
      try {
        String s;
        log.debug( "RESPONSE" );
        while ( ( s = in.readLine() ) != null ) {
          log.debug( s );
        }
        log.debug( "END" );
        in.close();
      } catch ( Exception e ) {
        log.warn( e.getMessage(), e );
      }
    }

    if ( out != null ) {
      try {
        out.close();
      } catch ( IOException e ) {
      }
    }

    if ( socket != null ) {
      try {
        socket.close();
      } catch ( IOException e ) {
      }
    }
  }

}


-- 
Stephen Byrne
step...@lincware.com
(585) 286-5817

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to