import { VideoWS } from '../ws.ts';
import net from 'net';
import path from 'path';
import fs from 'fs';

const videoTestPath = path.join(process.cwd(), 'videos/asr_example.wav');
const ws = new VideoWS({
  // url: 'wss://192.168.31.220:10095',
  url: 'wss://funasr.xiongxiao.me',
  isFile: true,
  onConnect: async () => {
    console.log('onConnect');
    const data = fs.readFileSync(videoTestPath);
    let sampleBuf = new Uint8Array(data);

    var chunk_size = 960; // for asr chunk_size [5, 10, 5]
    let totalsend = 0;
    let len = 0;
    ws.start();
    while (sampleBuf.length >= chunk_size) {
      const sendBuf = sampleBuf.slice(0, chunk_size);
      totalsend = totalsend + sampleBuf.length;
      sampleBuf = sampleBuf.slice(chunk_size, sampleBuf.length);
      if (len === 100) {
        // ws.stop();
        // ws.start();
        await new Promise((resolve) => setTimeout(resolve, 1000));
      }
      ws.send(sendBuf);
      len++;
    }
    ws.stop();
    console.log('len', len);
  },
});

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