本文整理汇总了Python中socketIO_client.SocketIO.get_namespace方法的典型用法代码示例。如果您正苦于以下问题:Python SocketIO.get_namespace方法的具体用法?Python SocketIO.get_namespace怎么用?Python SocketIO.get_namespace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类socketIO_client.SocketIO
的用法示例。
在下文中一共展示了SocketIO.get_namespace方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Transaction
# 需要导入模块: from socketIO_client import SocketIO [as 别名]
# 或者: from socketIO_client.SocketIO import get_namespace [as 别名]
class Transaction(object):
def __init__(self):
self.custom_timers = {}
self.word_list = buildArray( )
self.word_list_length = len(self.word_list)
self.socketIO = SocketIO(HOST, PORT)
self.socketIO.define(Namespace)
def makeCall( self, jsonObject ):
start_timer = time.time()
self.socketIO.message(jsonObject)
self.socketIO.wait(0.01)
namespace = self.socketIO.get_namespace()
#print namespace.response
latency = time.time() - start_timer
self.custom_timers['trie_service_sockets'] = latency
#if latency > 5:
#writeErrorInfo('latency value of %f with %s' %(latency, url) )
#if r.status_code != 200:
#writeErrorInfo('status code with %s is %s' %(r.status_code, url) )
#assert (r.status_code == 200), 'Bad HTTP Response'
#assert ('suggestions' in namespace.response), 'No suggestions'
def run(self):
#get a random word from the word_list and call to typeahead
whichPhrase = random.randrange(0,self.word_list_length)
phrase = self.word_list[whichPhrase]
word = phrase.split(" ")[0]
#print 'word is %s' % word
message = ""
jsonObject = ""
#loop through the chars in the word skipping the first
#for char in word:
# message = message + char
# jsonObject = {'@class': 'com.glg.service.TrieObject', 'entity':'cm', 'prefix':message}
# self.makeCall( jsonObject )
jsonObject = {'@class': 'com.glg.service.TrieObject', 'entity':'cm', 'prefix':word}
self.makeCall( jsonObject )
示例2: LetschatClient
# 需要导入模块: from socketIO_client import SocketIO [as 别名]
# 或者: from socketIO_client.SocketIO import get_namespace [as 别名]
#.........这里部分代码省略.........
room = [room for room in self._rooms if room.get('id') == args[0].get('id')]
if room:
room = room[0]
log.info('Updated {}'.format(room.get('name')))
room['name'] = args[0].get('name')
room['description'] = args[0].get('description')
@property
def username(self):
return self._user.get('username')
@property
def rooms(self):
return self._rooms
@property
def joined_rooms(self):
joined_ = [room for room in self._rooms if room.get('id') in self._joined_rooms]
return joined_
@property
def connected(self):
return self._connected
def __init__(self, hostname, port, token, protocol='http', callbacks={}):
self._sio = SocketIO('{}://{}'.format(protocol, hostname), port,
LetschatClient.LetschatNamespace,
params={'token': token})
self.on_users_join_handler = callbacks.get('on_users_join', None)
self.on_users_leave_handler = callbacks.get('on_users_leave', None)
self.on_messages_new_handler = callbacks.get('on_messages_new', None)
# wait for connection sequence.
while not self._sio.get_namespace().connected:
self._sio.wait(seconds=1)
def emit(self, event, *args, **kw):
self._sio.emit(event, *args, **kw)
def wait(self, seconds=None):
self._sio.wait(seconds)
def emit_messages_create(self, message):
self.emit('messages:create', message)
def emit_rooms_join(self, roomid):
self.emit('rooms:join', roomid, self.server.on_rooms_join_response)
def emit_rooms_leave(self, roomid):
self.emit('rooms:leave', roomid)
self.server._joined_rooms.remove(roomid)
def emit_rooms_create(self, name):
options = {
'name': name,
'slug': name,
}
self.emit('rooms:create', options, self.server.on_rooms_create_response)
def emit_rooms_archive(self, roomid):
room = [room for room in self.server._rooms if room.get('id') == roomid]
if room:
options = {
'id': roomid,
}
self.emit('rooms:archive', options)
示例3: TestSocketIO
# 需要导入模块: from socketIO_client import SocketIO [as 别名]
# 或者: from socketIO_client.SocketIO import get_namespace [as 别名]
class TestSocketIO(TestCase):
def setUp(self):
self.socketIO = SocketIO(HOST, PORT)
self.called_on_response = False
def tearDown(self):
del self.socketIO
def on_response(self, *args):
self.called_on_response = True
for arg in args:
if isinstance(arg, dict):
self.assertEqual(arg, PAYLOAD)
else:
self.assertEqual(arg, DATA)
def is_connected(self, socketIO, connected):
childThreads = [
socketIO._rhythmicThread,
socketIO._listenerThread,
]
for childThread in childThreads:
self.assertEqual(not connected, childThread.done.is_set())
self.assertEqual(connected, socketIO.connected)
def test_disconnect(self):
'Terminate child threads after disconnect'
self.is_connected(self.socketIO, True)
self.socketIO.disconnect()
self.is_connected(self.socketIO, False)
# Use context manager
with SocketIO(HOST, PORT) as self.socketIO:
self.is_connected(self.socketIO, True)
self.is_connected(self.socketIO, False)
def test_message(self):
'Message'
self.socketIO.define(Namespace)
self.socketIO.message()
self.socketIO.wait(0.1)
namespace = self.socketIO.get_namespace()
self.assertEqual(namespace.response, 'message_response')
def test_message_with_data(self):
'Message with data'
self.socketIO.define(Namespace)
self.socketIO.message(DATA)
self.socketIO.wait(0.1)
namespace = self.socketIO.get_namespace()
self.assertEqual(namespace.response, DATA)
def test_message_with_payload(self):
'Message with payload'
self.socketIO.define(Namespace)
self.socketIO.message(PAYLOAD)
self.socketIO.wait(0.1)
namespace = self.socketIO.get_namespace()
self.assertEqual(namespace.response, PAYLOAD)
def test_message_with_callback(self):
'Message with callback'
self.socketIO.message(callback=self.on_response)
self.socketIO.wait_for_callbacks(seconds=0.1)
self.assertEqual(self.called_on_response, True)
def test_message_with_callback_with_data(self):
'Message with callback with data'
self.socketIO.message(DATA, self.on_response)
self.socketIO.wait_for_callbacks(seconds=0.1)
self.assertEqual(self.called_on_response, True)
def test_emit(self):
'Emit'
self.socketIO.define(Namespace)
self.socketIO.emit('emit')
self.socketIO.wait(0.1)
self.assertEqual(self.socketIO.get_namespace().argsByEvent, {
'emit_response': (),
})
def test_emit_with_payload(self):
'Emit with payload'
self.socketIO.define(Namespace)
self.socketIO.emit('emit_with_payload', PAYLOAD)
self.socketIO.wait(0.1)
self.assertEqual(self.socketIO.get_namespace().argsByEvent, {
'emit_with_payload_response': (PAYLOAD,),
})
def test_emit_with_multiple_payloads(self):
'Emit with multiple payloads'
self.socketIO.define(Namespace)
self.socketIO.emit('emit_with_multiple_payloads', PAYLOAD, PAYLOAD)
self.socketIO.wait(0.1)
self.assertEqual(self.socketIO.get_namespace().argsByEvent, {
'emit_with_multiple_payloads_response': (PAYLOAD, PAYLOAD),
})
def test_emit_with_callback(self):
#.........这里部分代码省略.........
示例4: WaptSocketIOClient
# 需要导入模块: from socketIO_client import SocketIO [as 别名]
# 或者: from socketIO_client.SocketIO import get_namespace [as 别名]
class WaptSocketIOClient(threading.Thread):
def __init__(self,config_filename = 'c:/wapt/wapt-get.ini',task_manager=None):
threading.Thread.__init__(self)
self.name = 'SocketIOClient'
self.config_filename = config_filename
self.task_manager = task_manager
self.config = WaptServiceConfig(config_filename)
self.socketio_client = None
self.wapt_remote_calls = None
self.server_authorization_token = None
def run(self):
self.config.reload_if_updated()
with Wapt(config_filename = self.config.config_filename) as tmp_wapt:
logger.info('Starting socketio on "%s://%s:%s" ...' % (self.config.websockets_proto,self.config.websockets_host,self.config.websockets_port))
logger.debug('Certificate checking : %s' % self.config.websockets_verify_cert)
def get_connect_params(wapt):
connect_params = {'uuid': wapt.host_uuid}
if not self.server_authorization_token:
try:
self.server_authorization_token = wapt.get_auth_token('websocket')
logger.info('Websocket token: %s' % self.server_authorization_token)
connect_params['token'] = self.server_authorization_token
except Exception as e:
logger.warning('Websocket connect params: %s' % e)
self.server_authorization_token = None
return {'params':connect_params,
'cert' :(wapt.get_host_certificate_filename(),wapt.get_host_key_filename())}
while True:
try:
connect_params = get_connect_params(tmp_wapt)
if not self.socketio_client and self.config.websockets_host:
logger.debug('Creating socketio client')
logger.debug('Proxies : %s'%self.config.waptserver.proxies)
# bug in socketio... ? we must not pass proxies at all (even None) if we don"t want to switch to polling mode...
kwargs = {}
if self.config.waptserver.proxies and self.config.waptserver.proxies.get(self.config.websockets_proto,None) is not None:
kwargs['proxies'] = self.config.waptserver.proxies
kwargs.update(connect_params)
self.socketio_client = SocketIO(
host="%s://%s" % (self.config.websockets_proto,self.config.websockets_host),
port=self.config.websockets_port,
Namespace = WaptSocketIORemoteCalls,
resource=self.config.websockets_root,
verify=self.config.websockets_verify_cert,
wait_for_connection = False,
transport = ['websocket'],
ping_interval = self.config.websockets_ping,
hurry_interval_in_seconds = self.config.websockets_hurry_interval,
**kwargs)
self.socketio_client.get_namespace().wapt = tmp_wapt
self.socketio_client.get_namespace().task_manager = self.task_manager
if self.socketio_client and self.config.websockets_host:
if not self.socketio_client.connected:
self.socketio_client._http_session.update(connect_params)
self.socketio_client.define(WaptSocketIORemoteCalls)
self.socketio_client.get_namespace().wapt = tmp_wapt
self.socketio_client.get_namespace().task_manager = self.task_manager
self.socketio_client.connect('')
else:
# be sure server DB is aware of the current connection.
# could be avoided
self.socketio_client.emit('wapt_pong')
if self.socketio_client.connected:
logger.info('Socket IO listening for %ss' % self.config.websockets_check_config_interval )
startwait = time.time()
self.socketio_client.wait(self.config.websockets_check_config_interval)
# QAD workaround for cases where server disconnect but client is between 2 states.
# In this case; wait() immediately returns, leading to an indefinite loop eating 1 core.
if time.time() - startwait < self.config.websockets_check_config_interval-2:
raise Exception('Websocket client seems disconnected. Force Websocket connection to be recreated')
elif not self.config.websockets_host:
self.socketio_client = None
time.sleep(self.config.websockets_retry_delay)
else:
time.sleep(self.config.websockets_retry_delay)
if self.config.reload_if_updated():
tmp_wapt.reload_config_if_updated()
if self.socketio_client:
self.socketio_client.disconnect()
raise EWaptException('Configuration changed, force Websocket connection to be recreated')
except Exception as e:
try:
# reset token
self.server_authorization_token = None
self.config.reload_if_updated()
if self.socketio_client:
self.socketio_client = None
finally:
logger.debug(u'Exception %s, Socket IO Stopped, waiting %ss before retrying' %
(repr(e),self.config.websockets_retry_delay))
#.........这里部分代码省略.........