Skip to content

Commit

Permalink
feat(ws-tutorial): add web page used by the echo client
Browse files Browse the repository at this point in the history
  • Loading branch information
CaselIT committed Jul 25, 2024
1 parent 4226ddd commit 8ca7ba2
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/user/tutorial-websockets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ Things we've changed:
- Added a new middleware class `AuthMiddleware` that will check the token on the first message.
- Opening a websocket connection is now handled by the middleware.
- The client now sends a token as the first message, if required for that route.
- Falcon was configured to server a simple html page to use the echo websocket client for a browser.

If you try to query the reports endpoint now, everything works as expected on an
authenticated route.
Expand Down
6 changes: 6 additions & 0 deletions examples/wslook/wslook/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime
import logging
import pathlib

import uvicorn

Expand Down Expand Up @@ -115,5 +116,10 @@ async def on_websocket(self, req: Request, ws: WebSocket):
app.add_middleware(LoggerMiddleware())
app.add_middleware(AuthMiddleware(['/reports']))

# usually a web server, like Nginx or Caddy, should server static assets, but
# for the purpose of this example we use falcon.
static_path = pathlib.Path(__file__).parent / 'static'
app.add_static_route('/', static_path, fallback_filename='index.html')

if __name__ == '__main__':
uvicorn.run(app, host='localhost', port=8000) # pragma: no cover
80 changes: 80 additions & 0 deletions examples/wslook/wslook/static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<!DOCTYPE html>
<html>

<head>
<title>WebSocket Echo</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
}

#messageForm {
margin-bottom: 1em;
}

#history {
width: 500px;
height: 200px;
border: 1px solid #ccc;
padding: 1em;
overflow-y: scroll;
background-color: #fff;
}
</style>
<script type="text/javascript">
window.onload = () => {
const historyEl = document.getElementById('history');
const messageEl = document.getElementById('message')
// Create a new WebSocket.
const socket = new WebSocket('echo');

// Connection opened
socket.addEventListener('open', event => {
historyEl.textContent += 'Connected to server\n';
});

// Listen for messages
socket.addEventListener('message', event => {
historyEl.textContent += 'Server: ' + event.data + '\n';
});

// Connection closed
socket.addEventListener('close', event => {
historyEl.textContent += 'Server connection closed\n';
});

// Connection error
socket.addEventListener('error', event => {
console.log('Error: ', event);
});

// Send message to server on form submit
const form = document.getElementById('messageForm');
form.addEventListener('submit', event => {
event.preventDefault();
const message = messageEl.value
if (!message) {
return;
}
socket.send(message);
historyEl.textContent += `You: ${message}\n`;
messageEl.value = '';
});
};
</script>
</head>

<body>
<h1>WebSocket Echo Test</h1>
<form id="messageForm">
<input type="text" id="message" placeholder="Type your message here">
<input type="submit" value="Send">
</form>
<pre id="history"></pre>
</body>

</html>

0 comments on commit 8ca7ba2

Please sign in to comment.