Skip to content
Permalink
Browse files
Add new broker functions.
  • Loading branch information
huaqitian committed May 16, 2024
1 parent dfd858e commit 7f3a5cc6f0fa04e0c77798fa0e71f4f6bb0dd29f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
@@ -74,23 +74,34 @@ python client.py

You can schedule messages to be broadcasted at specific times using the HTTP API provided by the server.

### Example
## Example
Schedule a Message
To schedule a message, send a POST request to http://localhost:8000/schedule with a JSON payload containing the time and message:

To schedule a message, send a POST request to `http://localhost:8000` with a JSON payload containing the time and message:

```json
```bash
{
"time": "2024-05-15 10:00:00",
"message": "This is a scheduled broadcast message."
}
```
Example using curl:

Example using `curl`:
```bash
curl -X POST http://localhost:8000/schedule -H "Content-Type: application/json" -d '{"time":"2024-05-15 10:00:00", "message":"This is a scheduled broadcast message."}'
```
Broadcast a Message Immediately
To immediately broadcast a message, send a POST request to http://localhost:8000/broadcast with a JSON payload containing the message:

```bash
curl -X POST http://localhost:8000 -H "Content-Type: application/json" -d '{"time":"2024-05-15 10:00:00", "message":"This is a scheduled broadcast message."}'
{
"message": "This is an immediate broadcast message."
}
```
Example using curl:

```bash
curl -X POST http://localhost:8000/broadcast -H "Content-Type: application/json" -d '{"message":"This is a immediate broadcast message."}'
```
### API Details

- **Endpoint**: `http://localhost:8000`
@@ -63,23 +63,37 @@ def periodic_broadcast():
time.sleep(1)

class RequestHandler(BaseHTTPRequestHandler):
"""Handle HTTP requests for setting scheduled messages and times."""
"""Handle HTTP requests for setting scheduled messages and immediate broadcasts."""

def do_POST(self):
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
try:
data = json.loads(post_data)
global scheduled_messages
scheduled_messages.append({
"time": data["time"],
"message": data["message"]
})
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
response = {"status": "success", "message": "Scheduled message set"}
self.wfile.write(json.dumps(response).encode())
if self.path == "/schedule":
global scheduled_messages
scheduled_messages.append({
"time": data["time"],
"message": data["message"]
})
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
response = {"status": "success", "message": "Scheduled message set"}
self.wfile.write(json.dumps(response).encode())
elif self.path == "/broadcast":
broadcast(data["message"])
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
response = {"status": "success", "message": "Message broadcasted immediately"}
self.wfile.write(json.dumps(response).encode())
else:
self.send_response(404)
self.send_header('Content-Type', 'application/json')
self.end_headers()
response = {"status": "error", "message": "Endpoint not found"}
self.wfile.write(json.dumps(response).encode())
except json.JSONDecodeError:
self.send_response(400)
self.send_header('Content-Type', 'application/json')

0 comments on commit 7f3a5cc

Please sign in to comment.