diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d20b64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/.idea/dbnavigator.xml b/.idea/dbnavigator.xml new file mode 100644 index 0000000..792a196 --- /dev/null +++ b/.idea/dbnavigator.xml @@ -0,0 +1,445 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..f437c36 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..6dbb45e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/python-websocket-server.iml b/.idea/python-websocket-server.iml new file mode 100644 index 0000000..e85f674 --- /dev/null +++ b/.idea/python-websocket-server.iml @@ -0,0 +1,11 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..3e2749b --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,208 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + users + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1486049999616 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/server.py b/server.py index f0587c6..fba2c1d 100644 --- a/server.py +++ b/server.py @@ -1,24 +1,79 @@ from websocket_server import WebsocketServer +import json # Called for every client connecting (after handshake) def new_client(client, server): - print("New client connected and was given id %d" % client['id']) - server.send_message_to_all("Hey all, a new client has joined us") + print("New client connected and was given id %d" % client['id']) + server.send_message_to_all("Hey all, a new client has joined us") # Called for every client disconnecting def client_left(client, server): - print("Client(%d) disconnected" % client['id']) + print("Client(%d) disconnected" % client['id']) # Called when a client sends a message def message_received(client, server, message): - if len(message) > 200: - message = message[:200]+'..' - print("Client(%d) said: %s" % (client['id'], message)) + msg = json.loads(message) + incoming_action = str(msg['action']) -PORT=9001 + if incoming_action == 'USER_SAYS': + server.send_message_to_all(message) + + elif incoming_action == "SIGN_IN": + + new_user = { + 'username': str(msg['user']), + 'room': str(msg['room']) + } + + outgoing_socket_message = { + 'action': "USER_IN", + 'message': str(msg['message']), + 'room': str(msg['room']), + 'user': str(msg['user']) + } + print "avisando outros: %s" % json.dumps(outgoing_socket_message) + server.send_message_to_all(json.dumps(outgoing_socket_message)) + + + + elif incoming_action == "SIGN_OUT": + + outgoing_socket_message = { + 'action': "USER_OUT", + 'message': "", + 'room': msg['room'], + 'user': msg['user'] + } + + server.send_message_to_all(json.dumps(outgoing_socket_message)) + client_left(client, server) + + print json.dumps(outgoing_socket_message) + + elif incoming_action == "REQUEST_USERS": + + outgoing_socket_message = { + 'action': "ALL_USERS", + 'message': 'user1,user2,user3', + 'room': "", + 'user': "" + } + print "Informando usuarios existentes: %s" % json.dumps(outgoing_socket_message) + server.send_message(client, json.dumps(outgoing_socket_message)) + pass + + else: + print "I don't know what you mean!" + + if len(message) > 200: + message = message[:200]+'..' + print("Client(%d) said: %s" % (client['id'], message)) + + +PORT = 9001 server = WebsocketServer(PORT) server.set_fn_new_client(new_client) server.set_fn_client_left(client_left)