套接字错误:WebSocketException:连接到'http://192.168.42.48:8000/api/users/?EIO= 4 &transport =WebSocket#“未升级到WebSocket

7xllpg7q  于 2023-10-20  发布在  其他
关注(0)|答案(1)|浏览(275)

我在flutter应用程序中应用了本地Web Socket API,它抛出异常,尝试了很多但找不到任何解决方案,API在Laravel中,在Laravel方面我也使用Socket.io包和Laravel-echo,这里是我的flutter连接代码

void connectToSocket() {
    try {
      /// You can change your localhost [IPV4] here.
      String localhostIp = '192.168.42.48';
      String socketUrl = 'http://$localhostIp:8000';
      String apiUrl = 'http://$localhostIp:8000/api/users';
      String socketPath = apiUrl.replaceAll(socketUrl, '');

      socket = IO.io(
        socketUrl,
        <String, dynamic>{
          'transports': ['websocket'],
          'autoConnect': false,
          'path': socketPath,
          'Connection': 'Upgrade',
          'Upgrade': 'websocket',
          'Sec-WebSocket-Version': '4.6.2',
          'Sec-WebSocket-Extensions': 'client_max_window_bits',
        },
      );

      socket!.onConnect((_) {
        log('Connected to socket!');
      });

      socket!.onError((error) {
        log('Socket error: $error');
      });

      socket!.on('newMessage', (data) {
        log('New message: $data');
      });

      socket!.connect();
    } catch (e) {
      log('Error connecting to socket: $e');
    }
  }
62lalag4

62lalag41#

我认为,socket url需要这样:ws:192.168.42.48:{somePort}/API/users,not http.

相关问题