Search in sources :

Example 1 with WsUpgradeRequestFrame

use of com.firenio.codec.http11.WsUpgradeRequestFrame 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

ClientHttpCodec (com.firenio.codec.http11.ClientHttpCodec)1 ClientHttpFrame (com.firenio.codec.http11.ClientHttpFrame)1 HttpFrame (com.firenio.codec.http11.HttpFrame)1 WebSocketCodec (com.firenio.codec.http11.WebSocketCodec)1 WebSocketFrame (com.firenio.codec.http11.WebSocketFrame)1 WsUpgradeRequestFrame (com.firenio.codec.http11.WsUpgradeRequestFrame)1 Channel (com.firenio.component.Channel)1 ChannelConnector (com.firenio.component.ChannelConnector)1 Frame (com.firenio.component.Frame)1 IoEventHandle (com.firenio.component.IoEventHandle)1 LoggerChannelOpenListener (com.firenio.component.LoggerChannelOpenListener)1 NioEventLoopGroup (com.firenio.component.NioEventLoopGroup)1 HashMap (java.util.HashMap)1