This article or section documents something not included in the current version of Scratch (3.0). It is only useful from a historical perspective. |
- This article or section documents an outdated version of Scratch (version 1.4). For this article in Scratch 2.0, see Scratch Extension Protocol (2.0).
The Remote Sensors Protocol or Scratch Extension Protocol was an experimental extension feature that allowed interaction between Scratch (1.3+) and other programs.
Introduction
Three kinds of interaction were supported:
- Sharing broadcasts (in both directions)
- Virtual sensors
- Scratch's global variables are made visible
Protocol
The experimental extension feature was enabled using the right-button menu on one of the two sensor blocks. When remote sensors were enabled, Scratch would listen for connections on TCP port 42001.
Once a connection was established, messages could be sent in both directions over the socket connection.
Each message consisted of a four-byte "size" field, most-significant byte first, followed by the message itself:
<size: 4 bytes><msg: size bytes>
The four-byte size field was not counted as part of the message size. Thus, an empty message was four zero bytes.
Message Types
The message up to the first whitespace character (any byte <= 32) was a case-insensitive message type string that was used to decide how to handle the message.
Clients should have extracted and checked the message type string from a message before doing further processing.
Common Message Types
Most message types contained human-readable strings made up of the following elements:
- Unquoted single-word strings (cat, mouse-x)
- Quoted strings ("a four word string", "embedded ""quotation marks"" are doubled")
- Numbers (1, -1, 3.14, -1.2, .1, -.2)
- Booleans (true or false)
Words and strings were encoded in UTF-8.
Note: | ASCII is a subset of UTF-8. |
Here are the two most useful message types:
- Broadcast <string>
- Sensor-update <var-name_1> <new-value_1> ...
A sensor update message included one or more (variable name, value) pairs. Variable names were strings, while values could be either numbers or quoted strings.
Here is an example:
sensor-update "note" 60 "seconds" 0.1 broadcast "play note"
The first message sets the value of two virtual sensors named "note" and "seconds". The second broadcasts the "play note" message. A Scratch script might respond to this broadcast by playing a note as specified by the sensor values.
Scratch sent these two message types when broadcast messages or global variable changes occured. Scratch also responded to these messages; broadcast messages sent to Scratch caused a broadcast to occur, and sensor-update messages updated the values of virtual sensors available in the sensor value block drop-down menu.