def connect_to_server(self): socketIO = SocketIO(server_url, params={'t': long(time.time() * 1000)}, cookies={"session": str(self.server_session_id)}) server_namespace = socketIO.define(client.ServerNamespace, '/client') #really wish i could do this in the constructor server_namespace.login = self return socketIO, server_namespace
def connect(self): """ Public method to connect socket-io """ SocketIONamespace.rtc = self #we need to access this object from socket-io namespace #keyword arguments for socket-io instance kwargs = { 'Namespace': SocketIONamespace, #socket-io namespace 'cookies': api.session.cookies, #cookies for authentication 'hooks': { 'response': self.on_response }, #hook for response 'stream': True #for long polling } #socket-io by default uses websocket transport, but websockets wont work behind a proxy #we force xhr polling in such cases if self.univ.details['isProxy'] == True: _log.info('Proxy detected; Forcing xhr-polling for SocketIO') kwargs['transports'] = ['xhr-polling'] _log.debug('Waiting for SocketIO connection') exception = None try: #instantiate socket-io self.sio = None self.session_id = api.session.cookies.get('SessionID') self.sio = SocketIO(self.univ.get_url(), **kwargs) except SocketIOHandShakeError as e: #handshake error exception = e _log.error('Failed to connect SocketIO; %s' % unicode(e)) except Exception as e: exception = e _log.error(unicode(e)) return exception
def connect_to_app(self, app_name, url=None): self.login() if url is None: self.app_name = app_name url_response = self.s.get(server_url + '/app-url?app-name=' + app_name) url = url_response.json()['objects']['url'] self.app_url = url else: self.app_url = url auth_info = self.s.get(url + '/login').json() response = self.server_post(server_url + '/oauth/authorize', data=auth_info['objects']) response = response.json() self.app_auth_token = response['objects']['auth_token'] self.app_post(url + '/register-device-id', data={'device_id': self.device['id']}) #hack since i actually dont know how to just get a cookie cookie = None for session_cookie in self.s.cookies: if session_cookie.domain != self.server_domain: cookie = session_cookie o = urlparse(self.app_url) socketIO = SocketIO(o.hostname, o.port, params={'t': long(time.time() * 1000)}, cookies={"session": str(cookie.value)}) app_namespace = socketIO.define(client.AppNamespace, '/client') app_namespace.login = self return socketIO, app_namespace
import re import requests import json import os import subprocess import time from socketio_client import SocketIO, LoggingNamespace socketIO = SocketIO('localhost', 8000, LoggingNamespace) reg = "TS:(\d+).*Drop:.*RX:(\d+).*TX:(\d+).*RSSI:(-*\d+.\d+).*CRC Data:.*light: (\d+) temp: (\d+.\d*) humidity: (\d+)" cmd = "gstdbuf -o0 ./pip_sense.v2 l l | gstdbuf -o0 grep TX:03408" p = subprocess.Popen(cmd, bufsize=1, stdout=subprocess.PIPE, shell=True) for line in iter(p.stdout.readline, ""): line = line.decode("utf-8") print(line) payload = [] if line.startswith("TS"): data = {} match = re.search(reg, line).groups() data["ts"] = match[0] data["rx"] = match[1] data["tx"] = match[2] data["rssi"] = match[3] data["light"] = match[4] data["temp"] = match[5] data["hum"] = match[6] socketIO.emit('update', json.dumps(data)) p.stdout.close() p.wait()