Introduction

Installation

Limitations of the "file" protocol

Frequently asked questions

Syntax, keywords and built-in functions

Standard distribution

import implementation

Brython packages

Browser interface

Introduction - DOM API
Creating a document
Accessing elements
Attributes, properties and methods

Events
Mouse events
Keyboard events
Focus events
Drag events

Query string

Interactions with Javascript

Brython-specific built-in modules

browser
browser.aio
browser.ajax
browser.html
browser.local_storage
browser.markdown
browser.object_storage
browser.session_storage
browser.svg
browser.template
browser.timer
browser.webcomponent
browser.websocket
browser.worker

Widgets
browser.widgets.dialog
browser.widgets.menu

interpreter
javascript

Working with Brython

Execution options
Testing and debugging
Deploying an application

Cookbook

Hello world !
Insert content in an element
HTML markup (bold,italic...)
HTML table
Bind and unbind events
Handle options in a SELECT
Drag and drop
Get the content of an element
Read the content of a file
Store objects locally
Example of onmouseover
 

module browser.websocket

Web sockets, defined in HTML5, are a way to handle bi directional communication between client and server

The module defines a boolean:

supported
indicates if the protocol is supported by the browser

and a function:

WebSocket(host)
host is the location of a server that supports the WebSocket protocol. Returns a WebSocket object. If the browser doesn't support WebSocket, a NotImplementedError will be raised

WebSocket objects have the following methods :

bind(evt,function)
attaches the function to the event evt. The events and the corresponding function arguments are :

Event Function
open function with no argument, called once the connection with the server is established
error function with no argument, called if an error occurs during the communication
message function that takes one argument, an instance of DOMEvent. This instance has an attribute data that holds the message sent by the server as a string
close function with no argument, called when the connection is closed

send(data)
sends the string data to the server

close()
closes the connection

Example :

from browser import bind, document, websocket
from browser.widgets.dialog import InfoDialog

def on_open(evt):
    document['sendbtn'].disabled = False
    document['closebtn'].disabled = False
    document['openbtn'].disabled = True
    InfoDialog("websocket", f"Connection open")    

def on_message(evt):
    # message received from server
    InfoDialog("websocket", f"Message received : {evt.data}")

def on_close(evt):
    # websocket is closed
    InfoDialog("websocket", "Connection is closed")
    document['openbtn'].disabled = False
    document['closebtn'].disabled = True
    document['sendbtn'].disabled = True

ws = None

@bind('#openbtn', 'click')
def _open(ev):
    if not websocket.supported:
        InfoDialog("websocket", "WebSocket is not supported by your browser")
        return
    global ws
    # open a web socket
    ws = websocket.WebSocket("wss://echo.websocket.events")
    # bind functions to web socket events
    ws.bind('open',on_open)
    ws.bind('message',on_message)
    ws.bind('close',on_close)

@bind('#sendbtn', 'click')
def send(ev):
    data = document["data"].value
    if data:
        ws.send(data)

@bind('#closebtn', 'click')
def close_connection(ev):
    ws.close()
    document['openbtn'].disabled = False