use of com.generallycloud.baseio.component.SocketChannelContext in project baseio by generallycloud.
the class TestSimpleWebSocketClient method main.
public static void main(String[] args) throws Exception {
IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {
@Override
public void accept(SocketSession session, Future future) throws Exception {
if (future instanceof ClientHttpFuture) {
ClientHttpFuture f = (ClientHttpFuture) future;
if (f.getRequestHeader("Sec-WebSocket-Accept") != null) {
f.updateWebSocketProtocol();
WebSocketFuture f2 = new WebSocketFutureImpl(session.getContext());
f2.write("{action: \"add-user\", username: \"火星人\"}");
// f2.write("{\"action\":999}");
session.flush(f2);
}
System.out.println(f.getRequestHeaders());
} else {
WebSocketFuture f = (WebSocketFuture) future;
System.out.println(f.getReadText());
}
}
};
ServerConfiguration configuration = new ServerConfiguration();
configuration.setSERVER_HOST("47.89.30.77");
// configuration.setSERVER_HOST("120.76.222.210");
// configuration.setSERVER_HOST("115.29.193.48");
// configuration.setSERVER_HOST("workerman.net");
configuration.setSERVER_PORT(7680);
// configuration.setSERVER_PORT(30005);
// configuration.setSERVER_PORT(29000);
// configuration.setSERVER_PORT(8280);
SocketChannelContext context = new NioSocketChannelContext(configuration);
SocketChannelConnector connector = new SocketChannelConnector(context);
context.setIoEventHandleAdaptor(eventHandleAdaptor);
context.setProtocolFactory(new ProtobaseProtocolFactory());
context.addSessionEventListener(new LoggerSocketSEListener());
connector.getContext().setBeatFutureFactory(new WebSocketBeatFutureFactory());
connector.getContext().setProtocolFactory(new ClientHTTPProtocolFactory());
connector.getContext().setSslContext(SSLUtil.initClient(true));
SocketSession session = connector.connect();
String url = "/web-socket-chat";
url = "/c1020";
HttpFuture future = new WebSocketUpgradeRequestFuture(session.getContext(), url);
// future.setRequestURL("ws://120.76.222.210:30005/");
// future.setResponseHeader("Host", "120.76.222.210:30005");
// future.setResponseHeader("Pragma", "no-cache");
// future.setResponseHeader("Cache-Control", "no-cache");
// future.setResponseHeader("User-Agent",
// "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36");
// future.setResponseHeader("Accept-Encoding", "gzip, deflate, sdch");
// future.setResponseHeader("Accept-Language", "zh-CN,zh;q=0.8");
// future.setRequestHeader("", "");
session.flush(future);
// ThreadUtil.sleep(1000);
// WebSocketReadFuture f2 = new WebSocketReadFutureImpl();
// f2.write("test");
// session.flush(f2);
ThreadUtil.sleep(999999999);
CloseUtil.close(connector);
}
use of com.generallycloud.baseio.component.SocketChannelContext in project baseio by generallycloud.
the class TestBrowser method main.
public static void main(String[] args) throws Exception {
String queueName = "qName";
SimpleIoEventHandle eventHandle = new SimpleIoEventHandle();
ServerConfiguration configuration = new ServerConfiguration(8300);
SocketChannelContext context = new NioSocketChannelContext(configuration);
SocketChannelConnector connector = new SocketChannelConnector(context);
context.setIoEventHandleAdaptor(eventHandle);
context.setProtocolFactory(new ParamedProtobaseProtocolFactory());
context.addSessionEventListener(new LoggerSocketSEListener());
FixedSession session = new FixedSession(connector.connect());
session.login("admin", "admin100");
MessageBrowser browser = new DefaultMessageBrowser(session);
Message message = browser.browser(queueName);
System.out.println("message:" + message);
int size = browser.size();
System.out.println("size:" + size);
boolean isOnline = browser.isOnline(queueName);
System.out.println("isOnline:" + isOnline);
connector.close();
}
use of com.generallycloud.baseio.component.SocketChannelContext in project baseio by generallycloud.
the class TestListener method main.
public static void main(String[] args) throws Exception {
SimpleIoEventHandle eventHandle = new SimpleIoEventHandle();
ServerConfiguration configuration = new ServerConfiguration(8300);
SocketChannelContext context = new NioSocketChannelContext(configuration);
SocketChannelConnector connector = new SocketChannelConnector(context);
context.setIoEventHandleAdaptor(eventHandle);
context.setProtocolFactory(new ParamedProtobaseProtocolFactory());
context.addSessionEventListener(new LoggerSocketSEListener());
FixedSession session = new FixedSession(connector.connect());
session.login("admin", "admin100");
MessageConsumer consumer = new DefaultMessageConsumer(session);
long old = System.currentTimeMillis();
consumer.receive(new OnMessage() {
@Override
public void onReceive(Message message) {
System.out.println(message);
}
});
System.out.println("Time:" + (System.currentTimeMillis() - old));
ThreadUtil.sleep(1500000);
CloseUtil.close(connector);
}
use of com.generallycloud.baseio.component.SocketChannelContext in project baseio by generallycloud.
the class TestListenerByteMessage method main.
public static void main(String[] args) throws Exception {
SimpleIoEventHandle eventHandle = new SimpleIoEventHandle();
SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration(8300));
SocketChannelConnector connector = new SocketChannelConnector(context);
context.setIoEventHandleAdaptor(eventHandle);
context.setProtocolFactory(new ProtobaseProtocolFactory());
context.addSessionEventListener(new LoggerSocketSEListener());
FixedSession session = new FixedSession(connector.connect());
session.login("admin", "admin100");
MessageConsumer consumer = new DefaultMessageConsumer(session);
final long old = System.currentTimeMillis();
consumer.receive(new OnMessage() {
@Override
public void onReceive(Message message) {
System.out.println(message);
if (message.getMsgType() == Message.TYPE_TEXT_BYTE) {
TextByteMessage _Message = (TextByteMessage) message;
System.out.println(new String(_Message.getByteArray(), Encoding.UTF8));
}
System.out.println("Time:" + (System.currentTimeMillis() - old));
}
});
ThreadUtil.sleep(30000000);
connector.close();
}
use of com.generallycloud.baseio.component.SocketChannelContext in project baseio by generallycloud.
the class TestListenerCallBack method main.
public static void main(String[] args) throws Exception {
LoggerFactory.configure();
SimpleIoEventHandle eventHandle = new SimpleIoEventHandle();
ServerConfiguration configuration = new ServerConfiguration(8300);
SocketChannelContext context = new NioSocketChannelContext(configuration);
SocketChannelConnector connector = new SocketChannelConnector(context);
context.setIoEventHandleAdaptor(eventHandle);
context.setProtocolFactory(new ProtobaseProtocolFactory());
context.addSessionEventListener(new LoggerSocketSEListener());
FixedSession session = new FixedSession(connector.connect());
session.login("admin", "admin100");
MessageConsumer consumer = new DefaultMessageConsumer(session);
consumer.receive(new OnMessage() {
@Override
public void onReceive(Message message) {
System.out.println(message);
}
});
ThreadUtil.sleep(1000);
CloseUtil.close(connector);
}
Aggregations