-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket_test.py
67 lines (46 loc) · 1.89 KB
/
socket_test.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
67
# -*- coding: utf-8 -*-
from autobahn.twisted.websocket import WebSocketClientProtocol, WebSocketClientFactory, connectWS
import thread
import json
from twisted.internet import reactor
CLIENT_TOKEN = 'enter your client access token'
class CloudbusClientProtocol(WebSocketClientProtocol):
def onConnect(self, response):
self.factory.web_socket = self
def onOpen(self):
print 'Start oauth'
self.sendMessage( json.dumps({'kind':'oauth', 'token':CLIENT_TOKEN}) )
def onClose(self, wasClean, code, reason):
print 'Close connect; clean='+str(wasClean)+' code:'+str(code)+' reason:'+str(reason)
def onServerConnectionDropTimeout(self):
print 'ServerConnectionDropTimeout'
def onPing(self, payload):
self.sendPong(payload)
#def onPong(self, payload):
# print "Pong received"
def onMessage(self, payload, isBinary):
msg = json.loads(payload)
print 'MSG: '+str(msg)
if msg['kind']=='oauth':
print 'OAUTH '+str(msg['user'])
ent = 162 # Demo Cloudbus enterprise Id
# subscribe to all events 'vehicle enter to area' :
jsub = { 'kind':'subscribe', 'subscribe':{'enterprise':ent, 'kind':'area_enter', 'type':'transport.vehicle', 'id':0} }
self.sendMessage( json.dumps(jsub) )
self.setTrackTimings(True)
print 'Start listen'
elif msg['kind']=='data':
itm = str(msg['tag']['id'])+": "+str(msg['tag']['kind']+" "+msg['tag']['type'])
print 'DATA: '+str(itm)
def ping_pong():
# test_factory.factory.web_socket.sendPing()
reactor.callLater(100, ping_pong)
###
test_factory = WebSocketClientFactory(
'wss://baseride.com/sockjs/websocket',
debug=True,
debugCodePaths=True)
test_factory.protocol = CloudbusClientProtocol
connectWS(test_factory)
reactor.callWhenRunning(ping_pong)
reactor.run()