7.4. websocket_client — websocket client module API

7.4.1. Class websocket_client

websocket_client.websocket_client_start(PORT, connect_tuple)

Connect to the websocket Server using the connect_tuple and port. This tuple contains 4 arguments:

  • port - the port of the server which is going to be connected with
  • connect_tuple - (request_uri,host,origin,sec_websocket_protocol)
  • request_uri - the request uri
  • host - the host address
  • origin - origin
  • sec_websocket_protocol - secondary websocket protocol
websocket_client.websocket_send(send_tuple)

Send message to the server using the send_tuple The format of send_tuple is: (final_frame, payload_type, payload, payload_length, payload_buffer_size)

  • final_frame - Indicates if this is the final frame of the message (1 for final frame, 0 for non final frame)
  • payload_type - Type of payload (ping frame, binary frame, text frame etc).
  • payload - The buffer which contains the payload
  • payload_length - Length of payload being sent
  • payload_buffer_size - Length of buffer containing payload
websocket_client.websocket_receive()

Returns the received message.

7.4.2. Example

# websocket client Example

import websocket_client
import time
import event

#EVENTS NUMBERS
#Every event is mapped to the bellow numbers
EVENT_ON_OPEN         =       2
EVENT_ON_MESSAGE      =       3
EVENT_ON_CLOSE        =       4
EVENT_ON_ERROR        =       5

PORT = 8088
HOST = "192.168.2.81"

WEBSOCKET_CONTINUATION_FRAME= 0
WEBSOCKET_TEXT_FRAME          =       1
WEBSOCKET_BINARY_FRAME        =       2
WEBSOCKET_CONNECTION_CLOSE    =       8
WEBSOCKET_PING                =       9
WEBSOCKET_PONG                =       10

FRAGMENTED            =       0
NOT_FRAGMENTED        =       1

poll_flag = True

#eventHandler is the function which handles the events that happen while the event driven websocket communication
def eventHandler(argtype, *args):
        global poll_flag

        fragmentation_flag = NOT_FRAGMENTED
        payload_type = WEBSOCKET_TEXT_FRAME
        payload = "websocket test"
        payload_length = 14
        payload_buffer_size = 256
        send_tuple = (fragmentation_flag, payload_type, payload, payload_length, payload_buffer_size)

        #print ('Event fired! event type =',argtype)

        #EVENT ON_OPEN
        if argtype == EVENT_ON_OPEN:
                print('Websocket opened! Sending message: ' + payload)
                websocket_client.websocket_send(send_tuple)
                event.response(2,"OPEN")
        #EVENT ON_MESSAGE
        elif argtype == EVENT_ON_MESSAGE:
                print('Received websocket message!')
                #time.sleep(1000)
                s=websocket_client.websocket_receive()
                print("Received: "+ s)
                time.sleep(2000)
                # PONG
                print("Sending back: " + s)
                send_tuple = (fragmentation_flag, payload_type, s, len(s)-1, payload_buffer_size)
                result=websocket_client.websocket_send(send_tuple)
                if result == 0:
                        print('SUCCESS SEND')
                else:
                        print('FAILURE SEND')
                event.response(3,"MESSAGE")
        #EVENT ON_CLOSE
        elif argtype == EVENT_ON_CLOSE:
                print('Websocket is closed!')
                event.response(4,"CLOSE")
                poll_flag = False
        #EVENT ON_ERROR
        elif argtype == EVENT_ON_ERROR:
                print('An error on the websocket. Closing...')
                event.response(5,"ERROR")
                poll_flag = False
        else:
                print('default')


def main():

        #Register the Event Handler
        event.onevent(eventHandler)

        #for connect
        request_uri   = "/echo"
        host          = HOST
        origin                = "http://192.168.2.81:8088/"
        sec_websocket_protocol        = "" # The current Server doesn't support secondary websocket protocol
        connect_tuple = (request_uri,host,origin,sec_websocket_protocol)

        websocket_client.websocket_client_start(PORT,connect_tuple)

        # Start polling for events
        while poll_flag==True:
                event.poll(2000)

        print('websocket app finished')

        while True:
                time.sleep(1000)

if __name__ == "__main__":
    main()