|
| 1 | +% |
| 2 | +% This file is part of AtomVM. |
| 3 | +% |
| 4 | +% Copyright 2025 Paul Guyot <[email protected]> |
| 5 | +% |
| 6 | +% Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +% you may not use this file except in compliance with the License. |
| 8 | +% You may obtain a copy of the License at |
| 9 | +% |
| 10 | +% http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +% |
| 12 | +% Unless required by applicable law or agreed to in writing, software |
| 13 | +% distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +% See the License for the specific language governing permissions and |
| 16 | +% limitations under the License. |
| 17 | +% |
| 18 | +% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later |
| 19 | +% |
| 20 | + |
| 21 | +-module(echo_websocket). |
| 22 | +-export([start/0]). |
| 23 | + |
| 24 | +-define(ECHO_WEBSOCKET_URL, <<"wss://echo.websocket.org">>). |
| 25 | + |
| 26 | +start() -> |
| 27 | + register(main, self()), |
| 28 | + Supported = websocket:is_supported(), |
| 29 | + if |
| 30 | + Supported -> |
| 31 | + emscripten:run_script( |
| 32 | + [ |
| 33 | + <<"window.document.getElementById('supported').innerHTML = 'yes';">>, |
| 34 | + <<"window.document.getElementById('send-button').onclick = () => { Module.cast('main', window.document.getElementById('send-text').value); };">> |
| 35 | + ], |
| 36 | + [main_thread, async] |
| 37 | + ), |
| 38 | + websocket_loop(); |
| 39 | + true -> |
| 40 | + emscripten:run_script( |
| 41 | + [ |
| 42 | + <<"window.document.getElementById('supported').innerHTML = 'no';">> |
| 43 | + ], |
| 44 | + [main_thread, async] |
| 45 | + ) |
| 46 | + end. |
| 47 | + |
| 48 | +websocket_loop() -> |
| 49 | + Websocket = websocket:new(?ECHO_WEBSOCKET_URL), |
| 50 | + websocket_loop(Websocket). |
| 51 | + |
| 52 | +websocket_loop(Websocket) -> |
| 53 | + ReadyState = websocket:ready_state(Websocket), |
| 54 | + URL = websocket:url(Websocket), |
| 55 | + Protocol = websocket:protocol(Websocket), |
| 56 | + Extensions = websocket:extensions(Websocket), |
| 57 | + emscripten:run_script( |
| 58 | + [ |
| 59 | + <<"window.document.getElementById('ready-state').innerHTML = '">>, |
| 60 | + atom_to_list(ReadyState), |
| 61 | + <<"';">>, |
| 62 | + <<"window.document.getElementById('url').innerHTML = '">>, |
| 63 | + URL, |
| 64 | + <<"';">>, |
| 65 | + <<"window.document.getElementById('protocol').innerHTML = '">>, |
| 66 | + Protocol, |
| 67 | + <<"';">>, |
| 68 | + <<"window.document.getElementById('extensions').innerHTML = '">>, |
| 69 | + Extensions, |
| 70 | + <<"';">> |
| 71 | + ], |
| 72 | + [main_thread, async] |
| 73 | + ), |
| 74 | + receive |
| 75 | + {emscripten, {cast, Message}} -> |
| 76 | + emscripten:run_script( |
| 77 | + [ |
| 78 | + <<"const e = window.document.createElement('div');">>, |
| 79 | + <<"e.classList.add('client-msg');">>, |
| 80 | + <<"e.append(\"">>, |
| 81 | + escape_js_str(binary_to_list(Message)), |
| 82 | + <<"\");">>, |
| 83 | + <<"window.document.getElementById('transcript').appendChild(e);">> |
| 84 | + ], |
| 85 | + [main_thread, async] |
| 86 | + ), |
| 87 | + ok = websocket:send_binary(Websocket, Message), |
| 88 | + websocket_loop(Websocket); |
| 89 | + {websocket_open, Websocket} -> |
| 90 | + emscripten:run_script( |
| 91 | + [ |
| 92 | + <<"window.document.getElementById('send-text').disabled = false;">>, |
| 93 | + <<"window.document.getElementById('send-button').disabled = false;">> |
| 94 | + ], |
| 95 | + [main_thread, async] |
| 96 | + ), |
| 97 | + websocket_loop(Websocket); |
| 98 | + {websocket, Websocket, Data} -> |
| 99 | + emscripten:run_script( |
| 100 | + [ |
| 101 | + <<"const e = window.document.createElement('div');">>, |
| 102 | + <<"e.classList.add('server-msg');">>, |
| 103 | + <<"e.append(\"">>, |
| 104 | + escape_js_str(binary_to_list(Data)), |
| 105 | + <<"\");">>, |
| 106 | + <<"window.document.getElementById('transcript').appendChild(e);">> |
| 107 | + ], |
| 108 | + [main_thread, async] |
| 109 | + ), |
| 110 | + websocket_loop(Websocket); |
| 111 | + {websocket_error, Websocket} -> |
| 112 | + emscripten:run_script( |
| 113 | + [ |
| 114 | + <<"window.document.getElementById('send-text').disabled = false;">>, |
| 115 | + <<"window.document.getElementById('send-button').disabled = false;">>, |
| 116 | + <<"const e = window.document.createElement('div');">>, |
| 117 | + <<"e.classList.add('error-msg');">>, |
| 118 | + <<"e.append('Error');">>, |
| 119 | + <<"window.document.getElementById('transcript').appendChild(e);">> |
| 120 | + ], |
| 121 | + [main_thread, async] |
| 122 | + ); |
| 123 | + {websocket_closed, Websocket, {WasClean, Code, Reason}} -> |
| 124 | + emscripten:run_script( |
| 125 | + [ |
| 126 | + <<"window.document.getElementById('send-text').disabled = true;">>, |
| 127 | + <<"window.document.getElementById('send-button').disabled = true;">>, |
| 128 | + <<"const e = window.document.createElement('div');">>, |
| 129 | + <<"e.classList.add('close-msg');">>, |
| 130 | + <<"const wasClean = window.document.createElement('p');">>, |
| 131 | + <<"wasClean.append(\"">>, |
| 132 | + atom_to_list(WasClean), |
| 133 | + <<"\");">>, |
| 134 | + <<"e.appendChild(wasClean);">>, |
| 135 | + <<"const code = window.document.createElement('p');">>, |
| 136 | + <<"code.append(\"">>, |
| 137 | + integer_to_list(Code), |
| 138 | + <<"\");">>, |
| 139 | + <<"e.appendChild(code);">>, |
| 140 | + <<"const reason = window.document.createElement('p');">>, |
| 141 | + <<"reason.append(\"">>, |
| 142 | + escape_js_str(binary_to_list(Reason)), |
| 143 | + <<"\");">>, |
| 144 | + <<"e.appendChild(reason);">>, |
| 145 | + <<"window.document.getElementById('transcript').appendChild(e);">> |
| 146 | + ], |
| 147 | + [main_thread, async] |
| 148 | + ); |
| 149 | + Other -> |
| 150 | + io:format("Unexpected message ~p\n", [Other]), |
| 151 | + websocket_loop(Websocket) |
| 152 | + end. |
| 153 | + |
| 154 | +escape_js_str(Str) -> |
| 155 | + escape_js_str(Str, []). |
| 156 | + |
| 157 | +escape_js_str([$" | Tail], Acc) -> |
| 158 | + escape_js_str(Tail, ["\\\"" | Acc]); |
| 159 | +escape_js_str([$\n | Tail], Acc) -> |
| 160 | + escape_js_str(Tail, ["<br />" | Acc]); |
| 161 | +escape_js_str([C | Tail], Acc) -> |
| 162 | + escape_js_str(Tail, [C | Acc]); |
| 163 | +escape_js_str([], Acc) -> |
| 164 | + lists:reverse(Acc). |
0 commit comments