tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From John Dous <johnd...@hotmail.com>
Subject How to produce a Comet EventType.READ from the clint side?
Date Thu, 02 Apr 2009 15:21:53 GMT



Hello, 

Recently I started to develop a web application
that is based on the Comet Architecture. After a lot of research, I realize
that there is a lack of documentation and tutorials about how to build simple Comet
applications on Tomcat. The tomcat Advanced IO documentation previews the way
that the server handles the incoming different type events, but doesn’t explain
how to produce these events from the client side (ex. IE, Firefox). The snippet
below produces such an event. 

 

function newConnection(){

xhr = new XMLHttpRequest();

xhr.open('GET','/comet' ,true);

xhr.send(null);

}

When I execute this function, a new event (EventType.Begin)
arrives on the CometServlet. 

I am wondering if there is way to produce an (EventType.READ)
event from the client side.

The only way to produce (EventType.READ) events
on the CometServlet was by the above java code, but I don’t know how to use it efficiently
to accomplish my goal. 
Any suggestion?

    private static final String ENCODING = "ISO-8859-1";

    private static final String DELIMITER = "\r\n";

    private URL url;

    private
InputStream inputStream;

    private
OutputStream outputStream;

    private Socket socket;

    public static void main(String[]
args) throws Exception {

       Post test = new Post();

       
test.test();

    }

 

    private void test() throws Exception
{

        url = new URL("http://localhost:8080/comet");

       
initConnection();

       
sendHeaders();

        send("test1");

        Thread.sleep(2*1000);

        send("test2");

       
sendLastChunk();

        // block on read to keep app alive - server never
sends response

        byte[] data = new byte[8192];

        int result = inputStream.read(data);

        while
(result>0) {

           
String s = new String(data,0,result);

           
System.out.println("Received
data:\n"+s);

           
result = inputStream.read(data);

        }

        {

           
System.out.println("Received
EOF");

        }

    }

 

    private void
initConnection() throws IOException {

        int port = url.getPort();

        port =
(port < 0) ? url.getDefaultPort() : port;

        try {

            socket = new Socket(url.getHost(),
port);

            socket.setSoTimeout(Integer.MAX_VALUE);

            socket.setKeepAlive(true);

            inputStream = socket.getInputStream();

            outputStream = socket.getOutputStream();

        } catch
(NoRouteToHostException nrthe) {

           
System.out.println("host:
" + url.getHost());

           
nrthe.printStackTrace();

        }

    }

 

    private void sendHeaders() throws IOException
{

        String
path = url.getPath();

       
StringBuffer outputBuffer = new StringBuffer();

       
outputBuffer.append("POST
" + path + " HTTP/1.1" + DELIMITER);

       
outputBuffer.append("Host:
" + url.getHost() + DELIMITER);

        outputBuffer.append("User-Agent: CometTest" + DELIMITER);

       
outputBuffer.append("Connection:
keep-alive" + DELIMITER);

       
outputBuffer.append("Content-Type:
text/plain" + DELIMITER);

       
outputBuffer.append("Transfer-Encoding:
chunked" + DELIMITER);

       
outputBuffer.append(DELIMITER);

        byte[] outputBytes =
outputBuffer.toString().getBytes(ENCODING);

        outputStream.write(outputBytes);

        outputStream.flush();

    }

 

    private void send(String
chunkData) throws IOException {

        byte[] chunkBytes =
chunkData.getBytes(ENCODING);

        String
hexChunkLength = Integer.toHexString(chunkBytes.length);

       
StringBuffer outputBuffer = new StringBuffer();

       
outputBuffer.append(hexChunkLength);

        outputBuffer.append(DELIMITER);

       
outputBuffer.append(chunkData);

       
outputBuffer.append(DELIMITER);

        byte[] outputBytes =
outputBuffer.toString().getBytes(ENCODING);

        outputStream.write(outputBytes);

        outputStream.flush();

    }

 

    private void sendLastChunk()
throws IOException {

        byte[] outputBytes =
new String("0" +

DELIMITER+DELIMITER).getBytes(ENCODING);

        outputStream.write(outputBytes);

        outputStream.flush();

    }

}

 


_________________________________________________________________
Show them the way! Add maps and directions to your party invites. 
http://www.microsoft.com/windows/windowslive/products/events.aspx
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message