|
json - a JSON handle: the message to send file$ (optional) - a file whose bytes go with the message, such as a picture the program saved; "" sends none (default) |
|
Sends a message to the web page that runs the program. When your program runs in a web page, it can tell the page things: a game exported for the web can report a score, say that a level is finished or ask the page to show a button, and the online IDE's World Editor answers Blip's requests this way. The message is the JSON value the handle holds, usually an object you built with CreateJsonObject or ParseJson. SendPageMessage sends a copy, so the handle stays yours: free it with FreeJson when you are done. Give a file, and its bytes go with the message, read at once, so the program can delete the file straight after. That is how a picture reaches the page without passing through a string. The page receives {type: 'b3d-page-message', message, file}, with file holding the file's path and bytes, or null. A file that cannot be read is left out, and the message still goes.When you answer a message from PageMessage that has an id, put the same id in your answer, so the page knows which question you answered. On Windows and macOS there is no page, so SendPageMessage checks the handle and sends nothing. The command is there so that one program compiles and runs everywhere. See also: PageMessage, CreateJsonObject, JsonObjectSet, FreeJson. |
; SendPageMessage Example
; -----------------------
Graphics 640,480,0,2
SetBuffer BackBuffer()
score=0
sent=0
latest$="(nothing yet)"
While Not KeyDown(1)
; Space scores a point, and P sends a picture of the screen too
space=KeyHit(57)
picture=KeyHit(25)
If space Or picture Then
If space Then score=score+1
; Build the message: a JSON object with the score in it
report=CreateJsonObject()
value=CreateJsonInteger(score)
JsonObjectSet report,"score",value
FreeJson value
If picture Then
; The file's bytes go with the message; the file can go after
SaveBuffer FrontBuffer(),"demo_screen.bmp"
SendPageMessage report,"demo_screen.bmp"
DeleteFile "demo_screen.bmp"
Else
SendPageMessage report
EndIf
latest=JsonStringify(report,0)
FreeJson report
sent=sent+1
EndIf
Cls
Text 0,0,"Space: score a point and tell the page P: send a picture too Esc: exit"
Text 0,20,"Score: "+score+" messages sent: "+sent
Text 0,40,"Latest message: "+latest
Text 0,80,"On Windows and macOS there is no page, so SendPageMessage sends nothing."
Flip
Wend
End