예제 #1
0
def message(request, socket, context, message):
	"""
    Event handler for a room receiving a message. First validates a
    joining user's name and sends them the list of users.
	"""
	room_id = message["room"]
	room = chat_db.where("chat_chatroom", id = room_id)

	if message["action"] == "start":
		raw_user_name = message["name"]
		user_name =  base64.b64decode(raw_user_name).decode("utf_8")
		user = chat_db.where("chat_chatuser",name=user_name)
		if user:
			socket.send({"action": "in-use"})
		else:

			users = [base64.b64encode(u.name.encode("utf_8")) for u in chat_db.select("chat_chatuser",dict(room_id=room_id),where="room_id=$room_id")]			
			socket.send({"action": "started", "users": users})
			
			user_session = socket.session.session_id
			user_id = chat_db.insert("chat_chatuser",name=user_name,room_id=room_id,session=user_session)
			context["user"] = {"id":user_id,"name":raw_user_name,"room_id":room_id,"session":user_session}
			
			joined = {"action": "join", "name": context["user"]["name"], "id": context["user"]["id"]}
			socket.send_and_broadcast_channel(joined)
	else:
		try:
			user = context["user"]
		except KeyError:
			return
		if message["action"] == "message":		
			message["name"] = user["name"]
			socket.send_and_broadcast_channel(message)
예제 #2
0
	def GET(myself,slug):
		"""
		Show a room.
		"""
		rooms = chat_db.select("chat_chatroom",dict(slug=slug),where="slug = $slug")
		for room in rooms:
			return my_render.render("room",room=room)
		raise web.notfound()
예제 #3
0
    def GET(myself, slug):
        """
		Show a room.
		"""
        rooms = chat_db.select("chat_chatroom",
                               dict(slug=slug),
                               where="slug = $slug")
        for room in rooms:
            return my_render.render("room", room=room)
        raise web.notfound()
예제 #4
0
def message(request, socket, context, message):
    """
    Event handler for a room receiving a message. First validates a
    joining user's name and sends them the list of users.
	"""
    room_id = message["room"]
    room = chat_db.where("chat_chatroom", id=room_id)

    if message["action"] == "start":
        raw_user_name = message["name"]
        user_name = base64.b64decode(raw_user_name).decode("utf_8")
        user = chat_db.where("chat_chatuser", name=user_name)
        if user:
            socket.send({"action": "in-use"})
        else:

            users = [
                base64.b64encode(u.name.encode("utf_8"))
                for u in chat_db.select("chat_chatuser",
                                        dict(room_id=room_id),
                                        where="room_id=$room_id")
            ]
            socket.send({"action": "started", "users": users})

            user_session = socket.session.session_id
            user_id = chat_db.insert("chat_chatuser",
                                     name=user_name,
                                     room_id=room_id,
                                     session=user_session)
            context["user"] = {
                "id": user_id,
                "name": raw_user_name,
                "room_id": room_id,
                "session": user_session
            }

            joined = {
                "action": "join",
                "name": context["user"]["name"],
                "id": context["user"]["id"]
            }
            socket.send_and_broadcast_channel(joined)
    else:
        try:
            user = context["user"]
        except KeyError:
            return
        if message["action"] == "message":
            message["name"] = user["name"]
            socket.send_and_broadcast_channel(message)
예제 #5
0
	def GET(myself):
		rooms = chat_db.select("chat_chatroom")
		message = ""
		selected_room = ""
		return my_render.render("system_message",**locals())
예제 #6
0
	def GET(myself):
		"""
		Homepage - lists all rooms.
		"""
		rooms = chat_db.select('chat_chatroom')
		return my_render.render("rooms",**locals())
예제 #7
0
		message = ""
		selected_room = ""
		return my_render.render("system_message",**locals())
		
	@csrf_protected
	def POST(myself):
		input_data = web.input()
		selected_room = input_data["room"]
		data = {"action": "system", "message": input_data["message"]}
		try:
			if selected_room:
				broadcast_channel(data, channel="room-" + selected_room)
			else:
				broadcast(data)
		except NoSocket, e:
			message = e
		else:
			message = "Message sent"
		rooms = chat_db.select("chat_chatroom")
		return my_render.render("system_message",**locals())

SOCKETIO_HOST = ""
SOCKETIO_PORT = 8000
application = app.wsgifunc()

import events

if __name__ == "__main__":
	print 'Listening on http://127.0.0.1:%s and on port 843 (flash policy server)' % SOCKETIO_PORT
	SocketIOServer((SOCKETIO_HOST, SOCKETIO_PORT), application, resource="socket.io").serve_forever()
예제 #8
0
    def GET(myself):
        """
		Homepage - lists all rooms.
		"""
        rooms = chat_db.select('chat_chatroom')
        return my_render.render("rooms", **locals())
예제 #9
0
 def GET(myself):
     rooms = chat_db.select("chat_chatroom")
     message = ""
     selected_room = ""
     return my_render.render("system_message", **locals())
예제 #10
0
    @csrf_protected
    def POST(myself):
        input_data = web.input()
        selected_room = input_data["room"]
        data = {"action": "system", "message": input_data["message"]}
        try:
            if selected_room:
                broadcast_channel(data, channel="room-" + selected_room)
            else:
                broadcast(data)
        except NoSocket, e:
            message = e
        else:
            message = "Message sent"
        rooms = chat_db.select("chat_chatroom")
        return my_render.render("system_message", **locals())


SOCKETIO_HOST = ""
SOCKETIO_PORT = 8000
application = app.wsgifunc()

import events

if __name__ == "__main__":
    print 'Listening on http://127.0.0.1:%s and on port 843 (flash policy server)' % SOCKETIO_PORT
    SocketIOServer((SOCKETIO_HOST, SOCKETIO_PORT),
                   application,
                   resource="socket.io").serve_forever()