misskey-dev / misskey.js

Misskey SDK for JavaScript
MIT License
76 stars 25 forks source link

Error: No valid WebSocket class provided #48

Open ledlamp opened 1 year ago

ledlamp commented 1 year ago

my code

import * as Misskey from 'misskey-js';

var TOKEN = "redacted";

var client = new Misskey.api.APIClient({
    origin: "https://misskey.io",
    credential: TOKEN
});

var stream = new Misskey.Stream("https://misskey.io", {token: TOKEN});
var channel = stream.useChannel("homeTimeline");
channel.on("note", note => {
    console.log("note");
});

error

me@g:~/Desktop/c30-translate$ node index.mjs 
/home/me/Desktop/c30-translate/node_modules/reconnecting-websocket/dist/reconnecting-websocket-cjs.js:508
            throw Error('No valid WebSocket class provided');
                  ^

Error: No valid WebSocket class provided
    at ReconnectingWebSocket._connect (/home/me/Desktop/c30-translate/node_modules/reconnecting-websocket/dist/reconnecting-websocket-cjs.js:508:19)
    at new ReconnectingWebSocket (/home/me/Desktop/c30-translate/node_modules/reconnecting-websocket/dist/reconnecting-websocket-cjs.js:205:14)
    at new Stream (/home/me/Desktop/c30-translate/node_modules/misskey-js/built/streaming.js:39:23)
    at file:///home/me/Desktop/c30-translate/index.mjs:11:14
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
    at async loadESM (node:internal/process/esm_loader:91:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)

Node.js v18.12.1
ledlamp commented 1 year ago

work-around

import WebSocket from "ws";
global.WebSocket = WebSocket;
import * as Misskey from 'misskey-js';
paihu commented 1 year ago

Pass the WebSocket as an argument to Stream.

var stream = new Misskey.Stream("https://misskey.io", {token: TOKEN}, {WebSocket: WebSocket})

https://github.com/misskey-dev/misskey.js/pull/21/files