-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
put webpage in fastapi template (#17)
- Loading branch information
Showing
5 changed files
with
132 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>gischat</title> | ||
</head> | ||
<body> | ||
<h1>gischat websocket client</h1> | ||
<form action="" onsubmit="sendMessage(event)"> | ||
<label>Instance: <input type="text" id="instance" autocomplete="off" value=""/></label> | ||
<label>SSL: <input type="checkbox" id="ssl" checked/></label> | ||
<button onclick="onRulesButtonClick(event)">Rules</button> | ||
<button onclick="onStatusButtonClick(event)">Status</button> | ||
<hr> | ||
<label>Room: <input type="text" id="roomId" autocomplete="off" value=""/></label> | ||
<button onclick="onDetectButtonClick(event)">Detect</button> | ||
<button id="connectButton" onclick="onConnectButtonClick(event)">Connect</button> | ||
<hr> | ||
<label>Author: <input type="text" id="authorId" autocomplete="off" value=""/></label> | ||
<label>Message: <input type="text" id="messageText" autocomplete="off"/></label> | ||
<button>Send</button> | ||
</form> | ||
<hr> | ||
<ul id='messages'> | ||
</ul> | ||
<script> | ||
let websocket = null; | ||
let connected = false; | ||
const instance = document.getElementById('instance'); | ||
instance.value = window.location.host; | ||
const ssl = document.getElementById("ssl"); | ||
ssl.checked = window.location.protocol.startsWith("https"); | ||
const connectButton = document.getElementById('connectButton'); | ||
|
||
function displayMessage(msg) { | ||
const messages = document.getElementById('messages'); | ||
const message = document.createElement('li'); | ||
console.log(msg); | ||
const content = document.createTextNode(msg); | ||
message.appendChild(content); | ||
messages.appendChild(message); | ||
} | ||
|
||
function onRulesButtonClick(event) { | ||
const protocol = ssl.checked ? "https" : "http"; | ||
fetch(`${protocol}://${instance.value}/rules`) | ||
.then(response => response.json()) | ||
.then(data => displayMessage(`Instance rules: ${data.rules}`)); | ||
event.preventDefault(); | ||
} | ||
|
||
function onStatusButtonClick(event) { | ||
const protocol = ssl.checked ? "https" : "http"; | ||
fetch(`${protocol}://${instance.value}/status`) | ||
.then(response => response.json()) | ||
.then(data => displayMessage(`Instance status: ${data.status} - users in rooms: ${data.rooms.map(room => `'${room.name}': ${room.nb_connected_users}`).join(",")}`)); | ||
event.preventDefault(); | ||
} | ||
|
||
function onDetectButtonClick(event) { | ||
const protocol = ssl.checked ? "https" : "http"; | ||
fetch(`${protocol}://${instance.value}/rooms`) | ||
.then(response => response.json()) | ||
.then(data => displayMessage(`Available rooms: ${data.join(',')}`)); | ||
event.preventDefault(); | ||
} | ||
|
||
function onConnectButtonClick(event) { | ||
if (connected) { | ||
websocket.close(); | ||
event.preventDefault(); | ||
return; | ||
} | ||
const room = document.getElementById("roomId"); | ||
if (!room.value) { | ||
alert("Room must be set"); | ||
return; | ||
} | ||
const ws_protocol = ssl.checked ? "wss" : "ws"; | ||
const ws_url = `${ws_protocol}://${instance.value}/room/${room.value}/ws`; | ||
websocket = new WebSocket(ws_url); | ||
websocket.onopen = (event) => { | ||
displayMessage(`Connected to websocket in room ${room.value}`); | ||
connected = true; | ||
connectButton.innerText = "Disconnect"; | ||
} | ||
websocket.onmessage = (event) => { | ||
const data = JSON.parse(event.data); | ||
const log = `[${data.author}] (${new Date().toLocaleTimeString()}): ${data.message}`; | ||
displayMessage(log); | ||
}; | ||
websocket.onerror = (error) => { | ||
displayMessage(`Websocket error ${JSON.stringify(error)}`); | ||
console.log("Websocket error", error); | ||
}; | ||
websocket.onclose = (event) => { | ||
connected = false; | ||
connectButton.innerText = "Connect"; | ||
displayMessage("Disconnected from websocket"); | ||
} | ||
event.preventDefault(); | ||
} | ||
|
||
function sendMessage(event) { | ||
if (!connected) { | ||
displayMessage("Not connected to websocket"); | ||
event.preventDefault(); | ||
return; | ||
} | ||
const message = document.getElementById("messageText"); | ||
const author = document.getElementById("authorId"); | ||
if (!message.value || !author.value) { | ||
alert("Author and message can not be empty"); | ||
return; | ||
} | ||
websocket.send(JSON.stringify({message: message.value, author: author.value})); | ||
message.value = ''; | ||
event.preventDefault(); | ||
} | ||
</script> | ||
</body> | ||
</html> |
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters