use of io.transport.sdk.TransportClient in project transporter by wang4ever.
the class TransportProgramTools method execute.
/**
* 执行并发请求 <br/>
* 启动一个线程推送消息.
*
* @param hostAndPort
* 服务器地址
* @param tName
* 并发线程名
* @param count
* 每个线程发送消息数
* @throws Exception
*/
private static void execute(String hostAndPort, String tName, int count) throws Exception {
// initialize.
Configuration config = new Configuration(appId, appSecret, groupId, DeviceType.FRONTEND, MyReceiveTextHandler.class);
config.setHostAndPorts(hostAndPort);
// Maximum can only be 32 byte.
config.setDeviceId(UUID.randomUUID().toString().replaceAll("-", ""));
// Join to server.
TransportClient client = new TransportClient(config).join();
// build.destroy(); // destroy instance.
// For test the registered WS client.
// System.out.println("准备注册WS连接设备... threadName=" + tName);
// client.registered(24 * 60 * 60, "111111111122222222223333333333ab");
// Definition message.
String payload = "{\"testKey\":\"test1\", \"TestData\":\"发送给浏览器的测试JSON报文ABCDEFG123456\"}";
int len = payload.getBytes(CharsetUtil.UTF_8).length;
// Loop sending.
for (int c = 0; c < count; c++) {
// client.unicast(toDeviceId, payload);
System.out.println("准备发送测试报文至浏览器 i=" + c + ", threadName=" + tName + ", len=" + len);
client.groupcast(toGroupId, payload);
// Random wait.
Thread.sleep(random.nextInt(1000));
}
System.out.println("测试执行结束. threadName=" + tName);
Thread.sleep(1000L);
System.exit(0);
}
Aggregations