Search in sources :

Example 1 with WebSocketCodec

use of com.firenio.codec.http11.WebSocketCodec in project baseio by generallycloud.

the class TestSimpleHttpClient method main.

@Test
public void main() throws Exception {
    Options.setEnableEpoll(true);
    Waiter<String> w = new Waiter<>();
    ChannelConnector context = new ChannelConnector("firenio.com", 443);
    SslContext sslContext = SslContextBuilder.forClient(true).build();
    context.addProtocolCodec(new ClientHttpCodec());
    context.addProtocolCodec(new WebSocketCodec());
    context.setIoEventHandle(new IoEventHandle() {

        @Override
        public void accept(Channel ch, Frame frame) {
            ClientHttpFrame res = (ClientHttpFrame) frame;
            System.out.println();
            System.out.println(new String(res.getBytesContent()));
            System.out.println();
            Util.close(context);
            w.call(new String(res.getBytesContent()), null);
        }
    });
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.setSslContext(sslContext);
    long start = Util.now();
    Channel ch = context.connect(3000);
    ch.writeAndFlush(new ClientHttpFrame("/test?p=2222"));
    w.await(3000);
    System.out.println(Util.past(start));
    Assert.assertEquals("yes server already accept your message :) {p=2222}", w.getResponse());
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) Frame(com.firenio.component.Frame) ClientHttpFrame(com.firenio.codec.http11.ClientHttpFrame) Channel(com.firenio.component.Channel) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener) ChannelConnector(com.firenio.component.ChannelConnector) ClientHttpCodec(com.firenio.codec.http11.ClientHttpCodec) Waiter(com.firenio.concurrent.Waiter) ClientHttpFrame(com.firenio.codec.http11.ClientHttpFrame) SslContext(com.firenio.component.SslContext) WebSocketCodec(com.firenio.codec.http11.WebSocketCodec) Test(org.junit.Test)

Example 2 with WebSocketCodec

use of com.firenio.codec.http11.WebSocketCodec in project baseio by generallycloud.

the class TestSimpleHttpClient2 method main.

// telnet 192.168.133.134 8080
public static void main(String[] args) throws Exception {
    String host = "www.baidu.com";
    String url = "/plaintext";
    host = "firenio.com";
    host = "127.0.0.1";
    // host = "fe80::a793:9577:4396:8ca6";
    // host = "www.baidu.com";
    // host = "api.weixin.qq.com";
    // host = "192.168.1.103";
    int port = 1443;
    port = 8300;
    // port = 443;
    ChannelConnector context = new ChannelConnector(host, port);
    context.addProtocolCodec(new ClientHttpCodec());
    context.addProtocolCodec(new WebSocketCodec());
    context.setIoEventHandle(new IoEventHandle() {

        @Override
        public void accept(Channel ch, Frame frame) throws Exception {
            ClientHttpFrame res = (ClientHttpFrame) frame;
            System.out.println();
            System.out.println(new String(res.getBytesContent()));
            System.out.println();
        // Util.close(context);
        }
    });
    context.addChannelEventListener(new ChannelEventListenerAdapter() {

        @Override
        public void channelOpened(Channel ch) throws Exception {
            throw new IOException();
        }
    });
    context.addChannelEventListener(new LoggerChannelOpenListener());
    if (port == 1443 || port == 443) {
        context.setSslContext(SslContextBuilder.forClient(true).build());
    }
    Channel ch = context.connect(999999);
    ClientHttpFrame f = new ClientHttpFrame(url, HttpMethod.GET);
    // f.setRequestHeader(HttpHeader.Host, host);
    // f.setContent("abc123".readBytes());
    ch.write(f);
    ch.writeAndFlush(f);
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) Frame(com.firenio.component.Frame) ClientHttpFrame(com.firenio.codec.http11.ClientHttpFrame) Channel(com.firenio.component.Channel) IOException(java.io.IOException) ChannelEventListenerAdapter(com.firenio.component.ChannelEventListenerAdapter) IOException(java.io.IOException) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener) ChannelConnector(com.firenio.component.ChannelConnector) ClientHttpCodec(com.firenio.codec.http11.ClientHttpCodec) ClientHttpFrame(com.firenio.codec.http11.ClientHttpFrame) WebSocketCodec(com.firenio.codec.http11.WebSocketCodec)

