File
Blob: src/workerd/server/tests/python/durable-object-websocket/tester.js
| 1 | // Copyright (c) 2025 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | export default { |
| 5 | async test(ctrl, env, ctx) { |
| 6 | const resp = await env.PYTHON.fetch('http://example.com/websocket', { |
| 7 | headers: { Upgrade: 'websocket' }, |
| 8 | }); |
| 9 | const ws = resp.webSocket; |
| 10 | if (!ws) { |
| 11 | throw new Error('No websocket'); |
| 12 | } |
| 13 | ws.accept(); |
| 14 | const messagePromise = new Promise((resolve) => { |
| 15 | ws.addEventListener('message', (msg) => { |
| 16 | console.log('Received in JS Tester: ', msg.data); |
| 17 | resolve(msg.data); |
| 18 | }); |
| 19 | }); |
| 20 | ws.send('test'); |
| 21 | |
| 22 | await messagePromise; |
| 23 | ws.close(); |
| 24 | }, |
| 25 | }; |