当前位置: 首页>>代码示例>>Python>>正文


Python WebSocketClient.received_message方法代码示例

本文整理汇总了Python中ws4py.client.threadedclient.WebSocketClient.received_message方法的典型用法代码示例。如果您正苦于以下问题:Python WebSocketClient.received_message方法的具体用法?Python WebSocketClient.received_message怎么用?Python WebSocketClient.received_message使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ws4py.client.threadedclient.WebSocketClient的用法示例。


在下文中一共展示了WebSocketClient.received_message方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: skim_test_4_1_2

# 需要导入模块: from ws4py.client.threadedclient import WebSocketClient [as 别名]
# 或者: from ws4py.client.threadedclient.WebSocketClient import received_message [as 别名]
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()
开发者ID:rethink-rlinsalata,项目名称:test-debug,代码行数:51,代码来源:test_rbc_websocket.py

示例2: hack_test_4_1_2

# 需要导入模块: from ws4py.client.threadedclient import WebSocketClient [as 别名]
# 或者: from ws4py.client.threadedclient.WebSocketClient import received_message [as 别名]
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()
开发者ID:rethink-rlinsalata,项目名称:test-debug,代码行数:78,代码来源:test_rbc_websocket.py

示例3: received_message

# 需要导入模块: from ws4py.client.threadedclient import WebSocketClient [as 别名]
# 或者: from ws4py.client.threadedclient.WebSocketClient import received_message [as 别名]
 def received_message(self, message):
     WebSocketClient.received_message(self,message)
     self._wrapper._ws_on_message(message)
开发者ID:Omi-Jiang,项目名称:pydvbcss,代码行数:5,代码来源:__init__.py


注:本文中的ws4py.client.threadedclient.WebSocketClient.received_message方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。