asd
This commit is contained in:
		
							
								
								
									
										45
									
								
								server.py
									
									
									
									
									
								
							
							
						
						
									
										45
									
								
								server.py
									
									
									
									
									
								
							| @ -7,21 +7,38 @@ from websockets.sync.server import serve | |||||||
| import websockets | import websockets | ||||||
| import array | import array | ||||||
| import time | import time | ||||||
|  | from flask import render_template  | ||||||
|  |  | ||||||
| api = Flask(__name__) | api = Flask(__name__) | ||||||
| watchers_last_seen = {} | watchers_last_seen = {} | ||||||
|  | web_users = {} | ||||||
| stream = False | stream = False | ||||||
|  | freeze = False | ||||||
| debug = False | debug = False | ||||||
|  |  | ||||||
| def current_time(): | def current_time(): | ||||||
|     return str(int(time.time())) |     return str(int(time.time())) | ||||||
|  |  | ||||||
|  | @api.route("/", methods=["GET"]) | ||||||
|  | def web(): | ||||||
|  |     return render_template('index.html') | ||||||
|  |  | ||||||
| @api.route("/off", methods=["GET"]) | @api.route("/off", methods=["GET"]) | ||||||
| def streaOff(): | def streaOff(): | ||||||
|     global stream |     global stream | ||||||
|     stream=False |     stream=False | ||||||
|     return "True" |     return "True" | ||||||
|  |  | ||||||
|  | @api.route("/freeze", methods=["GET"]) | ||||||
|  | def freeze_the_stream(): | ||||||
|  |     global freeze | ||||||
|  |     if freeze == True: | ||||||
|  |         freeze=False | ||||||
|  |         return "un-frosen" | ||||||
|  |     else: | ||||||
|  |         freeze=True | ||||||
|  |         return "frosen" | ||||||
|  |  | ||||||
| @api.route("/on", methods=["GET"]) | @api.route("/on", methods=["GET"]) | ||||||
| def streamOn(): | def streamOn(): | ||||||
|     global stream |     global stream | ||||||
| @ -29,15 +46,14 @@ def streamOn(): | |||||||
|     return "True" |     return "True" | ||||||
|  |  | ||||||
| @api.route("/views", methods=["GET"]) | @api.route("/views", methods=["GET"]) | ||||||
| def views(): | def return_views(): | ||||||
|     global watchers_last_seen |     global watchers_last_seen | ||||||
|     return str(len(watchers_last_seen)) |     return str(len(watchers_last_seen.keys())) | ||||||
|  |  | ||||||
| @api.route("/mode", methods=["GET"]) | @api.route("/mode", methods=["GET"]) | ||||||
| def mode(): | def mode(): | ||||||
|     global watchers_last_seen |     global watchers_last_seen | ||||||
|     return str(stream) |     return "streaming-"+str(stream)+"\n frozen-"+str(freeze) | ||||||
|  |  | ||||||
|  |  | ||||||
| def ws_server(websocket): | def ws_server(websocket): | ||||||
|     global stream, watchers_last_seen |     global stream, watchers_last_seen | ||||||
| @ -52,11 +68,16 @@ def ws_server(websocket): | |||||||
|             if debug: print(req,"-----",args) |             if debug: print(req,"-----",args) | ||||||
|             match (req): |             match (req): | ||||||
|                 case ("get_stream"): |                 case ("get_stream"): | ||||||
|                     if stream: |                     if freeze == True: | ||||||
|                         watchers_last_seen[args["uuid"]] = current_time() |                         websocket.send(f"freeze") | ||||||
|                         websocket.send(f""+str(json.dumps([True, screenlive.gen()]))) |  | ||||||
|                     else: |                     else: | ||||||
|                         websocket.send(f"null") |                         if stream: | ||||||
|  |                             watchers_last_seen[args["uuid"]] = current_time() | ||||||
|  |                             websocket.send(f""+str(json.dumps([True, screenlive.gen()]))) | ||||||
|  |                         else: | ||||||
|  |                             websocket.send(f"null") | ||||||
|  |                 case ("web-site-user"): | ||||||
|  |                     users[args["uuid"]] = current_time() | ||||||
|                 case (_): |                 case (_): | ||||||
|                     websocket.send(f"null") |                     websocket.send(f"null") | ||||||
|     except websockets.exceptions.ConnectionClosedOK: |     except websockets.exceptions.ConnectionClosedOK: | ||||||
| @ -76,8 +97,14 @@ def timeout_task(): | |||||||
|                 if debug: print(key+"is timed out") |                 if debug: print(key+"is timed out") | ||||||
|                 watchers_last_seen.pop(key) |                 watchers_last_seen.pop(key) | ||||||
|                 break |                 break | ||||||
|  |         for key,value in users.items(): | ||||||
|  |             if debug: print(key+"||||"+value) | ||||||
|  |             if (int(current_time())-int(value)) > 5: | ||||||
|  |                 if debug: print(key+"is timed out") | ||||||
|  |                 users.pop(key) | ||||||
|  |                 break | ||||||
|         if debug: print("time -",current_time()) |         if debug: print("time -",current_time()) | ||||||
|         if debug: print("watchers -",watchers_last_seen.keys()) |         if debug: print("watchers -",watchers_last_seen.keys(),"::::: users - ",users.keys()) | ||||||
|         sleep(10) |         sleep(10) | ||||||
|  |  | ||||||
| def run_flask_server(): | def run_flask_server(): | ||||||
|  | |||||||
| @ -99,7 +99,7 @@ | |||||||
|             con() |             con() | ||||||
| 
 | 
 | ||||||
