Skip to content

Instantly share code, notes, and snippets.

@jeremy303
Created June 26, 2012 21:57
Show Gist options
  • Save jeremy303/2999527 to your computer and use it in GitHub Desktop.
Save jeremy303/2999527 to your computer and use it in GitHub Desktop.
Atmosphere WebSocket echo server experiment.
package scratch;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import org.atmosphere.annotation.Broadcast;
import org.atmosphere.annotation.Suspend;
import org.atmosphere.cpr.AtmosphereServlet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
/**
* WebSocket echo server experiment w/ Atmosphere-Jersey and Embedded Jetty
*
* @Suspend handler executes when the websocket connects but the @POST handler is never called when the client sends messages to the server.
*
* Dependencies:
* - Atmosphere 0.9.5
* - Jetty 8.1.1.v20120215
* - Jersey 1.12
*
* @author Jeremy M. Johnson
*
*/
@Path("/echo/")
@Consumes( "text/plain")
@Produces("text/plain")
public class EchoResource
{
@Suspend
@GET
public String suspend()
{
return "HELLO";
}
//@Broadcast( writeEntity=true)
@POST
public String receive( String request)
{
return request;
}
public static void main(String[] args) throws Exception
{
AtmosphereServlet atmosphereServlet = new AtmosphereServlet();
ServletHolder sh = new ServletHolder(atmosphereServlet);
sh.setInitParameter("com.sun.jersey.config.property.packages", "scratch");
ServletContextHandler context = new ServletContextHandler( ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.addServlet(sh, "/*");
Server server = new Server(8080);
server.setHandler(context);
server.start();
server.join();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment