aboutsummaryrefslogtreecommitdiffstats
path: root/pysite/views/ws/rst.py
blob: c85c10a0d1cbaba12e1c67f3dc685d924df30d77 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# coding=utf-8
import logging

from docutils.core import publish_parts
from geventwebsocket.websocket import WebSocket

from pysite.websockets import WS


class RSTWebsocket(WS):
    path = "/rst"
    name = "ws.rst"

    def __init__(self, socket: WebSocket):
        super().__init__(socket)
        self.log = logging.getLogger()

    def on_open(self):
        self.log.debug("RST | WS opened.")
        self.send("Hey, welcome!")

    def on_message(self, message):
        self.log.debug(f"RST | Message: {message}")
        self.send(publish_parts(source=message, writer_name="html5")["html_body"])

    def on_close(self):
        self.log.debug("RST | WS closed.")