use of com.generallycloud.baseio.connector.SocketChannelConnector in project baseio by generallycloud.
the class SimpleTestFIxedLengthClientPush method main.
@SuppressWarnings("resource")
public static void main(String[] args) throws Exception {
IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {
@Override
public void accept(SocketSession session, Future future) throws Exception {
System.out.println(">msg from server: " + future.getReadText());
}
};
SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration("localhost", 18300));
SocketChannelConnector connector = new SocketChannelConnector(context);
context.setIoEventHandleAdaptor(eventHandleAdaptor);
context.addSessionEventListener(new LoggerSocketSEListener());
context.setProtocolFactory(new FixedLengthProtocolFactory());
SocketSession session = connector.connect();
ThreadUtil.execute(new Runnable() {
@Override
public void run() {
System.out.println("************************************************");
System.out.println("提示:");
System.out.println("list(获取所有客户端id)");
System.out.println("id(获取当前客户端id)");
System.out.println("push id msg(推送消息到)");
System.out.println("broadcast msg(广播消息)");
System.out.println("exit(退出客户端)");
System.out.println("仅用于演示,msg请勿包含空格");
System.out.println("************************************************");
Scanner scanner = new Scanner(System.in);
for (; ; ) {
System.out.println(">");
String line = scanner.nextLine();
if ("exit".equals(line)) {
CloseUtil.close(session);
break;
}
FixedLengthFuture future = new FixedLengthFutureImpl(context);
future.write(line);
session.flush(future);
}
}
});
}
use of com.generallycloud.baseio.connector.SocketChannelConnector in project baseio by generallycloud.
the class TestFIxedLengthClient method main.
public static void main(String[] args) throws Exception {
IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {
@Override
public void accept(SocketSession session, Future future) throws Exception {
System.out.println();
System.out.println("____________________" + future.getReadText());
System.out.println();
}
};
SslContext sslContext = SSLUtil.initClient(true);
SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration("localhost", 18300));
SocketChannelConnector connector = new SocketChannelConnector(context);
context.setIoEventHandleAdaptor(eventHandleAdaptor);
context.addSessionEventListener(new LoggerSocketSEListener());
// context.addSessionEventListener(new SessionActiveSEListener());
context.setBeatFutureFactory(new FLBeatFutureFactory());
context.setProtocolFactory(new FixedLengthProtocolFactory());
context.setSslContext(sslContext);
SocketSession session = connector.connect();
FixedLengthFuture future = new FixedLengthFutureImpl(context);
future.write("hello server!");
session.flush(future);
ThreadUtil.sleep(100);
CloseUtil.close(connector);
DebugUtil.debug("连接已关闭。。。");
}
use of com.generallycloud.baseio.connector.SocketChannelConnector in project baseio by generallycloud.
the class TestHeartBeat method main.
public static void main(String[] args) throws Exception {
DebugUtil.setEnableDebug(true);
IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {
@Override
public void accept(SocketSession session, Future future) throws Exception {
DebugUtil.debug("______________" + future.getReadText());
}
};
ServerConfiguration configuration = new ServerConfiguration(18300);
configuration.setSERVER_SESSION_IDLE_TIME(20);
SocketChannelContext context = new NioSocketChannelContext(configuration);
SocketChannelConnector connector = new SocketChannelConnector(context);
context.addSessionIdleEventListener(new SocketSessionActiveSEListener());
context.addSessionEventListener(new LoggerSocketSEListener());
context.setBeatFutureFactory(new FLBeatFutureFactory());
context.setProtocolFactory(new FixedLengthProtocolFactory());
context.setIoEventHandleAdaptor(eventHandleAdaptor);
SocketSession session = connector.connect();
String param = "tttt";
long old = System.currentTimeMillis();
for (int i = 0; i < 5; i++) {
Future future = new FixedLengthFutureImpl(context);
future.write(param);
session.flush(future);
ThreadUtil.sleep(300);
}
System.out.println("Time:" + (System.currentTimeMillis() - old));
Thread.sleep(2000);
CloseUtil.close(connector);
}
use of com.generallycloud.baseio.connector.SocketChannelConnector in project baseio by generallycloud.
the class TestSimpleHttpClient method main.
public static void main(String[] args) throws Exception {
HttpIOEventHandle eventHandleAdaptor = new HttpIOEventHandle();
// ServerConfiguration c = new ServerConfiguration("localhost",80);
ServerConfiguration c = new ServerConfiguration("generallycloud.com", 443);
SocketChannelContext context = new NioSocketChannelContext(c);
SocketChannelConnector connector = new SocketChannelConnector(context);
SslContext sslContext = SSLUtil.initClient(true);
context.setProtocolFactory(new ClientHTTPProtocolFactory());
context.setIoEventHandleAdaptor(eventHandleAdaptor);
context.addSessionEventListener(new LoggerSocketSEListener());
context.setSslContext(sslContext);
SocketSession session = connector.connect();
HttpClient client = new HttpClient(session);
HttpFuture future = new ClientHttpFuture(context, "/test-show-memory");
HttpFuture res = client.request(future, 10000);
System.out.println();
System.out.println(new String(res.getBodyContent()));
System.out.println();
CloseUtil.close(connector);
}
use of com.generallycloud.baseio.connector.SocketChannelConnector in project baseio by generallycloud.
the class SimpleTestProtobaseClient method main.
public static void main(String[] args) throws Exception {
IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {
@Override
public void accept(SocketSession session, Future future) throws Exception {
System.out.println();
System.out.println("____________________" + future.getReadText());
System.out.println();
}
};
SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration("localhost", 18300));
context.getServerConfiguration().setSERVER_ENABLE_MEMORY_POOL_DIRECT(true);
SocketChannelConnector connector = new SocketChannelConnector(context);
connector.setTimeout(99999999);
context.setIoEventHandleAdaptor(eventHandleAdaptor);
context.addSessionEventListener(new LoggerSocketSEListener());
context.setProtocolFactory(new ProtobaseProtocolFactory());
SocketSession session = connector.connect();
ProtobaseFuture future = new ProtobaseFutureImpl(context, "test222");
future.write("hello server!");
session.flush(future);
ThreadUtil.sleep(100);
CloseUtil.close(connector);
}
Aggregations