You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fromteaimportTeaapp=Tea()
ws=app.websocket("/ws")
# handle new connectiondefhandle_open(e):
print("New client connected!")
ws.onopen=handle_open# handle message from clientdefhandle_message(e):
msg=e.dataprint("Message from client:", msg)
# send message to client who sent the messagee.client.write("Hi, got your message: "+msg)
# send message to all active clientsforclientinws.get_clients():
# except the client who sent the messageifclient.id!=e.client.id:
client.write("Client sent message: "+msg)
ws.onmessage=handle_message# handle client disconnectiondefhandle_close(e):
print("Client disconnected!")
ws.onclose=handle_closeapp.listen(port=8080)
Concepts
Handle Different Request Methods
fromteaimportTeaapp=Tea()
# specific methods on same pathdefindex_all(req, res):
print("GET Request")
# response somethingapp.get("/", index_all)
defindex_post(req, res):
print("POST Request")
# response somethingapp.post("/", index_post)
defindex_put(req, res):
print("PUT Request")
# response somethingapp.put("/", index_put)
defindex_delete(req, res):
print("DELETE Request")
# response somethingapp.delete("/", index_delete)
# all valid methods on the path# Note: if set specific method on same path with .all() then specific method will be calleddefindex_all(req, res):
ifreq.method=="GET":
# act like app.get()elifreq.method=="DELETE":
# act like app.delete()app.all("/admin", index_all)
app.listen()
fromteaimportTeaapp=Tea()
# serve static folder on pathapp.serve_static("/", "/docs")
"""if your folder structure like this:/docs├── index.html└── /assets ├── /css │ └── style.css └── /js └── script.jsGET request to path "/index.html" will return "/docs/index.html" file andGET request to path "/assets/css/style.css" will return "/docs/assets/css/style.css" file."""# you can server multipe static foldersapp.serve_static("/www", "/static")
# send a single filedefsend_posts(req, res):
res.send_file("posts.html")
app.get("/posts.html", send_posts)
app.listen()
API Reference
Tea Class
Method
Description
Example
Tea()
Create new app.
app = Tea()
.serve_static(path: str, folder_path: str)
Serve static files in specific folder on given path.
Send file as response inside the callback function with auto content type.
res.send_file("index.html")
.get_res_as_text()
Get raw response text as string.
res_text = res.get_res_as_text()
Property
Description
Example
.status_code
Response status code. (Changable with .send() and .send_file())
.status_message
Response status message automaticly from status code.
.content_type
Response content type. (Changable with .send() and .send_file())
application/json and json both valid as parameter.
.headers
Response headers as dict.
.body
Response body.
WebsocketServer Class
Method
Description
Example
.get_clients()
Get all active clients in the websocket server. Return list of WebsocketClient object.
clients = ws.get_clients()
Property
Description
Example
.onopen
Callback function for new connection. Return WebsocketServer.Event object.
ws.onopen = onopen
.onmessage
Callback function for new message.Return WebsocketServer.Event object.
ws.onmessage = onmessage
.onclose
Callback function for connection close.Return WebsocketServer.Event object.
ws.onclose = onclose
WebsocketServer.Event Class
Property
Description
Example
.client
WebsocketClient object of the event.
client = e.client
.ready_state
Websocket ready state. 1 if open, 3 if closed.
ready_state = e.ready_state
.data
Readed data from websocket client. None if it's outside the .onmessage callback.
data = e.data
WebsocketClient Class
Method
Description
Example
.write()
Write message to websocket client.
client.write("hello, world!")
Property
Description
Example
.id
Random 15 digit id of client.
client.id
⚠ Keep in Mind
You can't create WebsocketServer and WebsocketClient objects directly in your code. You need to create a websocket server with ws = app.websocket() as mentioned in Tea object. For websocket clients you can use:
ws=app.websocket("/ws")
clients=ws.get_clients() # this or ...forclientinclients:
# do something with clientdefonopen(e):
client=e.client# ... something like thisws.onopen=onopen
Limitations of WebSocket in Tea (for now, under development):
You can read and write only string data.
You can't pass parameters to path.
ws=app.websocket("/ws/:username") # not valid
You can create just one websocket server in one app.
ws1=app.websocket("/ws1") # not validws2=app.websocket("/ws2") # last one becomes valid
You can't use another methods like .get(), .post() etc. on websocket path.
ws=app.websocket("/ws")
app.get("/ws", handle_ws) # not valid
URL Class
Property
Description
Return Example
URL(url: str)
Parse url and create new URL object. Tea class for simplify URL stuff.