|
39 | 39 | <form>
|
40 | 40 | <input type="text" name="webrepl_url" id="url" value="ws://192.168.4.1:8266/" />
|
41 | 41 | <input type="submit" id="button" value="Connect" onclick="button_click(); return false;" />
|
| 42 | +<input type="button" value="Upload" onclick="put_file(); return false;" /> |
| 43 | +<input type="button" value="Download" onclick="get_file(); return false;" /> |
42 | 44 | </form>
|
43 | 45 | <div id="term">
|
44 | 46 | </div>
|
|
53 | 55 | var term;
|
54 | 56 | var ws;
|
55 | 57 | var connected = false;
|
| 58 | +var binary_state = 0; |
56 | 59 |
|
57 | 60 | function calculate_size(win) {
|
58 | 61 | var cols = Math.max(80, Math.min(150, (win.innerWidth - 40) / 7)) | 0;
|
|
96 | 99 |
|
97 | 100 | function connect(url) {
|
98 | 101 | ws = new WebSocket(url);
|
| 102 | + ws.binaryType = 'arraybuffer'; |
99 | 103 | ws.onopen = function() {
|
100 | 104 | term.removeAllListeners('data');
|
101 | 105 | term.on('data', function(data) {
|
|
114 | 118 | term.write('\x1b[31mWelcome to MicroPython!\x1b[m\r\n');
|
115 | 119 |
|
116 | 120 | ws.onmessage = function(event) {
|
| 121 | + if (event.data instanceof ArrayBuffer) { |
| 122 | + var data = new Uint8Array(event.data); |
| 123 | + console.log('ws data:', data); |
| 124 | + switch (binary_state) { |
| 125 | + case 11: |
| 126 | + // first response for put |
| 127 | + if (decode_resp(data) == 0) { |
| 128 | + // send file data |
| 129 | + ws.send(new Uint8Array([64,64,64,64,64,64,64,64,64,10])); |
| 130 | + binary_state = 12; |
| 131 | + } |
| 132 | + break; |
| 133 | + case 12: |
| 134 | + // final response for put |
| 135 | + if (decode_resp(data) == 0) { |
| 136 | + console.log('success!'); |
| 137 | + } else { |
| 138 | + console.log('fail'); |
| 139 | + } |
| 140 | + binary_state = 0; |
| 141 | + break; |
| 142 | + |
| 143 | + case 21: |
| 144 | + // first response for get |
| 145 | + if (decode_resp(data) == 0) { |
| 146 | + binary_state = 22; |
| 147 | + } |
| 148 | + break; |
| 149 | + case 22: { |
| 150 | + // file data |
| 151 | + var sz = data[0] | (data[1] << 8); |
| 152 | + if (data.length == 2 + sz) { |
| 153 | + // we assume that the data comes in single chunks |
| 154 | + if (sz == 0) { |
| 155 | + // end of file |
| 156 | + binary_state = 23; |
| 157 | + } else { |
| 158 | + console.log('file data:', data); |
| 159 | + } |
| 160 | + } else { |
| 161 | + binary_state = 0; |
| 162 | + } |
| 163 | + break; |
| 164 | + } |
| 165 | + case 23: |
| 166 | + // final response |
| 167 | + if (decode_resp(data) == 0) { |
| 168 | + console.log('success!'); |
| 169 | + } else { |
| 170 | + console.log('fail'); |
| 171 | + } |
| 172 | + binary_state = 0; |
| 173 | + break; |
| 174 | + } |
| 175 | + } |
117 | 176 | term.write(event.data);
|
118 | 177 | };
|
119 | 178 | };
|
|
126 | 185 | prepare_for_connect();
|
127 | 186 | }
|
128 | 187 | }
|
| 188 | + |
| 189 | +function decode_resp(data) { |
| 190 | + if (data[0] == 'W'.charCodeAt(0) && data[1] == 'B'.charCodeAt(0)) { |
| 191 | + var code = data[2] | (data[3] << 8); |
| 192 | + return code; |
| 193 | + } else { |
| 194 | + return -1; |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +function put_file() { |
| 199 | + var dest_fname = 'test2.txt'; |
| 200 | + var dest_fsize = 10; |
| 201 | + |
| 202 | + // WEBREPL_FILE = "<2sBBQLH64s" |
| 203 | + var rec = new Uint8Array(2 + 1 + 1 + 8 + 4 + 2 + 64); |
| 204 | + rec[0] = 'W'.charCodeAt(0); |
| 205 | + rec[1] = 'A'.charCodeAt(0); |
| 206 | + rec[2] = 1; // put |
| 207 | + rec[3] = 0; |
| 208 | + rec[4] = 0; rec[5] = 0; rec[6] = 0; rec[7] = 0; rec[8] = 0; rec[9] = 0; rec[10] = 0; rec[11] = 0; |
| 209 | + rec[12] = dest_fsize & 0xff; rec[13] = (dest_fsize >> 8) & 0xff; rec[14] = (dest_fsize >> 16) & 0xff; rec[15] = (dest_fsize >> 24) & 0xff; |
| 210 | + rec[16] = dest_fname.length & 0xff; rec[17] = (dest_fname.length >> 8) & 0xff; |
| 211 | + for (var i = 0; i < 64; ++i) { |
| 212 | + if (i < dest_fname.length) { |
| 213 | + rec[18 + i] = dest_fname.charCodeAt(i); |
| 214 | + } else { |
| 215 | + rec[18 + i] = 0; |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + // initiate put |
| 220 | + binary_state = 11; |
| 221 | + ws.send(rec); |
| 222 | +} |
| 223 | + |
| 224 | +function get_file() { |
| 225 | + var src_fname = 'test.txt'; |
| 226 | + |
| 227 | + // WEBREPL_FILE = "<2sBBQLH64s" |
| 228 | + var rec = new Uint8Array(2 + 1 + 1 + 8 + 4 + 2 + 64); |
| 229 | + rec[0] = 'W'.charCodeAt(0); |
| 230 | + rec[1] = 'A'.charCodeAt(0); |
| 231 | + rec[2] = 2; // get |
| 232 | + rec[3] = 0; |
| 233 | + rec[4] = 0; rec[5] = 0; rec[6] = 0; rec[7] = 0; rec[8] = 0; rec[9] = 0; rec[10] = 0; rec[11] = 0; |
| 234 | + rec[12] = 0; rec[13] = 0; rec[14] = 0; rec[15] = 0; |
| 235 | + rec[16] = src_fname.length & 0xff; rec[17] = (src_fname.length >> 8) & 0xff; |
| 236 | + for (var i = 0; i < 64; ++i) { |
| 237 | + if (i < src_fname.length) { |
| 238 | + rec[18 + i] = src_fname.charCodeAt(i); |
| 239 | + } else { |
| 240 | + rec[18 + i] = 0; |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + // initiate get |
| 245 | + binary_state = 21; |
| 246 | + ws.send(rec); |
| 247 | +} |
| 248 | + |
129 | 249 | </script>
|
130 | 250 |
|
131 | 251 | </html>
|
0 commit comments