org.tio.client.ClientChannelContext Java Examples

The following examples show how to use org.tio.client.ClientChannelContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClientStarter.java    From md_blockchain with Apache License 2.0 6 votes vote down vote up
/**
 * client在此绑定多个服务器,多个服务器为一个group,将来发消息时发给一个group
 */
private void bindServerGroup(Node serverNode) {
    try {
        AioClient aioClient = new AioClient(clientGroupContext);
        logger.info("开始绑定" + ":" + serverNode.toString());
        ClientChannelContext clientChannelContext = aioClient.connect(serverNode, 1);
        if (clientChannelContext == null) {
            logger.info("绑定" + serverNode.toString() + "失败");
            return;
        }
        if (Aio.isInGroup(GROUP_NAME, clientChannelContext)) {
            return;
        }
        //绑group是将要连接的各个服务器节点做为一个group
        Aio.bindGroup(clientChannelContext, GROUP_NAME);
    } catch (Exception e) {
        logger.info("异常");
    }

}
 
Example #2
Source File: AppClient.java    From blockchain with Apache License 2.0 5 votes vote down vote up
/**
 * 连接节点
 * @param node
 * @throws Exception
 */
public void connectNode(Node node) throws Exception
{
	ClientChannelContext channelContext = aioClient.connect(node);
	Aio.send(channelContext, new MessagePacket(SerializeUtils.serialize(MessagePacket.HELLO_MESSAGE)));
	Aio.bindGroup(channelContext, tioProps.getClientGroupName());
	logger.info("连接节点成功, {}", node);
}
 
Example #3
Source File: HttpClientStarter.java    From t-io with Apache License 2.0 5 votes vote down vote up
public static void gsOsc() throws Exception {
	boolean useSsl = false;
	String serverip = "www.baidu.com";
	int serverport = 80;
	String path = "/";
	//		String requestPath = "/json";
	String queryString = "";

	HttpClientStarter httpClientStarter;
	if (useSsl) {
		httpClientStarter = org.tio.http.client.HttpClientStarter.httpsClientStarter;
	} else {
		httpClientStarter = org.tio.http.client.HttpClientStarter.httpClientStarter;
	}

	Node serverNode = new Node(serverip, serverport);
	clientChannelContextArray = new ClientChannelContext[clientCount];
	for (int i = 0; i < clientCount; i++) {
		clientChannelContextArray[i] = httpClientStarter.tioClient.connect(serverNode);
	}

	requestCount = 10000; //每个客户端的请求数
	clientCount = 100; //客户端个数
	totalRequestCount = requestCount * clientCount; //总请求数
	stepCount = totalRequestCount / 10;
	startTime = System.currentTimeMillis();
	stageStartTime = System.currentTimeMillis();

	//received
	receivedCount = new AtomicLong();
	receivedStageCount = new AtomicLong();

	System.out.println("start time:" + startTime + "(" + DateUtil.formatDateTime(new Date(startTime)) + ")");
	ClientHttpRequest clientHttpRequest = ClientHttpRequest.get(path, queryString);
	for (int i = 0; i < clientCount; i++) {
		for (int j = 0; j < requestCount; j++) {
			Tio.send(clientChannelContextArray[i], clientHttpRequest);
		}
	}
}
 
Example #4
Source File: WsClient.java    From t-io with Apache License 2.0 4 votes vote down vote up
public ClientChannelContext getClientChannelContext() {
  return clientChannelContext;
}
 
