
export function connectWebSocket() {
	const websocketUrl = getWebSocketUrl();
	if ("WebSocket" in window) {
		iatWS = new WebSocket(websocketUrl);
	} else if ("MozWebSocket" in window) {
		iatWS = new MozWebSocket(websocketUrl);
	} else {
		alert("浏览器不支持WebSocket");
		return;
	}
	changeBtnStatus("CONNECTING");
	iatWS.onopen = (e) => {
		// 开始录音
		recorder.start({
			sampleRate: 16000,
			frameSize: 1280,
		});
		var params = {
			common: {
				app_id: APPID,
			},
			business: {
				language: "zh_cn",
				domain: "iat",
				accent: "mandarin",
				vad_eos: 5000,
				dwa: "wpgs",
			},
			data: {
				status: 0,
				format: "audio/L16;rate=16000",
				encoding: "raw",
			},
		};
		iatWS.send(JSON.stringify(params));
	};
	iatWS.onmessage = (e) => {
		renderResult(e.data);
	};
	iatWS.onerror = (e) => {
		console.error(e);
		recorder.stop();
		changeBtnStatus("CLOSED");
	};
	iatWS.onclose = (e) => {
		recorder.stop();
		changeBtnStatus("CLOSED");
	};
}
