Selaa lähdekoodia

handle basic types with cbs

David 10 vuotta sitten
vanhempi
commit
b94ac688ba
2 muutettua tiedostoa jossa 39 lisäystä ja 3 poistoa
  1. 10 1
      client/main.py
  2. 29 2
      client/websocket.py

+ 10 - 1
client/main.py

@@ -31,7 +31,7 @@ class Main(object):
         self.c = Clock(self.timerTick)
         self.c.setDaemon(True)
     
-        self.ws = Websocket()
+        self.ws = Websocket(self.add_question, self.handle_diapo,self.handle_rec,self.toggle,self.handle_video)
         self.ws.setDaemon(True)
 
         self.slides = Slides("../node/")
@@ -45,6 +45,15 @@ class Main(object):
         print('Running. Exit with Ctrl-C')
         signal.pause()
 
+    def add_question(self,question):
+        pass
+    def handle_diapo(self,data):
+        pass
+    def handle_rec(self,data):
+        pass
+    def handle_video(self,data):
+        pass
+
     def next(self):
         if self.mode == Mode.diapo:
             self.s.send(self.slides.next())

+ 29 - 2
client/websocket.py

@@ -1,11 +1,24 @@
 import asyncio
 import websockets
 import threading
+import json
 
 class Websocket(threading.Thread):
-    def __init__(self):
+    def __init__(self, add_question, handle_diapo, handle_rec, toggle, handle_video):
         super(Websocket, self).__init__()
         self.start_server = websockets.serve(self.handle_input, '127.0.0.1', 5678)
+        self.callbacks={ 
+                            "add_preg": add_question,
+                            "diapo": add_question,
+                            "startrec":handle_rec,
+                            "startrectrat":handle_rec,
+                            "stoprec":handle_rec,
+                            "toggle":toggle,
+                            "video":handle_video
+                        }
+        print(type(self.callbacks))
+        self.valid_data_types=["add_preg", "diapo", "startrec","startrectrat","stoprec","toggle","video"]
+
         #asyncio.get_event_loop().run_until_complete(start_server)
         #asyncio.get_event_loop().run_forever()
         self.loop = asyncio.get_event_loop()
@@ -19,7 +32,21 @@ class Websocket(threading.Thread):
         while True:
             try:
                 data = await websocket.recv()
-                print("{}".format(data))
+                try:
+                    data=json.loads(data)
+                except Exception as e:
+                    print(e)
+                    print(data)
+                    print("No pude hacer loads")
+                    continue
+                print(data)
+                if not "type" in data:
+                    print("data!= type?")
+                    continue
+                #add preg, diapo, start|stop rec, switch mode, add video
+                if data["type"] in self.valid_data_types:
+                    print("{}".format(data))
+                    self.callbacks[data["type"]]()
             except websockets.exceptions.ConnectionClosed as c:
                 print( "client dc'd")
                 break