import { VideoWS } from '../ws.ts';
import net from 'net';
import { Recording } from '../../../../recorder/index.ts';
import Stream from 'stream';

const recorder = new Recording();
const writeStream = new Stream.Writable();
const url = 'wss://funasr.xiongxiao.me';
const ws = new VideoWS({
  // url: 'wss://192.168.31.220:10095',
  url: url,
  isFile: false,
  mode: '2pass',
  wsOptions: {
    rejectUnauthorized: false,
  },
  onConnect: async () => {
    console.log('onConnect');
    recorder.start();
    let chunks: Buffer = Buffer.alloc(0);
    var chunk_size = 960; // for asr chunk_size [5, 10, 5]
    let totalsend = 0;
    let len = 0;
    recorder.stream().on('data', (chunk) => {
      // chunks = Buffer.concat([chunks, chunk]);
      // if (chunks.length > chunk_size) {
      //   ws.send(chunks);
      //   console.log('chunk', chunk.length);

      //   totalsend += chunks.length;
      //   chunks = Buffer.alloc(0);
      // }
      ws.send(chunk);
    });
    ws.start();
    setTimeout(() => {
      ws.stop();
      setTimeout(() => {
        process.exit(0);
      }, 1000);
      console.log('len', len);
    }, 10 * 30 * 1000);
    // }, 5 * 1000);
    ws.emitter.on('message', (event) => {
      // console.log('message', event.data);
    });
  },
});

const server = net.createServer((socket) => {
  socket.on('data', (data) => {
    console.log('data', data);
  });
});
server.listen(10097);