Example 3 with WebSocketCodec

use of com.firenio.codec.http11.WebSocketCodec in project baseio by generallycloud.

the class HttpFrameHandle method exceptionCaught.

@Override
public void exceptionCaught(Channel ch, Frame frame, Throwable ex) {
    logger.error(ex.getMessage(), ex);
    frame.release();
    if (!ch.isOpen()) {
        return;
    }
    if (ch.getCodec() instanceof WebSocketCodec) {
        WebSocketFrame f = new WebSocketFrame();
        f.setString((ex.getClass() + ex.getMessage()), ch);
        try {
            ch.writeAndFlush(f);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    } else {
        StringBuilder builder = new StringBuilder();
        builder.append("            <div>oops, server threw an inner exception, the stack trace is :</div>\n");
        builder.append("            <div style=\"font-family:serif;color:#5c5c5c;\">\n");
        builder.append("            -------------------------------------------------------</BR>\n");
        builder.append("            ");
        builder.append(ex.toString());
        builder.append("</BR>\n");
        StackTraceElement[] es = ex.getStackTrace();
        for (StackTraceElement e : es) {
            builder.append("                &emsp;at ");
            builder.append(e.toString());
            builder.append("</BR>\n");
        }
        try {
            printHtml(ch, frame, HttpStatus.C500, builder.toString());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
}
Also used : WebSocketFrame(com.firenio.codec.http11.WebSocketFrame) IOException(java.io.IOException) WebSocketCodec(com.firenio.codec.http11.WebSocketCodec)

Example 4 with WebSocketCodec

use of com.firenio.codec.http11.WebSocketCodec in project baseio by generallycloud.

the class TestHttpBootstrapEngine method bootstrap.

@Override
public void bootstrap(String rootPath, boolean prodMode) throws Exception {
    ClassLoader cl = this.getClass().getClassLoader();
    boolean debug = Util.isTrueValue(System.getProperty("http.debug"));
    if (debug) {
        for (; debug; ) {
            Util.sleep(100);
        }
    }
    DevelopConfig.NATIVE_DEBUG = true;
    DevelopConfig.BUF_DEBUG = true;
    DevelopConfig.BUF_PATH_DEBUG = true;
    DevelopConfig.SSL_DEBUG = true;
    DevelopConfig.CHANNEL_DEBUG = true;
    DevelopConfig.DEBUG_ERROR = true;
    Options.setEnableEpoll(true);
    Options.setEnableUnsafe(true);
    Options.setEnableOpenssl(true);
    Options.setBufThreadYield(true);
    // Options.setEnableUnsafeBuf(true);
    HttpDateUtil.start();
    final SpringHttpFrameHandle handle = new SpringHttpFrameHandle();
    Properties properties = FileUtil.readPropertiesByCls("server.properties", cl);
    NioEventLoopGroup group = new NioEventLoopGroup(true);
    ChannelAcceptor context = new ChannelAcceptor(group);
    ConfigurationParser.parseConfiguration("server.", context, properties);
    ConfigurationParser.parseConfiguration("server.", group, properties);
    context.setIoEventHandle(handle);
    context.addChannelIdleEventListener(new ChannelAliveListener());
    context.addChannelEventListener(new WebSocketChannelListener());
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.addChannelEventListener(new CountChannelListener());
    context.setExecutorGroup(new ThreadEventLoopGroup());
    context.addLifeCycleListener(new LifeCycleListener() {

        @Override
        public void lifeCycleStarting(LifeCycle lifeCycle) {
            try {
                handle.initialize(context, rootPath, prodMode);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }

        @Override
        public void lifeCycleStopped(LifeCycle lifeCycle) {
            handle.destroy(context);
        }
    });
    String[] applicationProtocols = null;
    if (properties.getBooleanProperty("app.enableHttp2")) {
        context.addProtocolCodec(new Http2Codec());
        applicationProtocols = new String[] { "h2", "http/1.1" };
    } else {
        context.addProtocolCodec(new HttpCodec(4));
        context.addProtocolCodec(new WebSocketCodec());
    }
    int defaultPort = 80;
    String pem = properties.getProperty("server.sslPem");
    if (!Util.isNullOrBlank(pem)) {
        defaultPort = 443;
        SslContext sslContext = loadSslContextFromPem(pem, applicationProtocols, cl);
        context.setSslContext(sslContext);
    }
    int port = properties.getIntegerProperty("server.port", defaultPort);
    context.setPort(port);
    try {
        context.bind();
    } catch (Exception e) {
        HttpDateUtil.stop();
        group.stop();
        throw e;
    }
    this.group = group;
    this.context = context;
    if (properties.getBooleanProperty("app.proxy")) {
        NetDataTransferServer.get().startup(group, 18088);
    }
}
Also used : LifeCycle(com.firenio.LifeCycle) ChannelAliveListener(com.firenio.component.ChannelAliveListener) ChannelAcceptor(com.firenio.component.ChannelAcceptor) LifeCycleListener(com.firenio.LifeCycleListener) Properties(com.firenio.common.Properties) WebSocketChannelListener(com.firenio.codec.http11.WebSocketChannelListener) SSLException(javax.net.ssl.SSLException) Http2Codec(com.firenio.codec.http2.Http2Codec) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener) ThreadEventLoopGroup(com.firenio.concurrent.ThreadEventLoopGroup) HttpCodec(com.firenio.codec.http11.HttpCodec) CountChannelListener(sample.http11.service.CountChannelListener) SpringHttpFrameHandle(sample.http11.SpringHttpFrameHandle) NioEventLoopGroup(com.firenio.component.NioEventLoopGroup) WebSocketCodec(com.firenio.codec.http11.WebSocketCodec) SslContext(com.firenio.component.SslContext)

Example 5 with WebSocketCodec

use of com.firenio.codec.http11.WebSocketCodec in project baseio by generallycloud.

the class TestSimpleWebSocketClient method main.

public static void main(String[] args) throws Exception {
    IoEventHandle eventHandleAdaptor = new IoEventHandle() {

        @Override
        public void accept(Channel ch, Frame frame) throws Exception {
            if (frame instanceof ClientHttpFrame) {
                ClientHttpFrame f = (ClientHttpFrame) frame;
                if (f.updateWebSocketProtocol(ch)) {
                    WebSocketFrame f2 = new WebSocketFrame();
                    Map<String, String> map = new HashMap<>();
                    map.put("action", "add-user");
                    map.put("username", "火星人" + Util.randomUUID());
                    f2.setString(JSON.toJSONString(map), ch);
                    ch.writeAndFlush(f2);
                }
                System.out.println(f.getResponse_headers());
            } else {
                WebSocketFrame f = (WebSocketFrame) frame;
                System.out.println(f.getStringContent());
            }
        }
    };
    String host = "firenio.com";
    int port = 443;
    NioEventLoopGroup g = new NioEventLoopGroup();
    g.setEnableMemoryPool(false);
    ChannelConnector context = new ChannelConnector(g, host, 443);
    // context.setExecutorGroup(new ExecutorEventLoopGroup());
    context.setIoEventHandle(eventHandleAdaptor);
    context.addProtocolCodec(new ClientHttpCodec());
    context.addProtocolCodec(new WebSocketCodec());
    context.addChannelEventListener(new LoggerChannelOpenListener());
    context.setSslContext(SslContextBuilder.forClient(true).build());
    Channel ch = context.connect();
    String url = "/web-socket-chat";
    HttpFrame frame = new WsUpgradeRequestFrame(url);
    frame.setRequestHeader(HttpHeader.Host, host + port);
    frame.setRequestHeader(HttpHeader.Pragma, "no-cache");
    frame.setRequestHeader(HttpHeader.Cache_Control, "no-cache");
    frame.setRequestHeader(HttpHeader.User_Agent, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36");
    frame.setRequestHeader(HttpHeader.Accept_Encoding, "gzip, deflate, sdch");
    frame.setRequestHeader(HttpHeader.Accept_Language, "zh-CN,zh;q=0.8");
    ch.writeAndFlush(frame);
    Util.sleep(100);
    WebSocketFrame f2 = new WebSocketFrame();
    Map<String, String> map = new HashMap<>();
    map.put("action", "new-message");
    map.put("message", TestUtil.newString(1024 * 8));
    f2.setString(JSON.toJSONString(map), ch);
    ch.writeAndFlush(f2);
    Util.sleep(999999999);
    Util.close(context);
}
Also used : IoEventHandle(com.firenio.component.IoEventHandle) Frame(com.firenio.component.Frame) ClientHttpFrame(com.firenio.codec.http11.ClientHttpFrame) WebSocketFrame(com.firenio.codec.http11.WebSocketFrame) WsUpgradeRequestFrame(com.firenio.codec.http11.WsUpgradeRequestFrame) HttpFrame(com.firenio.codec.http11.HttpFrame) HashMap(java.util.HashMap) Channel(com.firenio.component.Channel) ClientHttpFrame(com.firenio.codec.http11.ClientHttpFrame) HttpFrame(com.firenio.codec.http11.HttpFrame) LoggerChannelOpenListener(com.firenio.component.LoggerChannelOpenListener) WsUpgradeRequestFrame(com.firenio.codec.http11.WsUpgradeRequestFrame) ChannelConnector(com.firenio.component.ChannelConnector) ClientHttpCodec(com.firenio.codec.http11.ClientHttpCodec) WebSocketFrame(com.firenio.codec.http11.WebSocketFrame) ClientHttpFrame(com.firenio.codec.http11.ClientHttpFrame) NioEventLoopGroup(com.firenio.component.NioEventLoopGroup) WebSocketCodec(com.firenio.codec.http11.WebSocketCodec)

Aggregations

WebSocketCodec (com.firenio.codec.http11.WebSocketCodec)6 LoggerChannelOpenListener (com.firenio.component.LoggerChannelOpenListener)5 Channel (com.firenio.component.Channel)4 Frame (com.firenio.component.Frame)4 IoEventHandle (com.firenio.component.IoEventHandle)4 ClientHttpCodec (com.firenio.codec.http11.ClientHttpCodec)3 ClientHttpFrame (com.firenio.codec.http11.ClientHttpFrame)3 ChannelConnector (com.firenio.component.ChannelConnector)3 NioEventLoopGroup (com.firenio.component.NioEventLoopGroup)3 HttpCodec (com.firenio.codec.http11.HttpCodec)2 HttpFrame (com.firenio.codec.http11.HttpFrame)2 WebSocketFrame (com.firenio.codec.http11.WebSocketFrame)2 ChannelAcceptor (com.firenio.component.ChannelAcceptor)2 SslContext (com.firenio.component.SslContext)2 IOException (java.io.IOException)2 LifeCycle (com.firenio.LifeCycle)1 LifeCycleListener (com.firenio.LifeCycleListener)1 WebSocketChannelListener (com.firenio.codec.http11.WebSocketChannelListener)1 WsUpgradeRequestFrame (com.firenio.codec.http11.WsUpgradeRequestFrame)1 Http2Codec (com.firenio.codec.http2.Http2Codec)1