@@ -41,7 +41,7 @@ On a worker thread, this object is a proxy for the web page's
41
41
42
42
### ` pyscript.document `
43
43
44
- On both main and worker threads, this object is a proxy for the the web page's
44
+ On both main and worker threads, this object is a proxy for the web page's
45
45
[ document object] ( https://developer.mozilla.org/en-US/docs/Web/API/Document ) .
46
46
The ` document ` is a representation of the
47
47
[ DOM] ( https://developer.mozilla.org/en-US/docs/Web/API/Document_object_model/Using_the_Document_Object_Model )
@@ -257,6 +257,121 @@ result = await fetch("https://example.com", method="POST", body="HELLO").text()
257
257
bug). However, you could use a pass-through proxy service to get around
258
258
this limitation (i.e. the proxy service makes the call on your behalf).
259
259
260
+ ### ` pyscript.WebSocket `
261
+
262
+ If a ` pyscript.fetch ` results in a call and response HTTP interaction with a
263
+ web server, the ` pyscript.Websocket ` class provides a way to use
264
+ [ websockets] ( https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API )
265
+ for two-way sending and receiving of data via a long term connection with a
266
+ web server.
267
+
268
+ PyScript's implementation, available in both the main thread and a web worker,
269
+ closely follows the browser's own
270
+ [ WebSocket] ( https://developer.mozilla.org/en-US/docs/Web/API/WebSocket ) class.
271
+
272
+ This class accepts the following named arguments:
273
+
274
+ * A ` url ` pointing at the _ ws_ or _ wss_ address. E.g.:
275
+ ` WebSocket(url="ws://localhost:5037/") `
276
+ * Some ` protocols ` , an optional string or a list of strings as
277
+ [ described here] ( https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/WebSocket#parameters ) .
278
+
279
+ The ` WebSocket ` class also provides these convenient static constants:
280
+
281
+ * ` WebSocket.CONNECTING ` (` 0 ` ) - the ` ws.readyState ` value when a web socket
282
+ has just been created.
283
+ * ` WebSocket.OPEN ` (` 1 ` ) - the ` ws.readyState ` value once the socket is open.
284
+ * ` WebSocket.CLOSING ` (` 2 ` ) - the ` ws.readyState ` after ` ws.close() ` is
285
+ explicitly invoked to stop the connection.
286
+ * ` WebSocket.CLOSED ` (` 3 ` ) - the ` ws.readyState ` once closed.
287
+
288
+ A ` WebSocket ` instance has only 2 methods:
289
+
290
+ * ` ws.send(data) ` - where ` data ` is either a string or a Python buffer,
291
+ automatically converted into a JavaScript typed array. This sends data via
292
+ the socket to the connected web server.
293
+ * ` ws.close(code=0, reason="because") ` - which optionally accepts ` code ` and
294
+ ` reason ` as named arguments to signal some specific status or cause for
295
+ closing the web socket. Otherwise ` ws.close() ` works with the default
296
+ standard values.
297
+
298
+ A ` WebSocket ` instance also has the fields that the JavaScript
299
+ ` WebSocket ` instance will have:
300
+
301
+ * [ binaryType] ( https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/binaryType ) -
302
+ the type of binary data being received over the WebSocket connection.
303
+ * [ bufferedAmount] ( https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/bufferedAmount ) -
304
+ a read-only property that returns the number of bytes of data that have been
305
+ queued using calls to ` send() ` but not yet transmitted to the network.
306
+ * [ extensions] ( https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/extensions ) -
307
+ a read-only property that returns the extensions selected by the server.
308
+ * [ protocol] ( https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/protocol ) -
309
+ a read-only property that returns the name of the sub-protocol the server
310
+ selected.
311
+ * [ readyState] ( https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/readyState ) -
312
+ a read-only property that returns the current state of the WebSocket
313
+ connection as one of the ` WebSocket ` static constants (` CONNECTING ` , ` OPEN ` ,
314
+ etc...).
315
+ * [ url] ( https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/url ) -
316
+ a read-only property that returns the absolute URL of the ` WebSocket `
317
+ instance.
318
+
319
+ A ` WebSocket ` instance can have the following listeners. Directly attach
320
+ handler functions to them. Such functions will always receive a single
321
+ ` event ` object.
322
+
323
+ * [ onclose] ( https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close_event ) -
324
+ fired when the ` WebSocket ` 's connection is closed.
325
+ * [ onerror] ( https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/error_event ) -
326
+ fired when the connection is closed due to an error.
327
+ * [ onmessage] ( https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/message_event ) -
328
+ fired when data is received via the ` WebSocket ` . If the ` event.data ` is a
329
+ JavaScript typed array instead of a string, the reference it will point
330
+ directly to a _ memoryview_ of the underlying ` bytearray ` data.
331
+ * [ onopen] ( https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/open_event ) -
332
+ fired when the connection is opened.
333
+
334
+ The following code demonstrates a ` pyscript.WebSocket ` in action.
335
+
336
+ ``` html
337
+ <script type =" mpy" worker >
338
+ from pyscript import WebSocket
339
+
340
+ def onopen (event ):
341
+ print (event .type )
342
+ ws .send (" hello" )
343
+
344
+ def onmessage (event ):
345
+ print (event .type , event .data )
346
+ ws .close ()
347
+
348
+ def onclose (event ):
349
+ print (event .type )
350
+
351
+ ws = WebSocket (url= " ws://localhost:5037/" )
352
+ ws .onopen = onopen
353
+ ws .onmessage = onmessage
354
+ ws .onclose = onclose
355
+ </script >
356
+ ```
357
+
358
+ !!! info
359
+
360
+ It's also possible to pass in any handler functions as named arguments when
361
+ you instantiate the `pyscript.WebSocket` class:
362
+
363
+ ```python
364
+ from pyscript import WebSocket
365
+
366
+
367
+ def onmessage(event):
368
+ print(event.type, event.data)
369
+ ws.close()
370
+
371
+
372
+ ws = WebSocket(url="ws://example.com/socket", onmessage=onmessage)
373
+ ```
374
+
260
375
### ` pyscript.ffi.to_js `
261
376
262
377
A utility function to convert Python references into their JavaScript
@@ -343,6 +458,25 @@ an `id`, that `id` will be returned.
343
458
Then use the standard `document.getElementById(script_id)` function to
344
459
return a reference to it in your code.
345
460
461
+ ### ` pyscript.config `
462
+
463
+ A Python dictionary representing the configuration for the interpreter.
464
+
465
+ ``` python title="Reading the current configuration."
466
+ from pyscript import config
467
+
468
+
469
+ # It's just a dict.
470
+ print (config.get(" files" ))
471
+ ```
472
+
473
+ !!! warning
474
+
475
+ Changing the `config` dictionary at runtime has no effect on the actual
476
+ configuration.
477
+
478
+ It's just a convenience to **read the configuration** at run time.
479
+
346
480
### ` pyscript.HTML `
347
481
348
482
A class to wrap generic content and display it as un-escaped HTML on the page.
@@ -379,8 +513,9 @@ A class used to instantiate a new worker from within Python.
379
513
Sometimes we disambiguate between interpreters through naming conventions
380
514
(e.g. `py` or `mpy`).
381
515
382
- However, it is always `PyWorker` and the desired interpreter is specified
383
- via a `type` option which must be either `micropython` or `pyodide`.
516
+ However, this class is always `PyWorker` and **the desired interpreter
517
+ MUST be specified via a `type` option**. Valid values for the type of
518
+ interpreter are either `micropython` or `pyodide`.
384
519
385
520
The following fragments demonstrate how to evaluate the file ` worker.py ` on a
386
521
new worker from within Python.
@@ -400,7 +535,7 @@ print("awake")
400
535
``` python title="main.py - starts a new worker in Python."
401
536
from pyscript import PyWorker
402
537
403
- # type can be either `micropython` or `pyodide`
538
+ # type MUST be either `micropython` or `pyodide`
404
539
PyWorker(" worker.py" , type = " micropython" )
405
540
```
406
541
0 commit comments