コード例 #1
0
ファイル: views.py プロジェクト: MechanisM/django-socketio
def socketio(request):
    """
    Socket.IO handler - maintains the lifecycle of a Socket.IO
    request, sending the each of the events. Also handles
    adding/removing request/socket pairs to the CLIENTS dict
    which is used for sending on_finish events when the server
    stops.
    """
    socket = SocketIOChannelProxy(request.environ["socketio"])
    CLIENTS[socket.session.session_id] = (request, socket)
    try:
        if socket.on_connect():
            events.on_connect.send(request, socket)
        while True:
            message = socket.recv()
            if len(message) > 0:
                socket.handler.server.log.write(format_log(request, message))
                if message[0] == "__subscribe__" and len(message) == 2:
                    socket.subscribe(message[1])
                    events.on_subscribe.send(request, socket, message[1])
                elif message[0] == "__unsubscribe__" and len(message) == 2:
                    events.on_unsubscribe.send(request, socket, message[1])
                    socket.unsubscribe(message[1])
                else:
                    events.on_message.send(request, socket, message)
            else:
                if not socket.connected():
                    events.on_disconnect.send(request, socket)
                    break
    except Exception, exception:
        print_exc()
        events.on_error.send(request, socket, exception)
コード例 #2
0
def socketio(request):
    """
    Socket.IO handler - maintains the lifecycle of a Socket.IO
    request, sending the each of the events. Also handles
    adding/removing request/socket pairs to the CLIENTS dict
    which is used for sending on_finish events when the server
    stops.
    """
    context = {}
    socket = SocketIOChannelProxy(request.environ["socketio"])
    client_start(request, socket, context)
    try:
        if socket.on_connect():
            events.on_connect.send(request, socket, context)
        while True:
            messages = socket.recv()
            if not messages and not socket.connected():
                events.on_disconnect.send(request, socket, context)
                break
            # Subscribe and unsubscribe messages are in two parts, the
            # name of either and the channel, so we use an iterator that
            # lets us jump a step in iteration to grab the channel name
            # for these.
            messages = iter(messages)
            for message in messages:
                if message == "__subscribe__":
                    message = messages.next()
                    message_type = "subscribe"
                    socket.subscribe(message)
                    events.on_subscribe.send(request, socket, context, message)
                elif message == "__unsubscribe__":
                    message = messages.next()
                    message_type = "unsubscribe"
                    socket.unsubscribe(message)
                    events.on_unsubscribe.send(request, socket, context,
                                               message)
                else:
                    # Socket.IO sends arrays as individual messages, so
                    # they're put into an object in socketio_scripts.html
                    # and given the __array__ key so that they can be
                    # handled consistently in the on_message event.
                    message_type = "message"
                    if message == "__array__":
                        message = messages.next()
                    events.on_message.send(request, socket, context, message)
                log_message = format_log(request, message_type, message)
                if log_message:
                    socket.handler.server.log.write(log_message)
    except Exception as exception:
        from traceback import print_exc
        print_exc()
        events.on_error.send(request, socket, context, exception)
    client_end(request, socket, context)
    return HttpResponse("")
