Пример #1
0
  def thread_func(S):
    print 'SocketIO: websocket thread started'
    
    my_url = 'wss://' + S.url + "/1/websocket/" + S.id
    
    S.ws = create_connection(my_url)
    
    #S.ws = WebSocket(my_url, version=0) 
    S.run = True
    S.ws.send('1::/mtgox')

    # start keepalive thread
    S.keepalive_thread = Thread(target = S.keepalive_func)
    S.keepalive_thread.setDaemon(True)
    S.keepalive_thread.start()
    
    msg = S.ws.recv()
    while msg is not None and S.run:
      #print 'SocketIO msg: ', msg
      if msg[:10] == "4::/mtgox:":
        S.callback(msg[10:])
      #elif msg[:3] == "2::":
      #  True
      #else:
      #  print "SocketIO: dont know how to handle msg: ", msg
      msg = S.ws.recv()
    S.ws.close()
Пример #2
0
	def appsoma_test_alive_collector():
		try:
			with open( "appsoma.port" ) as f:
				port = int(f.read()) + 1
			
			#ACB: Using websockets to check if it's alive
			try:
				ws = websocket_client.create_connection("wss://localhost:" + str(port-1), timeout=2, header=[ "Sec-WebSocket-Protocol: base64" ])
			except websocket_client.WebSocketTimeoutException:
				return 0
			if ws.send(base64.b64encode(json.dumps({ 'command':'rpc_alive' }))) <= 0:
				return 0
			recv = json.loads(base64.b64decode(ws.recv()))
			try:
				ws.close()
			except:
				pass
			return 1 if (recv["command"] == "rpc_alive" and "error" not in recv) else 0
			
		except Exception as e:
			return 0
Пример #3
0
   def thread_func(S):
      thread_ident = current_thread().name + ":"
      S._debug(thread_ident + "this is thread_func")
      S._debug('SocketIO: websocket thread started')
      
      #handle if URL contains a '?' (in case of ?currency=XXX is appended)
      if S.url.find('?') == -1:
         my_url = 'wss://' + S.url + "/1/websocket/" + S.id
      else:
         my_url = 'wss://' + S.url[0:S.url.find('?')] + "/1/websocket/" + S.id + S.url[-(len(S.url)-S.url.find('?')):]
      
      S._debug("connecting websocket...")
      S.ws = create_connection(my_url)
      S._debug("connected!")

      S.run = True
      S.reconnect = False
      S.ws.send('1::/mtgox')

      # start keepalive thread
      if (S.keepalive_thread is None or S.keepalive_thread.isAlive() == False):
         S._debug(thread_ident + "no keepalive thread is running. creating new...")
         S.keepalive_thread = Thread(target=S.keepalive_func)
         S.keepalive_thread.daemon = True
         S.keepalive_thread.start()         
      
      msg = S.ws.recv()
      while msg is not None and S.run:
         if msg[:10] == "4::/mtgox:":
            S.callback(msg[10:])
         try:
            msg = S.ws.recv()
         except:
            if (S.run == True):
               print thread_ident, "error waiting for message to arrive. closing existing connection..."
               S.run = False
               S.ws.close()
               print thread_ident, '\ttrying reconnect...'
               S.connect()
            if (S.reconnect == True):
               print thread_ident, '\ttrying reconnect...'
               S.connect()
            #as far as I can tell, reraising the exception should make this thread exit
            #  so it's ok if we've create a new one by calling S.connect() above
            S._debug(thread_ident + 'about to raise exception...')
            raise
            #DEBUG
            print "ERROR!: this message shouldn't be printed! (after raise in a thread)"
      else:
         if (S.run == False and S.reconnect == True):
            #this means the keepalive thread function caught an exception, set S.run to False and closed the connection. we should reconnect and exit this thread
            print thread_ident, 'trying reconnect...'
            S.connect()
            raise SystemExit
            #DEBUG
            print "ERROR!: this message shouldn't be printed! (after raise in a thread)"

         if (msg is None and S.run == True):
            print thread_ident, "recv() returned empty message. connection is broken. let's try to close this connection and reconnect, and then exit this thread."
            S.run = False
            S.ws.close()
            print thread_ident, '\ttrying reconnect...'
            S.connect()
            raise SystemExit
            #DEBUG
            print "ERROR!: this message shouldn't be printed! (after raise in a thread)"