Example #1
0
 def handle_websocket_client(environ, session, user_id):
     if session:
         ws = get_websocket(environ)
         ws_main_online(ws, user_id)
         app = aConfig['gConfig']['wsgi']['application']
         interval = 1
         try:
             interval = float(aConfig['gConfig']['applications'][app]['websocket']['interval_poll_client'])
         except:
             interval = 1
         while ws and not ws.closed:
             try:
                 ws.send('', binary=False)
             except Exception as e:
                 # raise e
                 ws_main_offline(ws)
                 break
             gevent.sleep(interval)
Example #2
0
    def handle_websocket_data(environ, session, user_id):
        global gListenLoopList
        ws = get_websocket(environ)
        ws_mavlink_online(ws, user_id)
        v = str(ws.__hash__())
        m = _.find(gListenLoopList, {'ws_hash': v})
        if m is None:
            g = gevent.spawn(create_ws_recv_loop, aConfig, ws)
            gListenLoopList.append({'ws_hash':v, 'greenlet':g})

        app = aConfig['gConfig']['wsgi']['application']
        interval = 10
        try:
            interval = float(aConfig['gConfig']['applications'][app]['websocket']['interval_poll_mavlink'])
        except:
            interval = 10
        while ws and not ws.closed:
            try:
                # ws.send(str.encode('', encoding=ENCODING), binary=True)
                ws.send('', binary=True)
            except:
                ws_mavlink_offline(ws)
                break
            gevent.sleep(interval)
Example #3
0
 def handle_websocket(environ, session):
     ws = get_websocket(environ)
     app = aConfig['gConfig']['wsgi']['application']
     interval = 1.0
     try:
         interval = float(aConfig['gConfig']['applications'][app]['websocket']['interval_poll'])
     except:
         interval = 1.0
     while ws and not ws.closed:
         obj = ws_recv(environ)
         if obj and isinstance(obj, dict) and obj.has_key('op'):
             if obj['op'] == 'queue_size':
                 qsize = 0
                 if gJoinableQueue:
                     qsize = gJoinableQueue.qsize()
                 ws.send(json.dumps({'queue_size':qsize}, ensure_ascii=True, indent=4))
             elif obj['op'] == 'chat/online':
                 rec = []
                 if obj.has_key('_id') and len(obj['_id'])>0:
                     rec = user_query(session, {'_id':obj['_id']})
                 elif obj.has_key('username') and len(obj['username'])>0:
                     rec = user_query(session, {'username':obj['username']})
                 if len(rec)>0:
                     r0 = rec[0]
                     _id = str(r0['_id'])
                     online(_id, ws)
                     r0['contacts'] = json.loads(user_contact_get(session, {'_id':_id,'user_detail':True}))
                     r0['groups'] = json.loads(user_group_get(session, {'_id':_id,'user_detail':True}))
                     d = remove_mongo_id(r0)
                     d['op'] = obj['op']
                     d['from'] = _id
                     d['to'] = _id
                     d['timestamp'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
                     gJoinableQueue.put(d)
                     #ws.send(json.dumps(d, ensure_ascii=True, indent=4))
                     if obj.has_key('inform_contact') and obj['inform_contact'] is True:
                         other_contacts = list(gWebSocketsMap.keys())[:]
                         if _id in other_contacts:
                             other_contacts.remove(_id)
                         broadcast(session, ws, other_contacts, {'op':'chat/info/online','from':_id})
                     limit = 10
                     if aConfig['gConfig']['applications'][app].has_key('resend') and  aConfig['gConfig']['applications'][app]['resend'].has_key('max_resend_record_num'):
                         try:
                             limit = int(aConfig['gConfig']['applications'][app]['resend']['max_resend_record_num'])
                         except:
                             pass
                     resend_offline_msg(session, _id, limit)
                 else:
                     ws.send(json.dumps({'result':'chat_online_user_not_exist'}, ensure_ascii=True, indent=4))
             elif obj['op'] == 'chat/offline':
                 if obj.has_key('_id'):
                     _id = obj['_id']
                     if obj.has_key('inform_contact') and obj['inform_contact'] is True:
                         other_contacts = list(gWebSocketsMap.keys())[:]
                         if _id in other_contacts:
                             other_contacts.remove(_id)
                         broadcast(session, ws, other_contacts,  {'op':'chat/info/offline','from':_id, 'timestamp': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())})
                     offline(_id)
                 elif obj.has_key('username'):
                     rec = user_query(session, {'username':obj['username']})
                     if len(rec)>0:
                         _id = str(rec[0]['_id'])
                         if obj.has_key('inform_contact') and obj['inform_contact'] is True:
                             other_contacts = list(gWebSocketsMap.keys())[:]
                             if _id in other_contacts:
                                 other_contacts.remove(_id)
                             broadcast(session, ws, other_contacts, {'op':'chat/info/offline','from':_id})
                         offline(_id)
                     else:
                         ws.send(json.dumps({'result':'chat_offline_user_not_exist'}, ensure_ascii=True, indent=4))
                 else:
                     ws.send(json.dumps({'result':'chat_offline_username_or_id_required'}, ensure_ascii=True, indent=4))
             elif obj['op'] == 'chat/chat':
                 chat(session, ws, obj)
             elif 'chat/request' in obj['op'] or 'chat/response' in obj['op']:
                 request_response(session, ws, obj)
         else:
             try:
                 ws.send('')
             except:
                 _id = None
                 for k in gWebSocketsMap.keys():
                     if ws in gWebSocketsMap[k] :
                         _id = k
                         break
                 if _id:
                     print('websocket[%s] is closed2' % _id)
                     offline(_id)
                     broadcast(session, None, gWebSocketsMap.keys(), {'op':'chat/info/offline', 'from':_id})
         gevent.sleep(interval)
     if ws and ws.closed:
         del ws