From 00de4511009a7fbbb88142b2b2a0faeaa0e3e7d9 Mon Sep 17 00:00:00 2001
From: zhaomingwork <zhaomingwork@qq.com>
Date: 星期一, 15 五月 2023 22:05:26 +0800
Subject: [PATCH] add ws_server_online support wss

---
 funasr/runtime/html5/static/wsconnecter.js          |    1 +
 funasr/runtime/html5/readme.md                      |   22 +++++++++++++---------
 funasr/runtime/python/websocket/ws_server_online.py |   13 +++++++++++--
 funasr/runtime/html5/static/main.js                 |    1 +
 4 files changed, 26 insertions(+), 11 deletions(-)

diff --git a/funasr/runtime/html5/readme.md b/funasr/runtime/html5/readme.md
index 1c30cd3..d52ace5 100644
--- a/funasr/runtime/html5/readme.md
+++ b/funasr/runtime/html5/readme.md
@@ -17,9 +17,15 @@
 ### demo椤甸潰濡備笅
 ![img](https://github.com/alibaba-damo-academy/FunASR/blob/for-html5-demo/funasr/runtime/html5/demo.gif)
 
-## 鍏蜂綋鏁版嵁娴佸悜锛�
-娴忚鍣╤ttps楹﹀厠椋� --> html5 demo鏈嶅姟 --> js wss鎺ュ彛 --> nginx鏈嶅姟 --> ws asr online srv
-
+## 涓ょws_server_online杩炴帴妯″紡
+### 1)鐩存帴杩炴帴妯″紡锛屾祻瑙堝櫒https楹﹀厠椋� --> html5 demo鏈嶅姟 --> js wss鎺ュ彛 --> wss asr online srv(璇佷功鐢熸垚璇峰線鍚庣湅)
+```shell
+python ws_server_online.py --certfile server.crt --keyfile server.key  --port 5921
+```
+### 2)nginx涓浆锛屾祻瑙堝櫒https楹﹀厠椋� --> html5 demo鏈嶅姟 --> js wss鎺ュ彛 --> nginx鏈嶅姟 --> ws asr online srv
+```shell
+python ws_server_online.py  --port 5921
+```
 ## 1.html5 demo鏈嶅姟鍚姩
 ### 鍚姩html5鏈嶅姟锛岄渶瑕乻sl璇佷功(鑷繁鐢熸垚璇峰線鍚庣湅)
 
@@ -28,17 +34,15 @@
                    [--keyfile KEYFILE]
 python h5Server.py --port 1337
 ```
-## 2.鍚姩ws asr online srv
+## 2.鍚姩ws or wss asr online srv
 [鍏蜂綋璇风湅online asr](https://github.com/alibaba-damo-academy/FunASR/tree/main/funasr/runtime/python/websocket)
-鐩墠online asr鍙彁渚泈s鎺ュ彛锛岄渶瑕侀�氳繃nginx灏唚ss杞彂鍒拌online asr绔彛涓�
+online asr鍙彁渚涗袱绉峸s鍜寃ss妯″紡锛寃ss妯″紡鍙互鐩存帴鍚姩锛屾棤闇�nginx涓浆銆傚惁鍒欓渶瑕侀�氳繃nginx灏唚ss杞彂鍒拌online asr鐨剋s绔彛涓�
 
 ## 3.淇敼wsconnecter.js閲宎sr鎺ュ彛鍦板潃
-wsconnecter.js閲岄厤缃畂nline asr鏈嶅姟鍦板潃璺緞锛岃繖閲岄厤缃殑鏄痭ginx鐨剋ss绔彛
+wsconnecter.js閲岄厤缃畂nline asr鏈嶅姟鍦板潃璺緞锛岃繖閲岄厤缃殑鏄痺ss绔彛
 var Uri = "wss://xxx:xxx/" 
 
-## 4.閰嶇疆nginx骞跺惎鍔�
-
-## 5.娴忚鍣ㄦ墦寮�鍦板潃娴嬭瘯
+## 4.娴忚鍣ㄦ墦寮�鍦板潃娴嬭瘯
 https://127.0.0.1:1337/static/index.html
 
 
diff --git a/funasr/runtime/html5/static/main.js b/funasr/runtime/html5/static/main.js
index 7066582..5f6bb10 100644
--- a/funasr/runtime/html5/static/main.js
+++ b/funasr/runtime/html5/static/main.js
@@ -59,6 +59,7 @@
 	} else if ( connState === 2 ) {
 		stop();
 		console.log( 'connecttion error' );
+		setTimeout(function(){btnStart.disabled = true;info_div.innerHTML='connecttion error';}, 4000 );
 	}
 }
 
diff --git a/funasr/runtime/html5/static/wsconnecter.js b/funasr/runtime/html5/static/wsconnecter.js
index a4c2f22..4b22e8f 100644
--- a/funasr/runtime/html5/static/wsconnecter.js
+++ b/funasr/runtime/html5/static/wsconnecter.js
@@ -72,6 +72,7 @@
 	}
 	
 	function onError( e ) {
+		info_div.innerHTML="杩炴帴"+e;
 		console.log(e);
 		stateHandle(2);
 	}
diff --git a/funasr/runtime/python/websocket/ws_server_online.py b/funasr/runtime/python/websocket/ws_server_online.py
index a35b127..ba50f62 100644
--- a/funasr/runtime/python/websocket/ws_server_online.py
+++ b/funasr/runtime/python/websocket/ws_server_online.py
@@ -7,7 +7,7 @@
 import logging
 import tracemalloc
 import numpy as np
-
+import ssl
 from parse_args import args
 from modelscope.pipelines import pipeline
 from modelscope.utils.constant import Tasks
@@ -94,7 +94,16 @@
 				await websocket.send(message)
 
 
+if len(args.certfile)>0:
+  ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
 
-start_server = websockets.serve(ws_serve, args.host, args.port, subprotocols=["binary"], ping_interval=None)
+  # Generate with Lets Encrypt, copied to this location, chown to current user and 400 permissions
+  ssl_cert = args.certfile
+  ssl_key = args.keyfile
+
+  ssl_context.load_cert_chain(ssl_cert, keyfile=ssl_key)
+  start_server = websockets.serve(ws_serve, args.host, args.port, subprotocols=["binary"], ping_interval=None,ssl=ssl_context)
+else:
+  start_server = websockets.serve(ws_serve, args.host, args.port, subprotocols=["binary"], ping_interval=None)
 asyncio.get_event_loop().run_until_complete(start_server)
 asyncio.get_event_loop().run_forever()
\ No newline at end of file

--
Gitblit v1.9.1