def skim_test_4_1_2(n_topics=10): # test 'globals' subLock = threading.Lock() subscriptions = dict() def received_message(m): data = json.loads(m.data) if data['op'] == 'publish': r_topic = data['topic'] r_msg = json.dumps(data['msg']) with subLock: for sub in subscriptions.itervalues(): if r_topic == sub['topic']: sub['callback'](r_msg) ws = WebSocketClient(ROSBRIDGE_HOST) ws.received_message = received_message ws.connect() current_subID = 1 for i_top in xrange(n_topics): # RUN: One Topic (per topic code) topicArrived = threading.Event() topicArrived.clear() def genf(i_topic=0): return lambda data: topicArrived.set() # Subscribe subID = current_subID current_subID += 1 msg = {'op': 'subscribe', 'topic': SUBTOPIC, 'type': MSGTYPE, 'id': subID, 'throttle_rate': 5} ws.send(json.dumps(msg)) with subLock: subscriptions[subID] = {'callback': genf(i_top), 'topic': SUBTOPIC} # Wait for Msg got_msg = topicArrived.wait(2.0) # Unsubscribe msg = {'op': 'unsubscribe', 'id': subID, 'topic': SUBTOPIC} with subLock: del subscriptions[subID] ws.send(json.dumps(msg)) sleep(0.01) # 0.1 doesn't seem to cause the problem (after 20hr)... ws.close()
"mid": "6aad0b12-2192-4b90-8f40-08a2bc0b5c2a", "version": "1.0", "request": { "apiVer": "1.0.0", "timestamp": 1234567890, "pki":"fndjsafhop3u8rheowfh" }, "params": { "sn": "0000DB11138104887174101101740000", "category": "0xDB", "model": "123", "id": "23454365465643", "ip": "0.0.0.0", "mac": "88e9fe5d3829", "random": "545623" } } if __name__ == '__main__': try: ws = WebSocketClient('ws://linksit.aimidea.cn:10000/cloud/connect', protocols=['chat']) ws.connect() print(ws.received_message("连接成功")) # # ws.run_forever() print("连接成功") except KeyboardInterrupt: ws.close() print("失败了") ws.send(json.dumps(data)) # print(ws.recv()) time.sleep(10) ws.close()
def hack_test_4_1_2(n_topics=10): # test 'globals' subLock = threading.Lock() subscriptions = dict() def received_message(m): data = json.loads(m.data) if data['op'] == 'publish': r_topic = data['topic'] r_msg = json.dumps(data['msg']) # print data # print data.keys() with subLock: for sub in subscriptions.itervalues(): if r_topic == sub['topic']: sub['callback'](r_msg) w_type = 2 if w_type==1: ws = WebSocketClient(ROSBRIDGE_HOST) elif w_type==2: ws = WebSocketClient(ROSBRIDGE_HOST) ws.received_message = received_message # elif w_type==3: # ws = WebSocketClient(ROSBRIDGE_HOST) # def received_message(self, m): # data = json.loads(m.data) # if data['op'] == 'publish': # r_topic = data['topic'] # r_msg = json.dumps(data['msg']) # # self.subLock.acquire(True) # for sub in self.subscriptions.itervalues(): # if r_topic == sub['topic']: # sub['callback'](r_msg) # self.subLock.release() # # ws.received_message = received_message else: ws = RosBridgeClient(ROSBRIDGE_HOST) ws.connect() # subLock = threading.Lock() # subscriptions = dict() current_subID = 1 for i_top in xrange(n_topics): # RUN: One Topic (per topic code) topicArrived = threading.Event() topicArrived.clear() def genf(i_topic=0): return lambda data: topicArrived.set() # subID = ws.subscribe(SUBTOPIC, MSGTYPE, genf()) def subscribe(current_subID, topic, messageType, callback): subID = current_subID current_subID += 1 msg = {'op': 'subscribe', 'topic': SUBTOPIC, 'type': MSGTYPE, 'id': subID, 'throttle_rate': 5} ws.send(json.dumps(msg)) with subLock: subscriptions[subID] = {'callback': callback, 'topic': SUBTOPIC} return subID subID = subscribe(current_subID, SUBTOPIC, MSGTYPE, genf(i_top)) got_msg = topicArrived.wait(2.0) # ws.unsubscribe(subID) def unsubscribe(subID): msg = {'op': 'unsubscribe', 'id': subID, 'topic': SUBTOPIC} with subLock: del subscriptions[subID] ws.send(json.dumps(msg)) unsubscribe(subID) ws.close()
def received_message(self, message): WebSocketClient.received_message(self,message) self._wrapper._ws_on_message(message)