您的位置:首页 > 游戏 > 游戏 > 做广告牌子_网店排行榜前十名_十个有创意的线上活动_百度外包公司有哪些

做广告牌子_网店排行榜前十名_十个有创意的线上活动_百度外包公司有哪些

2024/12/22 23:02:34 来源:https://blog.csdn.net/guoruijun_2012_4/article/details/144486317  浏览:    关键词:做广告牌子_网店排行榜前十名_十个有创意的线上活动_百度外包公司有哪些
做广告牌子_网店排行榜前十名_十个有创意的线上活动_百度外包公司有哪些

客户端socket

package com.example.filedemo.controller;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.Base64;
import java.util.Timer;
import javax.sound.sampled.*;
public class AudioWebSocketClient extends WebSocketClient {public AudioWebSocketClient(URI serverUri) {super(serverUri);}@Overridepublic void onOpen(ServerHandshake handshakedata) {System.out.println("Connected to the server.");}@Overridepublic void onMessage(String message) {// 处理接收到的消息System.out.println("Received message: " + message);}@Overridepublic void onClose(int code, String reason, boolean remote) {System.out.println("Disconnected from server: " + reason);}@Overridepublic void onError(Exception ex) {ex.printStackTrace();}// 将 byte[] 转换为 Base64 编码的字符串public static String byteArrayToString(byte[] byteArray) {// 使用 Base64 编码将字节数组转换为字符串return Base64.getEncoder().encodeToString(byteArray);}// 从 Base64 字符串解码为 byte[]public static byte[] stringToByteArray(String base64String) {// 使用 Base64 解码字符串为字节数组return Base64.getDecoder().decode(base64String);}public static void main(String[] args) throws Exception {//URI serverUri = new URI("ws://yourserverip:9395/dotcwsasr?lanid=0&userid=1121212&token=1234567890&sid=sid1001");URI serverUri = new URI("ws://127.0.0.1:8888");AudioWebSocketClient client = new AudioWebSocketClient(serverUri);client.connectBlocking();//字符串socket请求//        while (true) {//            client.send("Hello, WebSocket server!@@@@@@@@@@@@@@@@@@@@@@@@@");//        }//声音流socket请求
//        File file = new File("D:\\work\\20241213\\3.wav");
//        AudioInputStream audioInputStream = null;
//        try {
//            audioInputStream = AudioSystem.getAudioInputStream(file);
//        } catch (UnsupportedAudioFileException e) {
//            throw new RuntimeException(e);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//        AudioFormat format = audioInputStream.getFormat();
//        int bufferSize = 3200;
//        byte[] buffer = new byte[bufferSize];
//        int bytesRead;
//        while (true) {
//            try {
//                if (!((bytesRead = audioInputStream.read(buffer)) != -1)) {
//                    break;
//                } else {
//                    byte[] smallerBuffer = new byte[bytesRead];
//                    System.arraycopy(buffer, 0, smallerBuffer, 0, bytesRead);
//                    buffer = smallerBuffer;
//                    // String str = new String(buffer);
//                    client.send(byteArrayToString(buffer));
//                    // client.send(buffer);
//                }
//            } catch (IOException e) {
//                throw new RuntimeException(e);
//            }
//        }
//        try {
//            audioInputStream.close();
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }//麦克风声音socket请求try {// 设置音频格式AudioFormat format = new AudioFormat(16000, 16, 1, true, false);DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);// 检查是否支持if (!AudioSystem.isLineSupported(info)) {System.out.println("Line not supported");return;}// 打开 TargetDataLineTargetDataLine line = (TargetDataLine) AudioSystem.getLine(info);line.open(format);// 启动采集line.start();// 创建缓冲区byte[] buffer = new byte[3200]; // 每次读取 3200 字节,等同于 1600 采样点(16K 采样率,16-bit 单声道)while (true) {// 读取音频数据到缓冲区int bytesRead = line.read(buffer, 0, buffer.length);if (bytesRead > 0) {byte[] smallerBuffer = new byte[3200];System.arraycopy(buffer, 0, smallerBuffer, 0, bytesRead);buffer = smallerBuffer;client.send(byteArrayToString(buffer));}}} catch (LineUnavailableException e) {e.printStackTrace();}}
}

服务端socket

package com.example.filedemo.controller;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import org.java_websocket.server.WebSocketServer;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Base64;
public class WebSocketForwardingServer extends WebSocketServer {private WebSocketClient remoteWebSocket;// 远程 WebSocket 地址private String remoteWsUrl = "ws://YOURSERVERIP:9395/dotcwsasr?lanid=0&userid=123322&token=1234567890&sid=sid1001";public WebSocketForwardingServer(int port) {super(new InetSocketAddress(port));}// 将 byte[] 转换为 Base64 编码的字符串public static String byteArrayToString(byte[] byteArray) {// 使用 Base64 编码将字节数组转换为字符串return Base64.getEncoder().encodeToString(byteArray);}// 从 Base64 字符串解码为 byte[]public static byte[] stringToByteArray(String base64String) {// 使用 Base64 解码字符串为字节数组return Base64.getDecoder().decode(base64String);}@Overridepublic void onOpen(WebSocket conn, ClientHandshake handshake) {System.out.println("Client connected: " + conn.getRemoteSocketAddress());// 建立连接到远程 WebSocketconnectToRemoteWebSocket(conn);}@Overridepublic void onClose(WebSocket conn, int code, String reason, boolean remote) {System.out.println("Client disconnected: " + conn.getRemoteSocketAddress());}// 用于控制连接过程// 需要从远程 WebSocket 获取数据的地址private static final String REMOTE_WEBSOCKET_URL = "ws://yourserverip:9395/dotcwsasr?lanid=0&userid=13606060253&token=1234567890&sid=sid1001";@Overridepublic void onMessage(WebSocket webSocket, String s) {remoteWebSocket.send(stringToByteArray(s));}@Overridepublic void onError(WebSocket conn, Exception ex) {ex.printStackTrace();}@Overridepublic void onStart() {System.out.println("onStart-------------------------- start  ");}// 连接到远程 WebSocket 服务private void connectToRemoteWebSocket(WebSocket clientWebSocket) {try {remoteWebSocket = new WebSocketClient(new URI(remoteWsUrl)) {private WebSocket[] connections;public WebSocket[] getConnections() {return connections;}@Overridepublic void onOpen(ServerHandshake handshakedata) {System.out.println("Connected to remote WebSocket server");}@Overridepublic void onMessage(String message) {clientWebSocket.send(message);}@Overridepublic void onClose(int code, String reason, boolean remote) {System.out.println("Remote WebSocket closed: " + reason);}@Overridepublic void onError(Exception ex) {ex.printStackTrace();}};remoteWebSocket.connectBlocking();} catch (URISyntaxException e) {e.printStackTrace();} catch (InterruptedException e) {throw new RuntimeException(e);}}static WebSocketForwardingServer server = new WebSocketForwardingServer(8888);public static void main(String[] args) {int port = 8888;  // 设置本地 WebSocket 服务器的端口server.start();System.out.println("Server started on port: " + port);}
}

POM

		<dependency><groupId>org.java-websocket</groupId><artifactId>Java-WebSocket</artifactId><version>1.3.5</version></dependency>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com