users@grizzly.java.net

Re: Am I crazy? Yes... (I think I know more, so will re-ask)

From: Paul.M <paul.mattheis_at_gmail.com>
Date: Mon, 8 Feb 2010 18:39:11 -0800 (PST)

I still used a seperate GrizzlyAdapter for static content, makes me happy :)
You can see I had the server send a message out to all clients when it
receives a message.

Again, I am so happy about this... Now I can move on to the next piece of
this project. (The plan is to have an automated task board, with boards in
different locations. Then changes to the tasks get reflected to all boards)

public class CometServer {

    public static void main(String args[]) {
        try {
            GrizzlyWebServer ws = new GrizzlyWebServer(8080, "/var/www");

            ws.addAsyncFilter(new CometAsyncFilter());

            CometdAdapter cometdAdapter = new CometdAdapter(new
PublishInterceptor() {

                public void onPublish(PublishContext context,
DeliverResponse patternResponse) {
                    final String channel = context.getChannel();
                    final Set<DataHandler> handlers =
context.lookupClientHandlers(channel);

                    final CometContext cometContext =
CometEngine.getEngine().getCometContext(channel);

                    final DataHandler sender = context.getSenderClient();

                    Map map = new HashMap();
                    map.put("chat", "Howdy");
                    map.put("user", "server");
                    Data data = new Data();
                    data.setMapData(map);

                    DeliverResponse deliverResponse = new DeliverResponse();
                    deliverResponse.setChannel("/chat/demo");
                    deliverResponse.setData(data);
                    deliverResponse.setId("99");
                    deliverResponse.setClientId("");
                    deliverResponse.setFollow(true);

                    /// Send message to all clients except the sender
                    for (DataHandler dataHandler : handlers) {
                        try {
                            // Send Test Message everywhere
                            cometContext.notify(deliverResponse,
dataHandler);
                            if (dataHandler != sender) {
                                System.out.println("Sending to client: " +
dataHandler.getClientId());
                                cometContext.notify(patternResponse,
dataHandler);
                            }
                        } catch (IOException ex) {
                           
Logger.getLogger(CometServer.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            });

            cometdAdapter.setHandleStaticResources(false);
            ws.addGrizzlyAdapter(cometdAdapter, new String[]{"/cometd/*"});

            GrizzlyAdapter ga = new GrizzlyAdapter() {

                @Override
                public void service(GrizzlyRequest request, GrizzlyResponse
response) {
                    System.out.println(request.getMethod() + " " +
request.getQueryString());
                }
            };
            ga.addRootFolder("/var/www");
            ga.setHandleStaticResources(true);
            ws.addGrizzlyAdapter(ga, new String[]{"/*"});

            ws.start();
        } catch (IOException ex) {
        }
    }

Paul.M wrote:
>
> Hot Holy Dog!
>
> That works to do what I want!!! Thank you so much. When I get my last
> bit working, I'll post the whole working example (with different response
> than patternResponse).
>

-- 
View this message in context: http://old.nabble.com/Am-I-crazy--Yes...--%28I-think-I-know-more%2C-so-will-re-ask%29-tp27475268p27509648.html
Sent from the Grizzly - Users mailing list archive at Nabble.com.