Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working in React Native #633

Open
zju1 opened this issue Jun 14, 2024 · 1 comment
Open

Not working in React Native #633

zju1 opened this issue Jun 14, 2024 · 1 comment

Comments

@zju1
Copy link

zju1 commented Jun 14, 2024

This is my code:

StompContainer.tsx

import { PropsWithChildren, useEffect } from "react";
import { useTranslation } from "react-i18next";
import { useAuth } from "../../hooks/useAuth";
import { useAppDispatch } from "../../app/store/store.config";
import { envVars } from "../../constants/envVars";
import { Client, IMessage } from "@stomp/stompjs";
import { setSync } from "../../app/store/slices/auth.slice";

const client = new Client({
  brokerURL: envVars.SOCKET_URL,
  forceBinaryWSFrames: true,
  appendMissingNULLonIncoming: true,
  splitLargeFrames: true,
  webSocketFactory() {
    return new WebSocket(envVars.SOCKET_URL, ["wss", "ws", "https", "http"]);
  },
  onConnect(frame) {
    console.log(frame);
  },
  onStompError(frame) {
    console.log("STOMP ERROR", frame);
  },
  onWebSocketError: (evt) => {
    console.log("WebSocket ERROR", evt);
  },
});

export function StompContainer(props: PropsWithChildren) {
  const { user, sync } = useAuth();
  const dispatch = useAppDispatch();
  const { t } = useTranslation();

  useEffect(() => {
    if (user && !sync) {
      client.activate();
    }
  }, [dispatch, t, user, sync]);

  return props.children;
}

AndroidManifest.xml

<application
     ...
      android:usesCleartextTraffic="true"
      ...">

And the error is:

{"isTrusted": false, "message": "Expected HTTP 101 response but was '403 '"}

Tried everything on the internet. Not working.

@flohhhh
Copy link

flohhhh commented Jul 8, 2024

Hi @zju1 , in adding stompVersions it worked.

const client = new StompJs.Client({
      brokerURL,
      connectHeaders: headers,
      stompVersions: new Versions([Versions.V1_2]), // <---------- Add this
      reconnectDelay: RECONNECT_DELAY,
      forceBinaryWSFrames: true, // Necessary to Android
      debug: (msg) => console.log(msg),
      logRawCommunication: true,
      onWebSocketError: (e) => console.log('-----e', e),
      onChangeState: (state) => console.log('-----sate', state),
    })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants