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);
}
Aggregations