添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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

Is there any reason a WebSocket wouldn't work? I get crickets, while the same code works from the main RN app.

 <WebView
        originWhitelist={['*']}
        injectedJavaScript={`
var exampleSocket = new WebSocket('ws://localhost:3000')
exampleSocket.onopen = function (event) {
  window.ReactNativeWebView.postMessage("onopen")
exampleSocket.onerror = (e) => {
  window.ReactNativeWebView.postMessage('onerror')
exampleSocket.onmessage = (e) => {
  window.ReactNativeWebView.postMessage('onmessage')
        source={{ html: '<html><body></body></html>' }}
          

Hello 👋, this issue has been opened for more than 2 months with no activity on it. If the issue is still here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solutions and if you found one, please open a pull request! You have 7 days until this gets closed automatically

Hello 👋, this issue has been opened for more than 2 months with no activity on it. If the issue is still here, please keep in mind that we need community support and help to fix it! Just comment something like still searching for solutions and if you found one, please open a pull request! You have 7 days until this gets closed automatically

I am having the same issue.

If I open the webview url in my macbook chrome, server catches the socket connection.

But nothing hits the server when I open it on my RN webview..

Its working on my side (tested with normal XML/Data-Requests and JS-SIP WebRTC)
when adding useWebView2={true}

Broke it down to this, maybe it helps someone;
return ( <WebView useWebView2={true} mixedContentMode={'always'} originWhitelist={['https://*', 'wss://*']} source={{ uri: src }} /> );

I was able to resolve this by updating iOS on my testing device to iOS 16+. There was a weird issue in iOS 15 with the way Safari handled WebSockets. I was seeing the Web Sockets work fine in the desktop browser , and on Android - but on mobile Safari they did not work and never connected. After updating to iOS 16+ the Web Sockets worked as they should on my iOS device. This is not a bug within react-native-webview.

Here is a Github issue referencing the issues with iOS 15 and RN WebView: #2281

Websockets were working when loading exactly the same code from a remote URL but as soon as I moved it to local file in ios/android folder I got a websocket close 1006 event, including on iOS 16.4.

Nothing such as originWhitelist=['*'] or mixedContentMode='always' or useWebView2 helps, it just doesn't connect in iOS or Android with local files for some reason. Testing on react-native-webview 12.1.0 + 13.3.0

This worked for me (socketio):

const WebViewComponent = ({ navigation, webAppUrl, setIsWebViewLoading }) => {
  const handleWebMessageEvent = (data) => {
  return (
    <WebView
      startInLoadingState={true}
      onLoad={() => setIsWebViewLoading(false)}
      incognito={false}
      scalesPageToFit={true}
      androidHardwareAccelerationDisabled={true}
      source={{
        uri: webAppUrl,
      onMessage={event => handleWebMessageEvent(event.nativeEvent.data)}
      useWebView2={true}
      mixedContentMode={'always'}
      originWhitelist={['*']}
export default WebViewComponent;