| 5 | /* 2021-2023 by zhaoming,mali aihealthx.com */ |
| 6 | |
| 7 | function WebSocketConnectMethod( config ) { //定义socket连接方法类 |
| 8 | |
| 9 | |
| 10 | var speechSokt; |
| 11 | var connKeeperID; |
| 12 | |
| 13 | var msgHandle = config.msgHandle; |
| 14 | var stateHandle = config.stateHandle; |
| 15 | |
| 16 | this.wsStart = function () { |
| 17 | var Uri = document.getElementById('wssip').value; //"wss://111.205.137.58:5821/wss/" //设置wss asr online接口地址 如 wss://X.X.X.X:port/wss/ |
| 18 | if(Uri.match(/wss:\S*|ws:\S*/)) |
| 19 | { |
| 20 | console.log("Uri"+Uri); |
| 21 | } |
| 22 | else |
| 23 | { |
| 24 | alert("请检查wss地址正确性"); |
| 25 | return 0; |
| 26 | } |
| 27 | |
| 28 | if ( 'WebSocket' in window ) { |
| 29 | speechSokt = new WebSocket( Uri ); // 定义socket连接对象 |
| 30 | speechSokt.onopen = function(e){onOpen(e);}; // 定义响应函数 |
| 31 | speechSokt.onclose = function(e){ |
| 32 | console.log("onclose ws!"); |
| 33 | //speechSokt.close(); |
| 34 | onClose(e); |
| 35 | }; |
| 36 | speechSokt.onmessage = function(e){onMessage(e);}; |
| 37 | speechSokt.onerror = function(e){onError(e);}; |
| 38 | return 1; |
| 39 | } |
| 40 | else { |
| 41 | alert('当前浏览器不支持 WebSocket'); |
| 42 | return 0; |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | // 定义停止与发送函数 |
| 47 | this.wsStop = function () { |
| 48 | if(speechSokt != undefined) { |
| 49 | console.log("stop ws!"); |
| 50 | speechSokt.close(); |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | this.wsSend = function ( oneData ) { |
| 55 | |
| 56 | if(speechSokt == undefined) return; |
| 57 | if ( speechSokt.readyState === 1 ) { // 0:CONNECTING, 1:OPEN, 2:CLOSING, 3:CLOSED |
| 58 | |
| 59 | speechSokt.send( oneData ); |
| 60 | |
| 61 | |
| 62 | } |
| 63 | }; |
| 64 | |