Skip to content
This repository was archived by the owner on Dec 6, 2023. It is now read-only.

Commit 6feae06

Browse files
authored
feat: Allow for additional types of connections
BREAKING CHANGES: This changes how communication works slightly. This should not cause any issues but we are marking it breaking just to be on the safe side.
1 parent 9c709e4 commit 6feae06

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

src/connection-manager.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { NativeModules } from "react-native"
2+
3+
export default class ConnectionManager {
4+
private webSocket: any
5+
private flipperConnection: any
6+
7+
constructor(path?: string) {
8+
this.webSocket = new WebSocket(path)
9+
this.flipperConnection = NativeModules.Reactotron
10+
}
11+
12+
send(payload: any) {
13+
if (this.webSocket.readyState === 1) {
14+
this.webSocket.send(payload)
15+
}
16+
17+
if (this.flipperConnection) {
18+
this.flipperConnection.sendCommand("command", JSON.parse(payload))
19+
}
20+
}
21+
22+
on(event: "open" | "close" | "message", callback: any) {
23+
if (event === "open") {
24+
if (this.flipperConnection) {
25+
callback()
26+
}
27+
28+
this.webSocket.onopen = callback
29+
} else if (event === "close") {
30+
this.webSocket.onclose = callback
31+
} else if (event === "message") {
32+
this.webSocket.onmessage = evt => callback(evt.data)
33+
}
34+
}
35+
36+
close() {
37+
this.webSocket.close()
38+
}
39+
}

src/reactotron-react-native.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import trackGlobalErrors, { TrackGlobalErrorsOptions } from "./plugins/trackGlob
1111
import networking, { NetworkingOptions } from "./plugins/networking"
1212
import storybook from "./plugins/storybook"
1313
import devTools from "./plugins/devTools"
14+
import ConnectionManager from "./connection-manager"
1415

1516
const constants = NativeModules.PlatformConstants || {}
1617

@@ -19,7 +20,7 @@ const REACTOTRON_ASYNC_CLIENT_ID = "@REACTOTRON/clientId"
1920
let tempClientId = null
2021

2122
const DEFAULTS = {
22-
createSocket: (path: string) => new WebSocket(path), // eslint-disable-line
23+
createSocket: (path: string) => new ConnectionManager(path), // eslint-disable-line
2324
host: getHost("localhost"),
2425
port: 9090,
2526
name: "React Native App",

0 commit comments

Comments
 (0)