Introduction

Installation

Frequently asked questions

Syntax, keywords and built-in functions

Standard distribution

import implementation

Browser interface

Brython-specific built-in modules

Working with Brython

Brython for Firefox OS

Cookbook

 

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 alert, document, websocket

def on_open(evt):
    document['sendbtn'].disabled = False
    document['closebtn'].disabled = False
    document['openbtn'].disabled = True

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

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

ws = None

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

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

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