Search in sources :

Example 1 with BroadcastSession

use of com.cgbystrom.sockjs.test.BroadcastSession in project sockjs-netty by cgbystrom.

the class TestServer method main.

public static void main(String[] args) {
    Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    LoggerContext loggerContext = rootLogger.getLoggerContext();
    loggerContext.reset();
    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setContext(loggerContext);
    encoder.setPattern("%-5level %-20class{0}: %message%n");
    encoder.start();
    ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<ILoggingEvent>();
    appender.setContext(loggerContext);
    appender.setEncoder(encoder);
    appender.start();
    rootLogger.addAppender(appender);
    InternalLoggerFactory.setDefaultFactory(new Slf4JLoggerFactory());
    ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
    final MetricRegistry registry = new MetricRegistry();
    final JmxReporter reporter = JmxReporter.forRegistry(registry).build();
    reporter.start();
    final ServiceRouter router = new ServiceRouter();
    router.setMetricRegistry(registry);
    router.registerService(new Service("/disabled_websocket_echo", new DisabledWebSocketEchoSession()));
    router.registerService(new Service("/close", new CloseSession()));
    router.registerService(new Service("/amplify", new AmplifySession()));
    router.registerService(new Service("/broadcast", new SessionCallbackFactory() {

        @Override
        public BroadcastSession getSession(String id) throws Exception {
            return new BroadcastSession();
        }
    }));
    Service echoService = new Service("/echo", new SessionCallbackFactory() {

        @Override
        public EchoSession getSession(String id) throws Exception {
            return new EchoSession();
        }
    });
    echoService.setMaxResponseSize(4096);
    router.registerService(echoService);
    Service cookieNeededEcho = new Service("/cookie_needed_echo", new EchoSession());
    cookieNeededEcho.setMaxResponseSize(4096);
    cookieNeededEcho.setCookieNeeded(true);
    router.registerService(cookieNeededEcho);
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {

        @Override
        public ChannelPipeline getPipeline() throws Exception {
            ChannelPipeline pipeline = pipeline();
            pipeline.addLast("decoder", new HttpRequestDecoder());
            // Required for WS handshaker or else NPE.
            pipeline.addLast("chunkAggregator", new HttpChunkAggregator(130 * 1024));
            pipeline.addLast("encoder", new HttpResponseEncoder());
            pipeline.addLast("preflight", new PreflightHandler());
            pipeline.addLast("router", router);
            return pipeline;
        }
    });
    bootstrap.bind(new InetSocketAddress(8090));
    System.out.println("Server running..");
}
Also used : ConsoleAppender(ch.qos.logback.core.ConsoleAppender) InetSocketAddress(java.net.InetSocketAddress) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) HttpRequestDecoder(org.jboss.netty.handler.codec.http.HttpRequestDecoder) Slf4JLoggerFactory(org.jboss.netty.logging.Slf4JLoggerFactory) PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) MetricRegistry(com.codahale.metrics.MetricRegistry) HttpChunkAggregator(org.jboss.netty.handler.codec.http.HttpChunkAggregator) BroadcastSession(com.cgbystrom.sockjs.test.BroadcastSession) LoggerContext(ch.qos.logback.classic.LoggerContext) JmxReporter(com.codahale.metrics.JmxReporter) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) HttpResponseEncoder(org.jboss.netty.handler.codec.http.HttpResponseEncoder) ChannelPipelineFactory(org.jboss.netty.channel.ChannelPipelineFactory)

Aggregations

Logger (ch.qos.logback.classic.Logger)1 LoggerContext (ch.qos.logback.classic.LoggerContext)1 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)1 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)1 ConsoleAppender (ch.qos.logback.core.ConsoleAppender)1 BroadcastSession (com.cgbystrom.sockjs.test.BroadcastSession)1 JmxReporter (com.codahale.metrics.JmxReporter)1 MetricRegistry (com.codahale.metrics.MetricRegistry)1 InetSocketAddress (java.net.InetSocketAddress)1 ServerBootstrap (org.jboss.netty.bootstrap.ServerBootstrap)1 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)1 ChannelPipelineFactory (org.jboss.netty.channel.ChannelPipelineFactory)1 NioServerSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory)1 HttpChunkAggregator (org.jboss.netty.handler.codec.http.HttpChunkAggregator)1 HttpRequestDecoder (org.jboss.netty.handler.codec.http.HttpRequestDecoder)1 HttpResponseEncoder (org.jboss.netty.handler.codec.http.HttpResponseEncoder)1 Slf4JLoggerFactory (org.jboss.netty.logging.Slf4JLoggerFactory)1