Skip to content

How to pass query parameters in react native WebSocket connection #16041

@KakarN

Description

@KakarN

Is this a bug report?

Yes

Have you read the Contributing Guidelines?

Yes

Environment

Packages: (wanted => installed)
"react": "16.0.0-alpha.12",
"react-native": "^0.47.0",

Target Platform: iOS, android

Steps to Reproduce

I have a server where I can make a WebSocket connection. When a device makes a WebSocket connection (when the app starts/splash screen), the server looks for the query parameter in the URL, which has the auth token of the user and if the token is correct, the connection is approved, else connection is rejected.

This is how the code looks on the react native client side:

const socket = new WebSocket(`ws://${website}/?token=${value}`);
socket.onmessage = (messageEvent) => {
    console.log('>>> socket on message:', messageEvent.data);
};
socket.onopen = function () {
    socket.send("hello world connection");
};
socket.onclose = () => {
    console.log('Socket disconnected');
};

This works fine in my iOS emulator, and the iOS emulator has a WebSocket connection to mine server. However, I have an android phone (testing via LAN), and when I open the app, I get an error saying:

Unable to set ws://192.xxx.xx.xx/token=aut sfiseft2sefsefs..... as default origin header

But, if I remove the query param from the URL

const socket = new WebSocket(ws://${website});

This works fine, but since I cannot pass the token in the query params, I get an error from the server.

How can I pass query parameters to the WebSocket connection in android? Am I missing something?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions