Giter VIP home page Giter VIP logo

Comments (5)

sophiaso avatar sophiaso commented on May 28, 2024

I can get the response message with the same client but with another server implemented as @managedservice just as the sample Chat.

So I have to implement the same as the Chat server? What if I have to reuse the same server used by the js client.

from wasync.

jfarcand avatar jfarcand commented on May 28, 2024

It should work, so I'm not sure what is the issue. Can you share a test case?

from wasync.

jfarcand avatar jfarcand commented on May 28, 2024

Also, can you use the mailing list for such question? One recommendation is can you try the sample's chat and see if it works for you? Thanks for your help!

from wasync.

sophiaso avatar sophiaso commented on May 28, 2024

Sorry for the inconvenience, I wrote a test case and the problems seems caused by Jackson. I will use the mailing list in the future. Test case sample attached, in case anyone else run into the same issue.

public final static class POJO3{
        public String name;
        public List<String> books;
        
        public POJO3(String name, List<String> books){
            this.name = name;
            this.books = books;
        }
        
        public POJO3(){
            //Without this empty constructor, jackson will fail the test case
        }
    }
    
    @Test
    public void basicDecoderTest2() throws Exception {
        final CountDownLatch l = new CountDownLatch(1);
        final ObjectMapper mapper = new ObjectMapper();
        
        Config config = new Config.Builder()
                .port(port)
                .host("127.0.0.1")
                .resource("/suspend", new AtmosphereHandler() {
                    private final AtomicBoolean b = new AtomicBoolean(false);
                    @Override
                    public void onRequest(AtmosphereResource r) throws IOException {
                        if (!b.getAndSet(true)) {
                            r.suspend(-1);
                        } else {
                            String message = r.getRequest().getReader().readLine();
                            r.getBroadcaster().broadcast(mapper.writeValueAsString(new POJO3(message, Lists.newArrayList("test"))));
                        }
                    }
                    @Override
                    public void onStateChange(AtmosphereResourceEvent r) throws IOException {
                        if (!r.isResuming() || !r.isCancelled()) {
                            r.getResource().getResponse().getWriter().print(r.getMessage());
                            r.getResource().resume();
                        }
                    }
                    @Override
                    public void destroy() {
                    }
                }).build();
        server = new Nettosphere.Builder().config(config).build();
        assertNotNull(server);
        server.start();
        final CountDownLatch latch = new CountDownLatch(1);
        final AtomicReference response = new AtomicReference();
        Client client = ClientFactory.getDefault().newClient();
        RequestBuilder request = client.newRequestBuilder()
                .method(Request.METHOD.GET)
                .uri(targetUrl + "/suspend")
                .decoder(new Decoder() {
                    @Override
                    public POJO3 decode(Event e, String s) {
                        if (e.equals(Event.MESSAGE)) {
                            try {
                                return mapper.readValue(s, POJO3.class);
                            } catch (IOException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                                return null;
                            }
                        }
                        return null;
                    }
                })
                .transport(transport());
        Socket socket = client.create();
        socket.on(Event.MESSAGE.name(), new Function() {
            @Override
            public void on(POJO3 t) {
                response.set(t);
                latch.countDown();
            }
        }).open(request.build()).fire("echo");
        latch.await(10, TimeUnit.SECONDS);
        assertNotNull(response.get());
        assertEquals(response.get().getClass(), POJO3.class);
        socket.close();
    }

from wasync.

jfarcand avatar jfarcand commented on May 28, 2024

Thanks for the test case, always appreciated.

from wasync.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.