5 library(http/websocket): WebSocket support
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
    • Packages
      • SWI-Prolog HTTP support
        • library(http/websocket): WebSocket support
          • http_open_websocket/3
          • http_upgrade_to_websocket/3
          • ws_send/2
          • ws_receive/2
          • ws_receive/3
          • ws_close/3
          • ws_open/3
          • ws_property/2
          • ws_mask/1
Availability::- use_module(library(http/websocket)).
Source[det]ws_receive(+WebSocket, -Message:dict)
[det]ws_receive(+WebSocket, -Message:dict, +Options)
Receive the next message from WebSocket. Message is a dict containing the following keys:
opcode:OpCode
OpCode of the message. This is an atom for known opcodes and an integer for unknown ones. If the peer closed the stream, OpCode is bound to close and data to the atom end_of_file.
data:String
The data, represented as a string. This field is always present. String is the empty string if there is no data in the message.
rsv:RSV
Present if the WebSocket RSV header is not 0. RSV is an integer in the range [1..7].

If ping message is received and WebSocket is a stream pair, ws_receive/1 replies with a pong and waits for the next message.

The predicate ws_receive/3 processes the following options:

format(+Format)
Defines how text messages are parsed. Format is one of
string
Data is returned as a Prolog string (default)
json
Data is parsed using json_read_dict/3, which also receives Options.
prolog
Data is parsed using read_term/3, which also receives Options.
To be done
Add a hook to allow for more data formats?