refactor quotes

This commit is contained in:
Lexi / Zoe 2018-11-24 03:45:12 +01:00
parent 733a537f58
commit fd6a40cb0d
2 changed files with 18 additions and 16 deletions

View File

@ -1,5 +1,5 @@
// Constants and variables // Constants and variables
//const wsUri = "ws://localhost:32715"; // const wsUri = "ws://localhost:32715";
const wsUri = "wss://chat.glitch-in.space:443/ws/"; const wsUri = "wss://chat.glitch-in.space:443/ws/";
let websocket; let websocket;
@ -21,7 +21,7 @@ function openWebSocket() {
// WebSocket event handlers // WebSocket event handlers
function onOpen(evt) { function onOpen(evt) {
console.log('Connected to ' + wsUri + '.'); console.log("Connected to " + wsUri + ".");
// Send init command containing chat ID and nickname // Send init command containing chat ID and nickname
sendInit(); sendInit();
@ -32,12 +32,12 @@ function onClose(evt) {
} }
function onMessage(evt) { function onMessage(evt) {
console.log('Received: "' + evt.data + '".'); console.log("Received: " + evt.data);
parseMessage(evt.data); parseMessage(evt.data);
} }
function onError(evt) { function onError(evt) {
console.error('Connection error: ', evt); console.error("Connection error: ", evt);
} }
// Command senders // Command senders
@ -51,7 +51,7 @@ function sendInit() {
// Send command as JSON string // Send command as JSON string
let initJson = JSON.stringify(initObj); let initJson = JSON.stringify(initObj);
console.log('Sending init: ' + initJson); console.log("Sending init: " + initJson);
websocket.send(initJson); websocket.send(initJson);
} }
@ -61,26 +61,26 @@ function parseMessage(msgString) {
let msg = JSON.parse(msgString); let msg = JSON.parse(msgString);
switch (msg.type) { switch (msg.type) {
// Response to 'init' command // Response to "init" command
case 'init': case "init":
// TODO // TODO
console.log('Got init response: ', msg); console.log("Got init response: ", msg);
break; break;
// Incoming chat message // Incoming chat message
case 'message': case "message":
// TODO // TODO
console.log('Got message event: from "' + msg.from + '", text "' + msg.text + '"'); console.log("Got message event: from '" + msg.from + "', text '" + msg.text + "'");
break; break;
// TODO Topic change, user join/leave, error, ... // TODO Topic change, user join/leave, error, ...
default: default:
console.error('Unknown message type: "' + msg.type + '"'); console.error("Unknown message type '" + msg.type + "'");
} }
} }
catch (e) { catch (e) {
console.error('Error parsing message JSON: ' + e.message); console.error("Error parsing message JSON: " + e.message);
} }
} }

View File

@ -31,6 +31,8 @@ async def parse_client_message(websocket, message_text):
async def handle_client_init(websocket, message): async def handle_client_init(websocket, message):
"""Handle client 'init' message.""" """Handle client 'init' message."""
# TODO input check for nickname
print(f"< init: chat_id='{message['chat_id']}', nickname='{message['nickname']}'") print(f"< init: chat_id='{message['chat_id']}', nickname='{message['nickname']}'")
await send_client_init_response(websocket) await send_client_init_response(websocket)
await send_client_previous_messages(websocket) await send_client_previous_messages(websocket)
@ -40,7 +42,7 @@ async def send_client_init_response(websocket):
"""Send an init response message to a newly connected client.""" """Send an init response message to a newly connected client."""
response = json.dumps({ response = json.dumps({
'type': 'init' "type": "init"
}) })
print(f"> {response}") print(f"> {response}")
await websocket.send(response) await websocket.send(response)
@ -52,9 +54,9 @@ async def send_client_previous_messages(websocket):
# For now: send test messages # For now: send test messages
for i in range(1, 4): for i in range(1, 4):
testmsg = json.dumps({ testmsg = json.dumps({
'type': 'message', "type": "message",
'from': 'Alice', "from": "Alice",
'text': f"Message number {i}, hello!" "text": f"Message number {i}, hello!"
}) })
print(f"> {testmsg}") print(f"> {testmsg}")
await websocket.send(testmsg) await websocket.send(testmsg)