コード例 #3
0
ファイル: views.py プロジェクト: su-danny/famdates
def socketio(request):
    """
    Socket.IO handler - maintains the lifecycle of a Socket.IO
    request, sending the each of the events. Also handles
    adding/removing request/socket pairs to the CLIENTS dict
    which is used for sending on_finish events when the server
    stops.
    """
    context = {}
    socket = SocketIOChannelProxy(request.environ["socketio"])
    client_start(request, socket, context)
    try:
        if socket.on_connect():
            events.on_connect.send(request, socket, context)
        while True:
            messages = socket.recv()
            if not messages and not socket.connected():
                events.on_disconnect.send(request, socket, context)
                break
                # Subscribe and unsubscribe messages are in two parts, the
            # name of either and the channel, so we use an iterator that
            # lets us jump a step in iteration to grab the channel name
            # for these.
            messages = iter(messages)
            for message in messages:
                if message == "__subscribe__":
                    message = messages.next()
                    message_type = "subscribe"
                    socket.subscribe(message)
                    events.on_subscribe.send(request, socket, context, message)
                elif message == "__unsubscribe__":
                    message = messages.next()
                    message_type = "unsubscribe"
                    socket.unsubscribe(message)
                    events.on_unsubscribe.send(request, socket, context, message)
                else:
                    # Socket.IO sends arrays as individual messages, so
                    # they're put into an object in socketio_scripts.html
                    # and given the __array__ key so that they can be
                    # handled consistently in the on_message event.
                    message_type = "message"
                    if message == "__array__":
                        message = messages.next()
                    events.on_message.send(request, socket, context, message)
                log_message = format_log(request, message_type, message)
                print log_message
                if log_message:
                    socket.handler.server.log.write(log_message)
    except Exception, exception:
        from traceback import print_exc

        print_exc()
        events.on_error.send(request, socket, context, exception)
コード例 #4
0
    def __call__(self, server, request, method):
        context = {}
        socket = SocketIOChannelProxy(request.environ["socketio"])
        CLIENTS[socket.session.session_id] = (request, socket, context)

        client_start(request, socket, context)
        try:
            if socket.on_connect():
                events.on_connect.send(request, socket, context)

            while True:
                message = socket.recv()
                if not messages and not socket.connected():
                    events.on_disconnect.send(request, socket, context)
                    break
                # Subscribe and unsubscribe messages are in two parts, the
                # name of either and the channel, so we use an iterator that
                # lets us jump a step in iteration to grab the channel name
                # for these.
                messages = iter(messages)
                for message in messages:
                    if message == "__subscribe__":
                        message = messages.next()
                        message_type = "subscribe"
                        socket.subscribe(message)
                        events.on_subscribe.send(request, socket, context, message)
                    elif message == "__unsubscribe__":
                        message = messages.next()
                        message_type = "unsubscribe"
                        socket.unsubscribe(message)
                        events.on_unsubscribe.send(request, socket, context, message)
                    else:
                        # Socket.IO sends arrays as individual messages, so
                        # they're put into an object in socketio_scripts.html
                        # and given the __array__ key so that they can be
                        # handled consistently in the on_message event.
                        message_type = "message"
                        if message == "__array__":
                            message = messages.next()
                        events.on_message.send(request, socket, context, message)
        except Exception, exception:
            events.on_error.send(request, socket, context, exception)
            raise exception
コード例 #5
0
def socketio(request):
    """
    Socket.IO handler - maintains the lifecycle of a Socket.IO
    request, sending the each of the events. Also handles
    adding/removing request/socket pairs to the CLIENTS dict
    which is used for sending on_finish events when the server
    stops.
    """
    context = {}
    socket = SocketIOChannelProxy(request.environ["socketio"])
    CLIENTS[socket.session.session_id] = (request, socket, context)
    try:
        if socket.on_connect():
            events.on_connect.send(request, socket, context)
        while True:
            message = socket.recv()
            if len(message) > 0:
                if MESSAGE_LOG_FORMAT is not None:
                    socket.handler.server.log.write(
                        format_log(request, message))
                if message[0] == "__subscribe__" and len(message) == 2:
                    socket.subscribe(message[1])
                    events.on_subscribe.send(request, socket, context,
                                             message[1])
                elif message[0] == "__unsubscribe__" and len(message) == 2:
                    events.on_unsubscribe.send(request, socket, context,
                                               message[1])
                    socket.unsubscribe(message[1])
                else:
                    events.on_message.send(request, socket, context, message)
            else:
                if not socket.connected():
                    events.on_disconnect.send(request, socket, context)
                    break
    except Exception, exception:
        print_exc()
        events.on_error.send(request, socket, context, exception)