Moddable-OpenSource / moddable

Tools for developers to create truly open IoT products using standard JavaScript on low cost microcontrollers.
http://www.moddable.com
1.35k stars 238 forks source link

WebSocket over SSL/TLS fails to open #1419

Closed stc1988 closed 1 month ago

stc1988 commented 1 month ago

Build environment: macOS Moddable SDK version: 5.0.1 Target device: desktop simulator

Now I try to use OpenAI Realtime API with the following code, but I got a error /Users/satoshi/Projects/moddable/examples/io/tcp/websocketclient/websocketclient.js (59) # Break: cannot coerce undefined to object (in WebSocketClient)!

    const ws = (this.#ws = new WebSocket({
      url: `wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01`,
      headers: new Headers([
        ["OpenAI-Beta", "realtime=v1"],
        ["Authorization", `Bearer ${apiKey}`],
      ]),
    }));

I think options.dns is set from WebSocket.js, and that the value comes from config.js.

It doesn’t seem that device.network.wss is set here, but is this correct?

phoddie commented 1 month ago

Yes, device.network.wss is undefined so the request fails because required configuration information is missing.

There is no wss example for 419. I've created that following the example of https. That will be added to this repository. For now you can download it. This is able to establish a TLS connection toapi.openai.com.

To use the HTML compatible WebSocket constructor (as your example does above), you will need to add the wss configuration to your project. Do that by including $(MODDABLE)/examples/io/tcp/websocketsclient/manifest_wssclient.json.

By the way, it is nice that issue #1419 is about ECMA-419. ;)

stc1988 commented 1 month ago

Thank you for confirming.

When I overwrite the examples/io/tcp/websocketclient folder and build, the following error occurred.

# mcrez resources
Total resource size: 5285 bytes
# cc mc.resources.c
TypeError: cannot coerce undefined to object
 at config ()
### TypeError: cannot coerce undefined to object!
make: *** [/Users/satoshi/Projects/moddable/build/tmp/mac/mc/debug/websocketclient/mc.xs.c] Error 22
phoddie commented 1 month ago

When I overwrite the examples/io/tcp/websocketclient folder and build, the following error occurred...

Yes, that is what would happen if you overwrite "websocketclient".

Notice that the zip file is named websocketsclient.zip not websocketclient.zip (there is an "s" between "socket" and "client", "s" for "secure"). The zip directory is intended to be placed adjacent to websocketclient, not replace it. That parallels the structure of the httpclient and httpsclient directories.

stc1988 commented 1 month ago

Wow! It was a basic mistake. As a result, the RealTime API hasn't been rolled out yet, but I was able to confirm that the connection via wss is likely working.

phoddie commented 1 month ago

As a result, the RealTime API hasn't been rolled out yet, but I was able to confirm that the connection via wss is likely working.

Nice. We are ahead!

stc1988 commented 1 month ago

I just checked, and it looks like the connection is now working 🚀

phoddie commented 1 month ago

Good luck. Looking forward to the demo. ;)