|             function con(){ |             function con(){ | ||||||
|                 ws = new WebSocket('wss://ws.n0r.su/'); |                 ws = new WebSocket('wss://n0r.su/screen_share/ws'); | ||||||
|                 ws.onopen = () => { |                 ws.onopen = () => { | ||||||
|                     console.log('websocket success---'); |                     console.log('websocket success---'); | ||||||
|                     get() |                     get() | ||||||
| @ -109,7 +109,7 @@ | |||||||
|                     setTimeout(()=>{con()},5000) |                     setTimeout(()=>{con()},5000) | ||||||
|                 }; |                 }; | ||||||
|                 ws.onerror = (e) => { |                 ws.onerror = (e) => { | ||||||
|                     setTimeout(()=>{con()},5000) |                     // setTimeout(()=>{con()},5000) | ||||||
|                     no_stream(); |                     no_stream(); | ||||||
|                     // console.error('websocket fail'); |                     // console.error('websocket fail'); | ||||||
|                 } |                 } | ||||||
| @ -120,23 +120,29 @@ | |||||||
|                 //     get(); |                 //     get(); | ||||||
|                 // }, 100); |                 // }, 100); | ||||||
|                 ws.onmessage = (message) => { |                 ws.onmessage = (message) => { | ||||||
|                     if (message.data != "null"){ |                     if (message.data == "freeze"){ | ||||||
|                         was_connect = true;                     |                         no_stream(true); | ||||||
|                         let data = message.data; |                         setTimeout(()=>{get();},2000) | ||||||
|                         // console.log('get websocket message---', JSON.parse(data)[1]); |  | ||||||
|                         document.body.style.backgroundImage = `url(data:image/jpeg;base64,${JSON.parse(data)[1]})`; |  | ||||||
|                         document.getElementById("top_bar").setAttribute("hidden",""); |  | ||||||
|                         status("conected") |  | ||||||
|                         setTimeout(()=>{get()},200) |  | ||||||
|                     } |                     } | ||||||
|                     else{ |                     else{ | ||||||
|                         setTimeout(()=>{get();},5000) |                         if (message.data != "null"){ | ||||||
|                         no_stream(); |                             was_connect = true;                     | ||||||
|  |                             let data = message.data; | ||||||
|  |                             // console.log('get websocket message---', JSON.parse(data)[1]); | ||||||
|  |                             document.body.style.backgroundImage = `url(data:image/jpeg;base64,${JSON.parse(data)[1]})`; | ||||||
|  |                             document.getElementById("top_bar").setAttribute("hidden",""); | ||||||
|  |                             status("conected") | ||||||
|  |                             setTimeout(()=>{get()},200) | ||||||
|  |                         } | ||||||
|  |                         else{ | ||||||
|  |                             setTimeout(()=>{get();},5000) | ||||||
|  |                             no_stream(); | ||||||
|  |                         } | ||||||
|                     } |                     } | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|             ws.onerror = (e) => { |             ws.onerror = (e) => { | ||||||
|                 setTimeout(()=>{con()},5000) |                 // setTimeout(()=>{con()},5000) | ||||||
|                 no_stream(); |                 no_stream(); | ||||||
|                         // console.error('websocket fail'); |                         // console.error('websocket fail'); | ||||||
|             } |             } | ||||||
| @ -146,16 +152,22 @@ | |||||||
|                 // console.log("The connection has been closed successfully.") |                 // console.log("The connection has been closed successfully.") | ||||||
|             }; |             }; | ||||||
| 
 | 
 | ||||||
|             function no_stream(){ |             function no_stream(freeze = false){ | ||||||
|                 if (document.body.style.backgroundImage != `url(/papi/savehouse/?const=club_dance)`){ |                 if (!freeze){ | ||||||
|                     document.body.style.backgroundImage = `url(/papi/savehouse/?const=club_dance)`; |                     if (document.body.style.backgroundImage != `url(/papi/savehouse/?const=club_dance)`){ | ||||||
|                 } |                         document.body.style.backgroundImage = `url(/papi/savehouse/?const=club_dance)`; | ||||||
|                 document.getElementById("top_bar").removeAttribute("hidden"); |                     } | ||||||
|                 if(was_connect){ |                     document.getElementById("top_bar").removeAttribute("hidden"); | ||||||
|                     status("Stream has ended") |                     if(was_connect){ | ||||||
|  |                         status("Stream has ended") | ||||||
|  |                     } | ||||||
|  |                     else{ | ||||||
|  |                         status("Stream is closed") | ||||||
|  |                     } | ||||||
|                 } |                 } | ||||||
|                 else{ |                 else{ | ||||||
|                     status("Stream is closed") |                     document.getElementById("top_bar").removeAttribute("hidden"); | ||||||
|  |                     status("stream is frozen") | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } else { |         } else { | ||||||
		Reference in New Issue
	
	Block a user