elpheria/rpc-websockets

Listen socket events on null after create one

sagittaracc opened this issue · 3 comments

  • Include index.browser-bundle.js
<!doctype html>
<html>
<head>
</head>
<body>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>

<script src="node_modules/rpc-websockets/dist/index.browser-bundle.js"></script>

<script src="index.js"></script>
</body>
</html>
  • Connect in index.js
ws = new WebSocket("...")
  • Try to listen when it opens
ws.on("open", ...)
  • Get an error ws.on is undefined

Can you please check if you have WebSocket instantiated there?

Yes, sure.
This is what i do in index.js:

ws = new WebSocket("<some address>")
    
console.log(ws);
console.log(ws.on);

This is what i get:

WebSocket
    binaryType: "blob"
    bufferedAmount: 0
    extensions: ""
    onclose: null
    onerror: null
    onmessage: null
    onopen: null
    protocol: ""
    readyState: 1
    url: "<my address>"
    __proto__: WebSocket

undefined

Im sorry that was probably misusing. Ive found an issue in this repo which tells i should go like this:

new RPCWebSocket.Client('...')

But i didnt notice anything in doc relation to this
Now it seems to be working fine. At least the socket exists and the open event has triggered