[*] 去掉rtp功能
This commit is contained in:
@@ -1,13 +0,0 @@
|
||||
package com.acgist.taoyao.main;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class TaoyaoApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.acgist.taoyao.signal;
|
||||
package com.acgist.taoyao.rtp;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
@@ -19,7 +19,7 @@ import com.acgist.taoyao.signal.utils.CipherUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class SocketSignalTest {
|
||||
public class RtpTest {
|
||||
|
||||
@Test
|
||||
void testSocket() throws Exception {
|
||||
@@ -1,48 +0,0 @@
|
||||
package com.acgist.taoyao.service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class Server {
|
||||
|
||||
public static final Executor EXECUTOR = Executors.newCachedThreadPool();
|
||||
|
||||
@Test
|
||||
public void testServer() throws Exception {
|
||||
final ServerSocket server = new ServerSocket(9999);
|
||||
while(!server.isClosed()) {
|
||||
final Socket accept = server.accept();
|
||||
EXECUTOR.execute(() -> {
|
||||
try {
|
||||
this.execute(accept);
|
||||
} catch (IOException e) {
|
||||
log.error("异常", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
server.close();
|
||||
}
|
||||
|
||||
public void execute(Socket accept) throws IOException {
|
||||
final InputStream inputStream = accept.getInputStream();
|
||||
final OutputStream outputStream = accept.getOutputStream();
|
||||
while(!accept.isClosed()) {
|
||||
final byte[] bytes = new byte[1024];
|
||||
final int length = inputStream.read(bytes);
|
||||
log.info("收到消息:{}", new String(bytes, 0, length));
|
||||
outputStream.write(bytes, 0, length);
|
||||
outputStream.flush();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package com.acgist.taoyao.signal;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import com.acgist.taoyao.annotation.TaoyaoTest;
|
||||
import com.acgist.taoyao.boot.model.MessageCodeException;
|
||||
import com.acgist.taoyao.main.TaoyaoApplication;
|
||||
import com.acgist.taoyao.signal.protocol.platform.PlatformErrorProtocol;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
@TaoyaoTest(classes = TaoyaoApplication.class)
|
||||
public class PlatformErrorProtocolTest {
|
||||
|
||||
@Autowired
|
||||
private PlatformErrorProtocol platformErrorProtocol;
|
||||
|
||||
@Test
|
||||
public void testException() {
|
||||
log.info("{}", this.platformErrorProtocol.build(MessageCodeException.of("自定义")));
|
||||
log.info("{}", this.platformErrorProtocol.build(new NullPointerException("空指针")));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
package com.acgist.taoyao.signal;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.WebSocket;
|
||||
import java.net.http.WebSocket.Listener;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class WebSocketClient {
|
||||
|
||||
public static final WebSocket build(String uri, String clientId) throws InterruptedException {
|
||||
return build(uri, clientId, null);
|
||||
}
|
||||
|
||||
public static final WebSocket build(String uri, String clientId, CountDownLatch count) throws InterruptedException {
|
||||
final Object lock = new Object();
|
||||
try {
|
||||
return HttpClient
|
||||
.newBuilder()
|
||||
.sslContext(newSSLContext())
|
||||
.build()
|
||||
.newWebSocketBuilder()
|
||||
.buildAsync(URI.create(uri), new Listener() {
|
||||
@Override
|
||||
public void onOpen(WebSocket webSocket) {
|
||||
webSocket.sendText(String.format("""
|
||||
{"header":{"signal":"client::register","v":"1.0.0","id":"1"},"body":{"username":"taoyao","password":"taoyao","ip":"127.0.0.1","clientId":"%s"}}
|
||||
""", clientId), true);
|
||||
Listener.super.onOpen(webSocket);
|
||||
}
|
||||
@Override
|
||||
public CompletionStage<?> onText(WebSocket webSocket, CharSequence data, boolean last) {
|
||||
synchronized (lock) {
|
||||
lock.notifyAll();
|
||||
}
|
||||
if(count == null) {
|
||||
log.debug("收到WebSocket消息:{}", data);
|
||||
} else {
|
||||
count.countDown();
|
||||
log.debug("收到WebSocket消息:{}-{}", count.getCount(), data);
|
||||
}
|
||||
return Listener.super.onText(webSocket, data, last);
|
||||
}
|
||||
})
|
||||
.join();
|
||||
} finally {
|
||||
synchronized (lock) {
|
||||
lock.wait(1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static final SSLContext newSSLContext() {
|
||||
SSLContext sslContext = null;
|
||||
try {
|
||||
// SSL协议:SSL、SSLv2、SSLv3、TLS、TLSv1、TLSv1.1、TLSv1.2、TLSv1.3
|
||||
sslContext = SSLContext.getInstance("TLSv1.2");
|
||||
sslContext.init(null, TRUST_ALL_CERT_MANAGER, new SecureRandom());
|
||||
} catch (KeyManagementException | NoSuchAlgorithmException e) {
|
||||
log.error("新建SSLContext异常", e);
|
||||
try {
|
||||
sslContext = SSLContext.getDefault();
|
||||
} catch (NoSuchAlgorithmException ex) {
|
||||
log.error("新建默认SSLContext异常", ex);
|
||||
}
|
||||
}
|
||||
return sslContext;
|
||||
}
|
||||
|
||||
private static final TrustManager[] TRUST_ALL_CERT_MANAGER = new TrustManager[] {
|
||||
new X509TrustManager() {
|
||||
@Override
|
||||
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
||||
}
|
||||
@Override
|
||||
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
||||
}
|
||||
@Override
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
package com.acgist.taoyao.signal;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.net.http.WebSocket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.acgist.taoyao.annotation.CostedTest;
|
||||
import com.acgist.taoyao.annotation.TaoyaoTest;
|
||||
import com.acgist.taoyao.main.TaoyaoApplication;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
@TaoyaoTest(classes = TaoyaoApplication.class)
|
||||
class WebSocketSignalTest {
|
||||
|
||||
/**
|
||||
* 防止GC
|
||||
*/
|
||||
private List<WebSocket> list = new ArrayList<>();
|
||||
|
||||
@Test
|
||||
void testSignal() throws InterruptedException {
|
||||
final WebSocket clientA = WebSocketClient.build("wss://localhost:8888/websocket.signal", "clientA");
|
||||
final WebSocket clientB = WebSocketClient.build("wss://localhost:8888/websocket.signal", "clientB");
|
||||
clientA.sendText("""
|
||||
{"header":{"signal":"client::heartbeat","v":"1.0.0","id":"1"},"body":{}}
|
||||
""", true).join();
|
||||
assertNotNull(clientA);
|
||||
assertNotNull(clientB);
|
||||
}
|
||||
|
||||
@Test
|
||||
@CostedTest(thread = 10, count = 100, waitRelease = 5000L)
|
||||
void testThread() throws InterruptedException {
|
||||
final int total = 100;
|
||||
final CountDownLatch count = new CountDownLatch(total);
|
||||
final WebSocket clientA = WebSocketClient.build("wss://localhost:8888/websocket.signal", "clientA", count);
|
||||
final long aTime = System.currentTimeMillis();
|
||||
for (int index = 0; index < total; index++) {
|
||||
clientA.sendText("""
|
||||
{"header":{"signal":"client::status","v":"1.0.0","id":"1"},"body":{}}
|
||||
""", true).join();
|
||||
}
|
||||
this.list.add(clientA);
|
||||
// final ExecutorService executor = Executors.newFixedThreadPool(10);
|
||||
// for (int index = 0; index < total; index++) {
|
||||
// executor.execute(() -> {
|
||||
// synchronized (clientA) {
|
||||
// clientA.sendText("""
|
||||
// {"header":{"signal":"client::status","v":"1.0.0","id":"1"},"body":{}}
|
||||
// """, true).join();
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
count.await();
|
||||
final long zTime = System.currentTimeMillis();
|
||||
log.info("执行时间:{}", zTime - aTime);
|
||||
log.info("当前连接数量:{}", this.list.size());
|
||||
assertNotNull(clientA);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMax() throws InterruptedException {
|
||||
final int size = 1024;
|
||||
final CountDownLatch count = new CountDownLatch(size);
|
||||
for (int index = 0; index < size; index++) {
|
||||
final WebSocket clientA = WebSocketClient.build("wss://localhost:8888/websocket.signal", "clientA", count);
|
||||
assertNotNull(clientA);
|
||||
assertTrue(!(clientA.isInputClosed() || clientA.isOutputClosed()));
|
||||
}
|
||||
count.await();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user