Example #5
Source File: WebSocketImpl.java    From t-io with Apache License 2.0 4 votes vote down vote up
private void handshake() {
  readyState = WebSocket.CONNECTING;

  ClientChannelContext ctx = wsClient.getClientChannelContext();
  WsSessionContext session = (WsSessionContext) ctx.get();

  session.setHandshaked(false);

  String path = wsClient.uri.getPath();
  if (StrUtil.isBlank(path)) {
    path = "/";
  }
  ClientHttpRequest httpRequest =
      new ClientHttpRequest(Method.GET, path, wsClient.uri.getRawQuery());
  Map<String, String> headers = new HashMap<>();
  if (additionalHttpHeaders != null) headers.putAll(additionalHttpHeaders);
  headers.put("Host", wsClient.uri.getHost() + ":" + wsClient.uri.getPort());
  headers.put("Upgrade", "websocket");
  headers.put("Connection", "Upgrade");
  headers.put("Sec-WebSocket-Key", getSecWebsocketKey());
  headers.put("Sec-WebSocket-Version", "13");
  httpRequest.setHeaders(headers);

  session.setHandshakeRequest(httpRequest);

  ObjKit.Box<Disposable> disposableBox = ObjKit.box(null);

  disposableBox.value =
      publisher
          .filter(packet -> !session.isHandshaked())
          .subscribe(
              packet -> {
                if (packet instanceof HttpResponse) {
                  HttpResponse resp = (HttpResponse) packet;

                  if (resp.getStatus() == HttpResponseStatus.C101) {
                    HeaderValue upgrade = resp.getHeader(HeaderName.Upgrade);
                    if (upgrade == null || !upgrade.value.toLowerCase().equals("websocket")) {
                      close(1002, "no upgrade or upgrade invalid");
                      return;
                    }
                    HeaderValue connection = resp.getHeader(HeaderName.Connection);
                    if (connection == null || !connection.value.toLowerCase().equals("upgrade")) {
                      close(1002, "no connection or connection invalid");
                      return;
                    }
                    HeaderValue secWebsocketAccept =
                        resp.getHeader(HeaderName.Sec_WebSocket_Accept);
                    if (secWebsocketAccept == null
                        || !verifySecWebsocketAccept(secWebsocketAccept.value)) {
                      close(1002, "no Sec_WebSocket_Accept or Sec_WebSocket_Accept invalid");
                      return;
                    }
                    // TODO: Sec-WebSocket-Extensions, Sec-WebSocket-Protocol

                    readyState = WebSocket.OPEN;
                    session.setHandshaked(true);
                    onOpen();
                  } else {
                    // TODO: support other http code
                    close(1002, "not support http code: " + resp.getStatus().status);
                    return;
                  }

                  disposableBox.value.dispose();
                }
              });

  Tio.send(ctx, httpRequest);
}
 
Example #6
Source File: HttpClientStarter.java    From t-io with Apache License 2.0 4 votes vote down vote up
/**
 * 开始测试
 * @throws Exception
 * @author tanyaowu
 */
public static void startTest(String serverip, int serverport, String path, int clientCount, int requestCount) throws Exception {
	boolean useSsl = false;
	String queryString = "";

	HttpClientStarter httpClientStarter;
	if (useSsl) {
		httpClientStarter = httpsClientStarter;
	} else {
		httpClientStarter = org.tio.http.client.HttpClientStarter.httpClientStarter;
	}

	HttpClientStarter.clientCount = clientCount; //客户端个数
	HttpClientStarter.requestCount = requestCount; //每个客户端的请求数
	HttpClientStarter.requestPath = path;

	HttpClientStarter.totalRequestCount = HttpClientStarter.requestCount * HttpClientStarter.clientCount; //总请求数
	HttpClientStarter.stepCount = HttpClientStarter.totalRequestCount / 10;

	Node serverNode = new Node(serverip, serverport);
	clientChannelContextArray = new ClientChannelContext[clientCount];
	for (int i = 0; i < clientCount; i++) {
		clientChannelContextArray[i] = httpClientStarter.tioClient.connect(serverNode);
	}

	startTime = System.currentTimeMillis();
	stageStartTime = System.currentTimeMillis();

	//received
	receivedCount = new AtomicLong();
	receivedStageCount = new AtomicLong();

	receivedBytes = new AtomicLong();
	receivedStageBytes = new AtomicLong();

	System.out.println("start time:" + startTime + "(" + DateUtil.formatDateTime(new Date(startTime)) + ")");
	ClientHttpRequest clientHttpRequest = ClientHttpRequest.get(requestPath, queryString);
	for (int i = 0; i < clientCount; i++) {
		for (int j = 0; j < requestCount; j++) {
			clientHttpRequest.addHeader(HttpConst.RequestHeaderKey.Host, clientChannelContextArray[i].getServerNode().getIp());
			Tio.send(clientChannelContextArray[i], clientHttpRequest);
		}
	}
}