Last active
August 29, 2015 13:57
-
-
Save jfarcand/9788330 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2014 Jeanfrancois Arcand | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | |
* use this file except in compliance with the License. You may obtain a copy of | |
* the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |
* License for the specific language governing permissions and limitations under | |
* the License. | |
*/ | |
package org.atmosphere.samples.chat; | |
import org.atmosphere.client.TrackMessageSizeInterceptor; | |
import org.atmosphere.config.service.MeteorService; | |
import org.atmosphere.cpr.AtmosphereResourceEventListenerAdapter; | |
import org.atmosphere.cpr.BroadcasterFactory; | |
import org.atmosphere.cpr.DefaultBroadcaster; | |
import org.atmosphere.cpr.Meteor; | |
import org.atmosphere.interceptor.AtmosphereResourceLifecycleInterceptor; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.IOException; | |
import java.util.Date; | |
/** | |
* Simple Servlet that implement the logic to build a Chat application using | |
* a {@link Meteor} to suspend and broadcast chat message. | |
* | |
* @author Jeanfrancois Arcand | |
*/ | |
@MeteorService(path = "/*", atmosphereConfig = "org.atmosphere.useStream=false", interceptors = {AtmosphereResourceLifecycleInterceptor.class, TrackMessageSizeInterceptor.class}) | |
public class MeteorChat extends HttpServlet { | |
/** | |
* Create a {@link Meteor} and use it to suspend the response. | |
* | |
* @param req An {@link HttpServletRequest} | |
* @param res An {@link HttpServletResponse} | |
*/ | |
@Override | |
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { | |
// Set the logger level to TRACE to see what's happening. | |
Meteor.build(req).addListener(new AtmosphereResourceEventListenerAdapter()); | |
res.getWriter().print(""); | |
} | |
/** | |
* Re-use the {@link Meteor} created on the first GET for broadcasting message. | |
* | |
* @param req An {@link HttpServletRequest} | |
* @param res An {@link HttpServletResponse} | |
*/ | |
@Override | |
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException { | |
for (int i=0; i <50; i++) { | |
BroadcasterFactory.getDefault().lookup(DefaultBroadcaster.class, "/*").broadcast(getString(1000000)); | |
} | |
} | |
protected String getString(int len) { | |
StringBuilder b = new StringBuilder(); | |
for (int i = 0; i < len; i++) { | |
if (i % 100 == 0) { | |
b.append("\n"); | |
} else { | |
b.append('A'); | |
} | |
//b.append(i); | |
} | |
return b.toString(); | |
} | |
private final static class Data { | |
private final String text; | |
private final String author; | |
public Data(String author, String text) { | |
this.author = author; | |
this.text = text; | |
} | |
public String toString() { | |
return "{ \"text\" : \"" + text + "\", \"author\" : \"" + author + "\" , \"time\" : " + new Date().getTime() + "}"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment