Chat example with sample java client code

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Chat example with sample java client code

spepper
Hi,

I'm doing some research around atmosphere, specifically around support for java clients.  I have downloaded the chat example, but I'm struggling to get it to work using the async http client.  Would you happen to have some tests or example java client code that has been written to test the rest-enabled chat demo using the async http client?

I'm running atmosphere 5.8 on weblogic.

Thanks in advance.

-Steve Pepper
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Chat example with sample java client code

Jeanfrancois Arcand-4
Salut,

sorry for the delay. Take a look at the PubSub test sample here:

https://github.com/Atmosphere/atmosphere/blob/master/modules/jersey/src/test/java/org/atmosphere/jersey/tests/BasePubSubTest.java

All the tests use AHC...if you can share your client's code I can take a
look.

A+

-Jeanfrancois


On 11-02-14 3:06 PM, spepper wrote:

>
> Hi,
>
> I'm doing some research around atmosphere, specifically around support for
> java clients.  I have downloaded the chat example, but I'm struggling to get
> it to work using the async http client.  Would you happen to have some tests
> or example java client code that has been written to test the rest-enabled
> chat demo using the async http client?
>
> I'm running atmosphere 5.8 on weblogic.
>
> Thanks in advance.
>
> -Steve Pepper
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Chat example with sample java client code

spepper
Hi, Thanks for the response...

Below is my very simple test class.  My problem is that onBodyPartReceived() isn't getting called as someone types in a message on the chat window.  Here's how to exercise:

1. Change the url in the code.
2. Run the client.  It will initialize and then sleep.
3. Login to the usual chat web interface and send a message.  I expect to see the "Received body part" message in the java client window.

I have upgraded to atmosphere 0.6.5 which seemed to solve my previous problem.

Thanks again for your help...


//code
import java.util.concurrent.Future;

import com.ning.http.client.AsyncHandler;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.AsyncHttpClientConfig;
import com.ning.http.client.HttpResponseBodyPart;
import com.ning.http.client.HttpResponseHeaders;
import com.ning.http.client.HttpResponseStatus;
import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder;


public class TestChat
{
    private static final String url = "<a href="http://host:port/path/to/chat">http://host:port/path/to/chat";

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        AsyncHttpClientConfig config = new AsyncHttpClientConfig.Builder()
            .setRequestTimeoutInMs(10000)
            .build();
        AsyncHttpClient httpClient = new AsyncHttpClient(config);
        BoundRequestBuilder builder = httpClient.prepareGet(url);
        try
        {
            Future<String> f = builder.execute(new AsyncHandler<String>() {

                public void onThrowable(Throwable throwable)
                {
                }

                public STATE onBodyPartReceived(HttpResponseBodyPart bp)
                throws Exception
                {
                    System.out.println("Received body part:" + new String(bp.getBodyPartBytes()));
                    return STATE.CONTINUE;
                }

                public STATE onStatusReceived(HttpResponseStatus hs) throws Exception {
                    return STATE.CONTINUE;
                }

                public STATE onHeadersReceived(HttpResponseHeaders rh) throws Exception {
                    return STATE.CONTINUE;
                }

                public String onCompleted() throws Exception {
                    System.out.println("onCompleted...");
                    return "";
                }
            });
            Thread.sleep(30000);
            f.get();
        }
        catch(Throwable e)
        {
            e.printStackTrace();
        }
        finally
        {
            httpClient.close();
            System.exit(0);
        }
       
    }

}


jfarcand-3 wrote
Salut,

sorry for the delay. Take a look at the PubSub test sample here:

https://github.com/Atmosphere/atmosphere/blob/master/modules/jersey/src/test/java/org/atmosphere/jersey/tests/BasePubSubTest.java

All the tests use AHC...if you can share your client's code I can take a
look.

A+

-Jeanfrancois


On 11-02-14 3:06 PM, spepper wrote:
>
> Hi,
>
> I'm doing some research around atmosphere, specifically around support for
> java clients.  I have downloaded the chat example, but I'm struggling to get
> it to work using the async http client.  Would you happen to have some tests
> or example java client code that has been written to test the rest-enabled
> chat demo using the async http client?
>
> I'm running atmosphere 5.8 on weblogic.
>
> Thanks in advance.
>
> -Steve Pepper
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Chat example with sample java client code

Jeanfrancois Arcand-4
Salut,

On 11-02-15 8:25 PM, spepper wrote:

>
> Hi, Thanks for the response...
>
> Below is my very simple test class.  My problem is that onBodyPartReceived()
> isn't getting called as someone types in a message on the chat window.
> Here's how to exercise:
>
> 1. Change the url in the code.
> 2. Run the client.  It will initialize and then sleep.
> 3. Login to the usual chat web interface and send a message.  I expect to
> see the "Received body part" message in the java client window.
>
> I have upgraded to atmosphere 0.6.5 which seemed to solve my previous
> problem.
>
> Thanks again for your help...

Just to make sure, now it's working with 0.6.5?

Thanks!

- Jeanfrancois


>
>
> //code
> import java.util.concurrent.Future;
>
> import com.ning.http.client.AsyncHandler;
> import com.ning.http.client.AsyncHttpClient;
> import com.ning.http.client.AsyncHttpClientConfig;
> import com.ning.http.client.HttpResponseBodyPart;
> import com.ning.http.client.HttpResponseHeaders;
> import com.ning.http.client.HttpResponseStatus;
> import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder;
>
>
> public class TestChat
> {
>      private static final String url = "<a href="http://host:port/path/to/chat">http://host:port/path/to/chat";
>
>      /**
>       * @param args
>       */
>      public static void main(String[] args)
>      {
>          AsyncHttpClientConfig config = new AsyncHttpClientConfig.Builder()
>              .setRequestTimeoutInMs(10000)
>              .build();
>          AsyncHttpClient httpClient = new AsyncHttpClient(config);
>          BoundRequestBuilder builder = httpClient.prepareGet(url);
>          try
>          {
>              Future<String>  f = builder.execute(new AsyncHandler<String>() {
>
>                  public void onThrowable(Throwable throwable)
>                  {
>                  }
>
>                  public STATE onBodyPartReceived(HttpResponseBodyPart bp)
>                  throws Exception
>                  {
>                      System.out.println("Received body part:" + new
> String(bp.getBodyPartBytes()));
>                      return STATE.CONTINUE;
>                  }
>
>                  public STATE onStatusReceived(HttpResponseStatus hs) throws
> Exception {
>                      return STATE.CONTINUE;
>                  }
>
>                  public STATE onHeadersReceived(HttpResponseHeaders rh)
> throws Exception {
>                      return STATE.CONTINUE;
>                  }
>
>                  public String onCompleted() throws Exception {
>                      System.out.println("onCompleted...");
>                      return "";
>                  }
>              });
>              Thread.sleep(30000);
>              f.get();
>          }
>          catch(Throwable e)
>          {
>              e.printStackTrace();
>          }
>          finally
>          {
>              httpClient.close();
>              System.exit(0);
>          }
>
>      }
>
> }
>
>
>
> jfarcand-3 wrote:
>>
>> Salut,
>>
>> sorry for the delay. Take a look at the PubSub test sample here:
>>
>> https://github.com/Atmosphere/atmosphere/blob/master/modules/jersey/src/test/java/org/atmosphere/jersey/tests/BasePubSubTest.java
>>
>> All the tests use AHC...if you can share your client's code I can take a
>> look.
>>
>> A+
>>
>> -Jeanfrancois
>>
>>
>> On 11-02-14 3:06 PM, spepper wrote:
>>>
>>> Hi,
>>>
>>> I'm doing some research around atmosphere, specifically around support
>>> for
>>> java clients.  I have downloaded the chat example, but I'm struggling to
>>> get
>>> it to work using the async http client.  Would you happen to have some
>>> tests
>>> or example java client code that has been written to test the
>>> rest-enabled
>>> chat demo using the async http client?
>>>
>>> I'm running atmosphere 5.8 on weblogic.
>>>
>>> Thanks in advance.
>>>
>>> -Steve Pepper
>>
>>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Chat example with sample java client code

spepper
Hi, and sorry for the confusion.  

The code that I posted does not work.  onBodyPartReceived() is never invoked when chat messages are received.

-Steve
jfarcand-3 wrote
Salut,

On 11-02-15 8:25 PM, spepper wrote:
>
> Hi, Thanks for the response...
>
> Below is my very simple test class.  My problem is that onBodyPartReceived()
> isn't getting called as someone types in a message on the chat window.
> Here's how to exercise:
>
> 1. Change the url in the code.
> 2. Run the client.  It will initialize and then sleep.
> 3. Login to the usual chat web interface and send a message.  I expect to
> see the "Received body part" message in the java client window.
>
> I have upgraded to atmosphere 0.6.5 which seemed to solve my previous
> problem.
>
> Thanks again for your help...

