MySocketIOTest / myiotestb /

from myiotestb.models import DBSession
from myiotestb.models import MyModel
from pyramid.view import view_config
from import SocketIOContext, socketio_manage
from pyramid.response import Response

import gevent

#class ConnectIOContext(SocketIOContext):
#    # is the Socket.IO socket
#    # self.request is the request
#    def msg_connect(self, msg):
#        print "Connect message received", msg
#        self.msg("connected", hello="world")

class ConnectIOContext(SocketIOContext):
    def msg_connect(self, msg):
        def sendcpu():
            """Calculate CPU utilization"""
            prev = None
                vals = map(int, [x for x in open('/proc/stat').readlines()
                                 if x.startswith('cpu ')][0].split()[1:5])
                if prev:
                    percent = (100.0 * (sum(vals[:3]) - sum(prev[:3])) / 
                               (sum(vals) - sum(prev)))
                    self.msg("showdata", point=percent)
                prev = vals

# Socket.IO implementation
def socketio_service(request):
    print "call de la vue socketio_service"
    retval = socketio_manage(ConnectIOContext(request))
    return Response(retval)

@view_config(route_name='home', renderer='myiotestb:templates/index.mako')
def test(request):
    print "socket io is inside request.environ"
    print request.environ['socketio']
    return {}