forked from chepecarlos/ElGarrobo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMiWebSoket.py
47 lines (42 loc) · 1.31 KB
/
MiWebSoket.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import websocket
#
# def ComandoWebSocket(comando):
# if 'Servidor' in data:
# ws = websocket.WebSocket()
#
# print(Servidor)
# ws.connect(Servidor)
# ws.send(comando)
# # print ("Reciviendo...")
# # result = ws.recv()
# print (f"comando enviado")
# ws.close()
class MiWebSoket:
def __init__(self):
self.host = "localhost"
self.port = 4444
self.WebSocketConectado = False
def CambiarHost(self, host_):
self.host = host_
def Conectar(self):
try:
self.ws = websocket.WebSocket()
self.Servidor = "ws://{}:8765".format(self.host)
self.ws.connect(self.Servidor)
self.WebSocketConectado = True
# self.result = self.ws.recv()
# print("Received '%s'" % self.result)
print("Conectado")
except:
print("No se pudo conectar a WebSocket")
self.WebSocketConectado = False
async def Enviar(self, EnviarValor):
if self.WebSocketConectado:
print("Enviando datos")
await self.ws.send(EnviarValor)
else:
print("No conectado a WebSocket")
def Cerrar(self):
if self.WebSocketConectado:
print("Cerrando WebSocket")
self.ws.close()