Just to make sure, now it's working with 0.6.5?

Thanks!

- Jeanfrancois


>
>
> //code
> import java.util.concurrent.Future;
>
> import com.ning.http.client.AsyncHandler;
> import com.ning.http.client.AsyncHttpClient;
> import com.ning.http.client.AsyncHttpClientConfig;
> import com.ning.http.client.HttpResponseBodyPart;
> import com.ning.http.client.HttpResponseHeaders;
> import com.ning.http.client.HttpResponseStatus;
> import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder;
>
>
> public class TestChat
> {
>      private static final String url = "<a href="http://host:port/path/to/chat">http://host:port/path/to/chat";
>
>      /**
>       * @param args
>       */
>      public static void main(String[] args)
>      {
>          AsyncHttpClientConfig config = new AsyncHttpClientConfig.Builder()
>              .setRequestTimeoutInMs(10000)
>              .build();
>          AsyncHttpClient httpClient = new AsyncHttpClient(config);
>          BoundRequestBuilder builder = httpClient.prepareGet(url);
>          try
>          {
>              Future<String>  f = builder.execute(new AsyncHandler<String>() {
>
>                  public void onThrowable(Throwable throwable)
>                  {
>                  }
>
>                  public STATE onBodyPartReceived(HttpResponseBodyPart bp)
>                  throws Exception
>                  {
>                      System.out.println("Received body part:" + new
> String(bp.getBodyPartBytes()));
>                      return STATE.CONTINUE;
>                  }
>
>                  public STATE onStatusReceived(HttpResponseStatus hs) throws
> Exception {
>                      return STATE.CONTINUE;
>                  }
>
>                  public STATE onHeadersReceived(HttpResponseHeaders rh)
> throws Exception {
>                      return STATE.CONTINUE;
>                  }
>
>                  public String onCompleted() throws Exception {
>                      System.out.println("onCompleted...");
>                      return "";
>                  }
>              });
>              Thread.sleep(30000);
>              f.get();
>          }
>          catch(Throwable e)
>          {
>              e.printStackTrace();
>          }
>          finally
>          {
>              httpClient.close();
>              System.exit(0);
>          }
>
>      }
>
> }
>
>
>
> jfarcand-3 wrote:
>>
>> Salut,
>>
>> sorry for the delay. Take a look at the PubSub test sample here:
>>
>> https://github.com/Atmosphere/atmosphere/blob/master/modules/jersey/src/test/java/org/atmosphere/jersey/tests/BasePubSubTest.java
>>
>> All the tests use AHC...if you can share your client's code I can take a
>> look.
>>
>> A+
>>
>> -Jeanfrancois
>>
>>
>> On 11-02-14 3:06 PM, spepper wrote:
>>>
>>> Hi,
>>>
>>> I'm doing some research around atmosphere, specifically around support
>>> for
>>> java clients.  I have downloaded the chat example, but I'm struggling to
>>> get
>>> it to work using the async http client.  Would you happen to have some
>>> tests
>>> or example java client code that has been written to test the
>>> rest-enabled
>>> chat demo using the async http client?
>>>
>>> I'm running atmosphere 5.8 on weblogic.
>>>
>>> Thanks in advance.
>>>
>>> -Steve Pepper
>>
>>
>
ale
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Chat example with sample java client code

ale
This work for me.

I'm using AHC v1.6.0. Atmosphere 0.6.5.

import java.util.*;
import com.ning.http.client.*;

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

                Builder builder = new AsyncHttpClientConfig.Builder();
                builder.setCompressionEnabled(true).setAllowPoolingConnection(true).build();

                final AsyncHttpClient client = new AsyncHttpClient(builder.build());

                client.prepareGet("http://localhost:8080/atmosphere-rest-chat/chat").execute(new AsyncHandler<String>() {
                        public void onThrowable(Throwable t) {
                                System.out.println("onThrowable");
                                t.printStackTrace();
                        }

                        public AsyncHandler.STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
                                System.out.println("onBodyPartReceived");
                                System.out.println(new String(bodyPart.getBodyPartBytes()));
                                return AsyncHandler.STATE.CONTINUE;
                        }

                        public AsyncHandler.STATE onStatusReceived(HttpResponseStatus responseStatus) throws Exception {
                                System.out.println("onStatusReceived");
                                return AsyncHandler.STATE.CONTINUE;
                        }

                        public AsyncHandler.STATE onHeadersReceived(HttpResponseHeaders headers) throws Exception {
                                System.out.println("onHeadersReceived");
                                return AsyncHandler.STATE.CONTINUE;
                        }

                        public String onCompleted() throws Exception {
                                System.out.println("onCompleted");
                                return "";
                        }
                });

                new Timer().schedule(new TimerTask() {
                        @Override
                        public void run() {
                                try {
                                        client.preparePost("http://localhost:8080/atmosphere-rest-chat/chat").addParameter("action", "login").addParameter("name", "test").execute();
                                } catch (Exception e) {
                                        System.err.println("Se pudrio todo");
                                        e.printStackTrace();
                                }
                        }
                }, 5000);

        }
}


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Chat example with sample java client code

