About
Websockets in the Mys programming language.
Examples
Client side
from websocket.client import Client
func main():
client = Client()
client.connect("localhost", 20000)
client.send_text("Hello!")
print(client.receive_text())
client.disconnect()
Server side
Warning
Server side websockets are not yet implemented!
from websocket.server import Server
func main():
server = Server()
server.listen(20000)
while True:
client = server.accept()
message = client.receive_text()
print(f"Got: {message}")
client.send_text(f"Message: '{message}'")
API
Client side
trait Handler:
All methods are called from the client's reader fiber.
func on_binary(self, data: bytes):
Called when a binary message has been received from the server.
func on_text(self, data: string):
Called when a text message has been received from the server.
func on_disconnected(self):
Called when disconnected by the server.
class Client:
A websocket client, used to communicate with a websocket server.
func __init__(self, handler: Handler? = None, secure: bool = False):
Create a client. Give `handler` as ``None``` to use the default
handler which puts received messages on message queues that
are read from with the `receive_binary()` and `receive_text()`
methods.
func connect(self, host: string, port: i64, path: string = "/"):
Connect to the server identified by given `host` and
`port`. Non-secure websockets normally use port 80, while
secure use port 443.
`path` in the path as sent in the HTTP request to the
server. For example "/info/299?name=Kalle&date=2021-03-01".
func disconnect(self):
Disconnect from the server.
func send_binary(self, data: bytes):
Send `data` to the server as a binary message.
This method never blocks, but instead enqueues the message if
the OS would block the write.
func send_text(self, data: string):
Send `data` to the server as a text message.
This method never blocks, but instead enqueues the message if
the OS would block the write.
func receive_binary(self) -> bytes:
Receive a binary message from the server. This method can only be
used if no handler was passed to __init__(). Raises and error if
disconnected.
func receive_text(self) -> string:
Receive a text message from the server. This method can only be
used if no handler was passed to __init__(). Raises and error if
disconnected.
Server side
Warning
Server side websockets are not yet implemented!
class Client:
An accepted websocket client.
`path` is the path received in the HTTP request from the client.
path: string?
func __init__(self, tcp_client: TcpClient):
func is_connected(self) -> bool:
func send_binary(self, data: bytes):
Send given data to the client as a binary message.
func send_text(self, data: string):
Send given data to the client as a text message.
func receive_binary(self) -> bytes?:
Receive a binary message from the client. Returns None if
disconnected.
func receive_text(self) -> string?:
Receive a text message from the client. Returns None if
disconnected.
class Server:
A websocket server, used to communicate with websocket clients.
func __init__(self):
func listen(self, port: i64):
Start listening for clients to connect to given `port` on any
interface.
func accept(self) -> Client:
Wait for a client to connect and accept it.