refactor quotes
This commit is contained in:
parent
733a537f58
commit
fd6a40cb0d
|
|
@ -1,5 +1,5 @@
|
|||
// Constants and variables
|
||||
//const wsUri = "ws://localhost:32715";
|
||||
// const wsUri = "ws://localhost:32715";
|
||||
const wsUri = "wss://chat.glitch-in.space:443/ws/";
|
||||
let websocket;
|
||||
|
||||
|
|
@ -21,7 +21,7 @@ function openWebSocket() {
|
|||
|
||||
// WebSocket event handlers
|
||||
function onOpen(evt) {
|
||||
console.log('Connected to ' + wsUri + '.');
|
||||
console.log("Connected to " + wsUri + ".");
|
||||
|
||||
// Send init command containing chat ID and nickname
|
||||
sendInit();
|
||||
|
|
@ -32,12 +32,12 @@ function onClose(evt) {
|
|||
}
|
||||
|
||||
function onMessage(evt) {
|
||||
console.log('Received: "' + evt.data + '".');
|
||||
console.log("Received: " + evt.data);
|
||||
parseMessage(evt.data);
|
||||
}
|
||||
|
||||
function onError(evt) {
|
||||
console.error('Connection error: ', evt);
|
||||
console.error("Connection error: ", evt);
|
||||
}
|
||||
|
||||
// Command senders
|
||||
|
|
@ -51,7 +51,7 @@ function sendInit() {
|
|||
|
||||
// Send command as JSON string
|
||||
let initJson = JSON.stringify(initObj);
|
||||
console.log('Sending init: ' + initJson);
|
||||
console.log("Sending init: " + initJson);
|
||||
websocket.send(initJson);
|
||||
}
|
||||
|
||||
|
|
@ -61,26 +61,26 @@ function parseMessage(msgString) {
|
|||
let msg = JSON.parse(msgString);
|
||||
|
||||
switch (msg.type) {
|
||||
// Response to 'init' command
|
||||
case 'init':
|
||||
// Response to "init" command
|
||||
case "init":
|
||||
// TODO
|
||||
console.log('Got init response: ', msg);
|
||||
console.log("Got init response: ", msg);
|
||||
break;
|
||||
|
||||
// Incoming chat message
|
||||
case 'message':
|
||||
case "message":
|
||||
// TODO
|
||||
console.log('Got message event: from "' + msg.from + '", text "' + msg.text + '"');
|
||||
console.log("Got message event: from '" + msg.from + "', text '" + msg.text + "'");
|
||||
break;
|
||||
|
||||
// TODO Topic change, user join/leave, error, ...
|
||||
|
||||
default:
|
||||
console.error('Unknown message type: "' + msg.type + '"');
|
||||
console.error("Unknown message type '" + msg.type + "'");
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
console.error('Error parsing message JSON: ' + e.message);
|
||||
console.error("Error parsing message JSON: " + e.message);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -31,6 +31,8 @@ async def parse_client_message(websocket, message_text):
|
|||
async def handle_client_init(websocket, message):
|
||||
"""Handle client 'init' message."""
|
||||
|
||||
# TODO input check for nickname
|
||||
|
||||
print(f"< init: chat_id='{message['chat_id']}', nickname='{message['nickname']}'")
|
||||
await send_client_init_response(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."""
|
||||
|
||||
response = json.dumps({
|
||||
'type': 'init'
|
||||
"type": "init"
|
||||
})
|
||||
print(f"> {response}")
|
||||
await websocket.send(response)
|
||||
|
|
@ -52,9 +54,9 @@ async def send_client_previous_messages(websocket):
|
|||
# For now: send test messages
|
||||
for i in range(1, 4):
|
||||
testmsg = json.dumps({
|
||||
'type': 'message',
|
||||
'from': 'Alice',
|
||||
'text': f"Message number {i}, hello!"
|
||||
"type": "message",
|
||||
"from": "Alice",
|
||||
"text": f"Message number {i}, hello!"
|
||||
})
|
||||
print(f"> {testmsg}")
|
||||
await websocket.send(testmsg)
|
||||
|
|
|
|||
Loading…
Reference in New Issue