bernard.pirenne
Hello,

I'v the same problem that speper : code that you've posted seems does not work and method onBodyPartReceived() is never invoked when chat messages are received. But method to post message (client.preparePost) working well.

I'm using AHC v1.6.4 and atmosphere 0.7.2 (chat rest example)

Bernard
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Chat example with sample java client code

Jeanfrancois Arcand-4
Salut,

can you post your sample?

Thanks

-- Jeanfrancois

On 11-07-21 12:04 PM, bernard.pirenne wrote:

> Hello,
>
> I'v the same problem that speper : code that you've posted seems does not
> work and method onBodyPartReceived() is never invoked when chat messages are
> received. But method to post message (client.preparePost) working well.
>
> I'm using AHC v1.6.4 and atmosphere 0.7.2 (chat rest example)
>
> Bernard
>
> --
> View this message in context: http://atmosphere-users-mailling-list.2493822.n2.nabble.com/Chat-example-with-sample-java-client-code-tp6024984p6607414.html
> Sent from the Atmosphere users mailling list mailing list archive at Nabble.com.
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Chat example with sample java client code

bernard.pirenne
Yes of course ! Thank's to your time !

import java.util.Timer;
import java.util.TimerTask;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ning.http.client.AsyncHandler;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.AsyncHttpClientConfig;
import com.ning.http.client.HttpResponseBodyPart;
import com.ning.http.client.HttpResponseHeaders;
import com.ning.http.client.HttpResponseStatus;
import com.ning.http.client.AsyncHttpClientConfig.Builder;

public class ClientChat {
        private static final Logger logger = LoggerFactory.getLogger(ClientChat.class);
       
        public static void main(String[] args) throws Exception {

            Builder builder = new AsyncHttpClientConfig.Builder();
            builder.setCompressionEnabled(true).setAllowPoolingConnection(true).build();

            final AsyncHttpClient client = new AsyncHttpClient(builder.build());

            client.prepareGet("http://127.0.0.1:8080/atmosphere-rest-chat/chat").execute(new AsyncHandler<String>() {
                    public void onThrowable(Throwable t) {
                            System.out.println("onThrowable");
                            t.printStackTrace();
                    }

                    public AsyncHandler.STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
                            System.out.println("onBodyPartReceived");
                            System.out.println(new String(bodyPart.getBodyPartBytes()));
                            return AsyncHandler.STATE.CONTINUE;
                    }

                    public AsyncHandler.STATE onStatusReceived(HttpResponseStatus responseStatus) throws Exception {
                            System.out.println("onStatusReceived");
                            return AsyncHandler.STATE.CONTINUE;
                    }

                    public AsyncHandler.STATE onHeadersReceived(HttpResponseHeaders headers) throws Exception {
                            System.out.println("onHeadersReceived");
                            return AsyncHandler.STATE.CONTINUE;
                    }

                    public String onCompleted() throws Exception {
                            System.out.println("onCompleted");
                            return "";
                    }
            });
       
                new Timer().schedule(new TimerTask() {
                        @Override
                        public void run() {
                                try {
                                        client.preparePost("http://127.0.0.1:8080/atmosphere-rest-chat/chat").
                                        addParameter("action", "login").
                                        addParameter("name", "testJava").execute();
                                       
                                } catch (Exception e) {
                                        System.err.println("Se pudrio todo");
                                        e.printStackTrace();
                                }
                        }
                }, 5000);
               
               
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                            try {
                                    client.preparePost("http://127.0.0.1:8080/atmosphere-rest-chat/chat")
                                    .addParameter("action", "post").
                                    addParameter("name", "testJava").
                                    addParameter("message", "test msg from java test").execute();
                            } catch (Exception e) {
                                    System.err.println("Se pudrio todo");
                                    e.printStackTrace();
                            }
                    }
            }, 15000);                

        }
}
Loading...