Skip to content

Commit bc1937e

Browse files
author
Michael Cohen
authored
Passing in default window.WebSocket in browsers (#84)
* Passing in default window.WebSocket in browsers * 0.0.14 * Remove unused import
1 parent e0d5ad8 commit bc1937e

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@opensea/stream-js",
3-
"version": "0.0.13",
3+
"version": "0.0.14",
44
"description": "An SDK to receive pushed updates from OpenSea over websocket",
55
"license": "MIT",
66
"author": "OpenSea Developers",

src/client.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,36 +31,40 @@ export class OpenSeaStreamClient {
3131
onError = (error) => this.error(error)
3232
}: ClientConfig) {
3333
const endpoint = apiUrl || ENDPOINTS[network];
34+
const webTransportDefault =
35+
typeof window !== 'undefined' ? window.WebSocket : undefined;
3436
this.socket = new Socket(endpoint, {
3537
params: { token },
38+
transport: webTransportDefault,
3639
...connectOptions
3740
});
41+
3842
this.socket.onError(onError);
3943
this.channels = new Map<string, Channel>();
4044
this.logLevel = logLevel;
4145
}
4246

4347
private debug(message: unknown) {
4448
if (this.logLevel <= LogLevel.DEBUG) {
45-
console.debug(message);
49+
console.debug(`[DEBUG]: ${message}`);
4650
}
4751
}
4852

4953
private info(message: unknown) {
5054
if (this.logLevel <= LogLevel.INFO) {
51-
console.info(message);
55+
console.info(`[INFO]: ${message}`);
5256
}
5357
}
5458

5559
private warn(message: unknown) {
5660
if (this.logLevel <= LogLevel.WARN) {
57-
console.warn(message);
61+
console.warn(`[WARN]: ${message}`);
5862
}
5963
}
6064

6165
private error(message: unknown) {
6266
if (this.logLevel <= LogLevel.ERROR) {
63-
console.error(message);
67+
console.error(`[ERROR]: ${message}`);
6468
}
6569
}
6670

@@ -90,6 +94,7 @@ export class OpenSeaStreamClient {
9094
private getChannel = (topic: string): Channel => {
9195
let channel = this.channels.get(topic);
9296
if (!channel) {
97+
this.debug(`Creating channel for topic: "${topic}"`);
9398
channel = this.createChannel(topic);
9499
}
95100
return channel;
@@ -103,9 +108,12 @@ export class OpenSeaStreamClient {
103108
this.socket.connect();
104109

105110
const topic = collectionTopic(collectionSlug);
111+
this.debug(`Fetching channel ${topic}`);
106112
const channel = this.getChannel(topic);
113+
this.debug(`Subscribing to ${eventType} events on ${topic}`);
107114
channel.on(eventType, callback);
108115
return () => {
116+
this.debug(`Unsubscribing from ${eventType} events on ${topic}`);
109117
channel.leave().receive('ok', () => {
110118
this.channels.delete(topic);
111119
this.info(

0 commit comments

Comments
 (0)