Page MenuHomePhabricator

D9607.id32626.diff
No OneTemporary

D9607.id32626.diff

diff --git a/lib/facts/tunnelbroker.js b/lib/facts/tunnelbroker.js
new file mode 100644
--- /dev/null
+++ b/lib/facts/tunnelbroker.js
@@ -0,0 +1,9 @@
+// @flow
+
+import { isDev } from '../utils/dev-utils.js';
+
+const tunnnelbrokerURL: string = isDev
+ ? 'ws://localhost:51001'
+ : 'https://tunnelbroker.commtechnologies.org';
+
+export { tunnnelbrokerURL };
diff --git a/lib/tunnelbroker/tunnelbroker-context.js b/lib/tunnelbroker/tunnelbroker-context.js
--- a/lib/tunnelbroker/tunnelbroker-context.js
+++ b/lib/tunnelbroker/tunnelbroker-context.js
@@ -3,6 +3,7 @@
import invariant from 'invariant';
import * as React from 'react';
+import { tunnnelbrokerURL } from '../facts/tunnelbroker.js';
import { type TunnelbrokerMessage } from '../types/tunnelbroker/messages.js';
import type { ConnectionInitializationMessage } from '../types/tunnelbroker/session-types.js';
@@ -31,9 +32,35 @@
};
function TunnelbrokerProvider(props: Props): React.Node {
- const { children } = props;
- const [connected] = React.useState(false);
+ const { children, initMessage } = props;
+ const [connected, setConnected] = React.useState(false);
const listeners = React.useRef<Set<TunnelbrokerSocketListener>>(new Set());
+ const socket = React.useRef<?WebSocket>(null);
+
+ React.useEffect(() => {
+ if (connected || !initMessage) {
+ return;
+ }
+
+ const tunnelbrokerSocket = new WebSocket(tunnnelbrokerURL);
+
+ tunnelbrokerSocket.onopen = () => {
+ tunnelbrokerSocket.send(JSON.stringify(initMessage));
+ };
+
+ tunnelbrokerSocket.onclose = () => {
+ setConnected(false);
+ console.error('Connection to Tunnelbroker closed');
+ };
+ tunnelbrokerSocket.onerror = e => {
+ console.error('Tunnelbroker socket error:', e.message);
+ };
+ tunnelbrokerSocket.onmessage = (event: MessageEvent) => {
+ console.log('Received message:', event.data);
+ };
+
+ socket.current = tunnelbrokerSocket;
+ }, [connected, initMessage]);
const sendMessage: () => Promise<void> = React.useCallback(() => {
return new Promise(() => {});

File Metadata

Mime Type
text/plain
Expires
Sat, Nov 16, 11:45 AM (12 h, 14 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2497697
Default Alt Text
D9607.id32626.diff (2 KB)

Event Timeline