Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added websocket extension #826

Merged
merged 41 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
ba5c6f3
added websocket extension
RedMan13 Aug 1, 2023
99727b7
Merge branch 'TurboWarp:master' into websocket
RedMan13 Aug 8, 2023
3295d75
Update ws.js
RedMan13 Aug 20, 2023
ca2bc1e
Delete index.ejs
RedMan13 Aug 20, 2023
1dff481
Merge branch 'TurboWarp:master' into websocket
RedMan13 Aug 20, 2023
a2be889
Merge branch 'TurboWarp:master' into websocket
RedMan13 Aug 25, 2023
fd21e2d
i hate this formating but okay
RedMan13 Aug 26, 2023
4b2979f
some style thigns
GarboMuffin Aug 27, 2023
7e58165
Merge branch 'master' into websocket
GarboMuffin Aug 27, 2023
2bad27c
HAT -> EVENT
GarboMuffin Aug 27, 2023
13fcd24
message received? should not return undefined
GarboMuffin Aug 27, 2023
73e95df
Merge branch 'TurboWarp:master' into websocket
RedMan13 Sep 7, 2023
38f8536
message caching thigy
RedMan13 Sep 8, 2023
b88b41b
handle binnary data better
RedMan13 Sep 8, 2023
98a4092
better binnary handling
RedMan13 Sep 8, 2023
d4e1098
warning
RedMan13 Sep 8, 2023
f7f36ca
prettier
RedMan13 Sep 8, 2023
371f452
didnt notice that was added
RedMan13 Sep 8, 2023
645843e
add an echo server
RedMan13 Sep 8, 2023
5fc8048
Merge branch 'TurboWarp:master' into websocket
RedMan13 Sep 20, 2023
3369ddb
Update ws.js
RedMan13 Sep 20, 2023
3aaa48d
Merge branch 'TurboWarp:master' into websocket
RedMan13 Sep 30, 2023
bb7e355
bruh
RedMan13 Sep 30, 2023
e67e5d3
Merge branch 'TurboWarp:master' into websocket
RedMan13 Oct 9, 2023
af6bd30
Update description
GarboMuffin Nov 1, 2023
d615e99
stackoverflow license is not GPL compatible
GarboMuffin Nov 1, 2023
2582eb8
fix some weird things
GarboMuffin Nov 1, 2023
8bd0eb8
it's a queue, not a cache. quite different
GarboMuffin Nov 1, 2023
ccbe671
pedantic changes
GarboMuffin Nov 1, 2023
3d6fe74
queue if *any* hat is waiting, not all of them
GarboMuffin Nov 1, 2023
3182d27
formatting
GarboMuffin Nov 1, 2023
b10804b
The queue is an implementation detail
GarboMuffin Nov 1, 2023
6abfdf5
Try to queue up data to send if we're still CONNECTING
GarboMuffin Nov 1, 2023
578fefb
minor tweaks
GarboMuffin Nov 1, 2023
333b98f
Try to avoid having connections that just never get closed
GarboMuffin Nov 1, 2023
b2485da
remove messageReceived, they should use the event, which works just fine
GarboMuffin Nov 1, 2023
693191a
Fix various usability bugs from making an example project
GarboMuffin Nov 1, 2023
303dd21
npm run format
GarboMuffin Nov 1, 2023
90b6854
Documentation
GarboMuffin Nov 1, 2023
469bc73
don't delete the instance right away, that breaks close code
GarboMuffin Nov 1, 2023
3ffd228
Disable monitors
GarboMuffin Nov 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions docs/godslayerakp/ws.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# WebSocket

