Skip to content

Instantly share code, notes, and snippets.

@flowersinthesand
Last active December 11, 2015 00:28
Show Gist options
  • Save flowersinthesand/4516326 to your computer and use it in GitHub Desktop.
Save flowersinthesand/4516326 to your computer and use it in GitHub Desktop.
package com.github.flowersinthesand.samples;
import javax.servlet.annotation.WebServlet;
import org.atmosphere.cpr.AtmosphereServlet;
@SuppressWarnings("serial")
@WebServlet(urlPatterns = "/test-with-atmosphere", loadOnStartup = 0)
public class MyServlet extends AtmosphereServlet {
}
/*
* Copyright 2012 Donghwan Kim
*
* 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 com.github.flowersinthesand.samples;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import org.atmosphere.config.service.AtmosphereHandlerService;
import org.atmosphere.cpr.AtmosphereHandler;
import org.atmosphere.cpr.AtmosphereRequest;
import org.atmosphere.cpr.AtmosphereResource;
import org.atmosphere.cpr.AtmosphereResourceEvent;
import org.atmosphere.cpr.AtmosphereResourceEventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@AtmosphereHandlerService(path = "/test-with-atmosphere")
public class TestAtmosphereHandler implements AtmosphereHandler {
private final Logger logger = LoggerFactory.getLogger(TestAtmosphereHandler.class);
@Override
public void onRequest(final AtmosphereResource resource) throws IOException {
AtmosphereRequest request = resource.getRequest();
if (request.getMethod().equalsIgnoreCase("GET")) {
resource.addEventListener(new AtmosphereResourceEventListener() {
@Override
public void onPreSuspend(AtmosphereResourceEvent event) {
logger.debug("onPreSuspend {}", event);
}
@Override
public void onSuspend(AtmosphereResourceEvent event) {
logger.debug("onSuspend {}", event);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
logger.debug("Time to bid farewell");
resource.resume();
}
}, 20000);
}
@Override
public void onBroadcast(AtmosphereResourceEvent event) {
logger.debug("onBroadcast {}", event);
}
@Override
public void onThrowable(AtmosphereResourceEvent event) {
logger.debug("onThrowable {}", event);
}
@Override
public void onResume(AtmosphereResourceEvent event) {
logger.debug("onResume {}", event);
}
@Override
public void onDisconnect(AtmosphereResourceEvent event) {
logger.debug("onDisconnect {}", event);
}
}).suspend();
}
}
@Override
public void onStateChange(AtmosphereResourceEvent event) throws IOException {
}
@Override
public void destroy() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment