felangel / web_socket_client

A simple WebSocket client for Dart which includes automatic reconnection logic.
https://pub.dev/packages/web_socket_client
MIT License
150 stars 32 forks source link

Safari: 'null' is not a valid value for binaryType; binaryType remains unchanged. #38

Closed chrallard closed 4 months ago

chrallard commented 7 months ago

Description

'null' is not a valid value for binaryType; binaryType remains unchanged.

This causes frequent reconnects and messages aren't coming through the messages stream. Seems to only happen in Safari from what I can tell so far.

Steps To Reproduce

name: socket_test
description: A new Flutter project.
publish_to: 'none'
version: 0.1.0

environment:
  sdk: '>=3.1.5 <4.0.0'

dependencies:
  flutter:
    sdk: flutter
  web_socket_client: ^0.1.0

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: ^2.0.0

flutter:
  uses-material-design: true
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:web_socket_client/web_socket_client.dart' as ws;

void main() {
  runApp(const MainApp());
}

class MainApp extends StatefulWidget {
  const MainApp({super.key});

  @override
  State<MainApp> createState() => _MainAppState();
}

class _MainAppState extends State<MainApp> {
  ws.WebSocket? _socket;
  StreamSubscription? _socketMessageSub;
  StreamSubscription? _socketConnectionSub;

  @override
  void initState() {
    startSocket();
    super.initState();
  }

  @override
  void dispose() {
    _socketConnectionSub?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
        body: Center(
          child: Text('Hello World!'),
        ),
      ),
    );
  }

  void startSocket() async {
    print('startSocket()');

    _socket = ws.WebSocket(
      Uri.parse(
        'wss://socketsbay.com/wss/v2/1/demo/',
      ),
    );
    _listenToSocketConnection();
  }

  void _listenToSocketConnection() {
    print('_listenToSocketConnection()');

    _socketConnectionSub = _socket?.connection.listen(
      _onSocketConnectionChanged,
    );
  }

  void _listenToSocketMessages() async {
    print('_listenToSocketMessages()');

    await _socketMessageSub?.cancel();

    _socketMessageSub = _socket?.messages.listen(
      _onSocketMessageReceived,
    );
  }

  void _onSocketConnectionChanged(ws.ConnectionState cs) {
    print('_onSocketConnectionChanged() - $cs');

    if (cs is ws.Connected || cs is ws.Reconnected) {
      _listenToSocketMessages();
    }
  }

  void _onSocketMessageReceived(dynamic data) {
    print('_onSocketMessageReceived() - $data');
  }
}

Screenshots

Screenshot 2024-02-05 at 9 09 00 AM

felangel commented 4 months ago

Hi @chrallard đź‘‹ I'm not able to reproduce this with Version 16.1 (18614.2.9.1.12) of Safari. Closing for now but if this is still an issue feel free to let me know what version of Safari you're using and I'm happy to take a closer look.