-
Notifications
You must be signed in to change notification settings - Fork 0
/
UsoWebSocket.py
66 lines (48 loc) · 2.05 KB
/
UsoWebSocket.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from twisted.internet.protocol import ReconnectingClientFactory
from autobahn.twisted.websocket import WebSocketClientProtocol, WebSocketClientFactory
import json
import base64
import subprocess
from twisted.internet.task import LoopingCall
from twisted.internet import reactor
server = "ritaportal.udistrital.edu.co" # Server IP Address or domain eg: tabvn.com
port = 10207 # Server Port
class AppProtocol(WebSocketClientProtocol):
INTERVAL=1
def onConnect(self, response):
self._loop = LoopingCall(self.envioImage)
print("server conectado")
def onConnecting(self, transport_details):
return None # ask for defaults
def onMessage(self, payload, isBinary):
text_data_json = json.loads(payload.decode('utf8'))
if(text_data_json['type']=='MC'):
print("MC")
self._loop.start(self.INTERVAL)
elif(text_data_json['type']=='MD'):
print("MD")
if _loop.running:
_loop.stop()
else:
print("No entiendo")
async def onClose(self, wasClean, code, reason):
print("WebSocket connection closed: {0}".format(reason))
def Send(self):
print("Sending")
self.sendMessage(json.dumps({'userFrom':'2','userTo': '1','type':'data','message':'hello'}).encode('utf8'))
class AppFactory(WebSocketClientFactory, ReconnectingClientFactory):
protocol = AppProtocol
def clientConnectionFailed(self, connector, reason):
print("Unable connect to the server {0}".format(reason))
self.retry(connector)
def clientConnectionLost(self, connector, reason):
print("Lost connection and retrying... {0}".format(reason))
self.retry(connector)
if __name__ == '__main__':
import sys
from twisted.python import log
from twisted.internet import reactor
log.startLogging(sys.stdout)
factory = AppFactory(u"ws://ritaportal.udistrital.edu.co:10207/jordan")
reactor.connectTCP(server, port, factory)
reactor.run()