This extension lets you communicate directly with most [WebSocket](https://en.wikipedia.org/wiki/WebSocket) servers. This is the protocol that things like cloud variables and Cloudlink use.

These are rather low level blocks. They let you establish the connection, but your project still needs to know what kinds of messages to send and how to read messages from the server.

## Blocks

```scratch
connect to [wss://...] :: #307eff
```
You have to run this block before any of the other blocks can do anything. You need to provide a valid WebSocket URL.

The URL should start with `ws://` or `wss://`. For security reasons, `ws://` URLs will usually only work if the WebSocket is running on your computer (for example, `ws://localhost:8000`).

Something simple to play with is the echo server: `wss://echoserver.redman13.repl.co`. Any message you send to it, it'll send right back to you.

Note that connections are **per sprite**. Each sprite (or clone) can connect to one server at a time. Multiple sprites can connect to the same or different servers as much as your computer allows, but note those will all be separate connections.

---

```scratch
when connected :: hat #307eff
```
<br>

```scratch
<is connected? :: #307eff >
```
Connecting to the server can take some time. Use these blocks to know when the connection was successful. After this, you can start sending and receiving messages.

When the connection is lost, any blocks under the hat will also be stopped.

---

```scratch
when message received :: hat #307eff
```
<br>

```scratch
(received message data :: #307eff)
```

These blocks let you receive messages from the server. The hat block block will run once for each message the server sends with the data stored in the round reporter block.

Note that WebSocket supports two types of messages:

- **Text messages**: The data in the block will just be the raw text from the server.
- **Binary messages**: The data in the block will be a base64-encoded data: URL of the data, as it may not be safe to store directly in a string. You can use other extensions to convert this to something useful, such as fetch, depending on what data it contains.

If multiple messages are received in a single frame or if your message processing logic causes delays (for example, using wait blocks), messages after the first one will be placed in a **queue**. Once your script finishes, if there's anything in the queue, the "when message received" block will run again the next frame.

---

```scratch
send message (...) :: #307eff
```

This is the other side: it lets you send messages to the server. Only text messages are supported; binary messages are not yet supported.

There's no queue this time. The messages are sent over as fast as your internet connection and the server will allow.

---

```scratch
when connection closes :: hat #307eff
```
<br>

```scratch
<is connection closed? :: #307eff>
```
<br>

These let you detect when either the server closes the connection or your project closes the connection. They don't distinguish. Note that connections have separate blocks.

Servers can close connections for a lot of reasons: perhaps it's restarting, or perhaps your project tried to do something the server didn't like.

```scratch
(closing code :: #307eff)
```
<br>

```scratch
(closing message :: #307eff)
```

These blocks can help you gain some insight. Closing code is a number from the WebSocket protocol. There is a [big table](https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent/code#value) of possible values, but generally there is very little to gain from looking at these.

Servers can also send a text "reason" when they close the connection, although almost no servers actually do this.

```scratch
close connection :: #307eff
```
<br>

```scratch
close connection with code (1000) :: #307eff
```
<br>

```scratch
close connection with reason (...) and code (1000) :: #307eff
```

Your project can also close the connection whenever it wants. All of these blocks do basically the same thing.

Just like how the server can send a code and a reason when it closes the connection, you can send those to the server. Note some limitations:

- **Code** can be either the number 1000 ("Normal Closure") or an integer in the range 3000-4999 (meaning depends on what server you're talking to). Anything not in this range will be converted to 1000. Few servers will look at this.
- **Reason** can be any text up to 123-bytes long when encoded as UTF-8. Usually that just means up to 123 characters, but things like Emoji are technically multiple characters. Regardless very few servers will even bother to look at this.

---

```scratch
when connection errors :: hat #307eff
```
<br>

```scratch
<is connection errored? :: #307eff>
```

Sometimes things don't go so well. Maybe your internet connection died, the server is down, or you typed in the wrong URL. There's a lot of things that can go wrong. These let you try to handle that.

Unfortunately we can't give much insight as to what caused the errors. Your browser tells us very little, but even if it did give us more information, it probably wouldn't be very helpful.
1 change: 1 addition & 0 deletions extensions/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"qxsck/var-and-list",
"vercte/dictionaries",
"godslayerakp/http",
"godslayerakp/ws",
"Lily/CommentBlocks",
"veggiecan/LongmanDictionary",
"CubesterYT/TurboHook",
Expand Down
Loading