flutter_websocket_manager 1.0.2 copy "flutter_websocket_manager: ^1.0.2" to clipboard
flutter_websocket_manager: ^1.0.2 copied to clipboard

Simplifies WebSocket management in Flutter, handling messages, errors, and connection states seamlessly.

example/main.dart

// import 'package:flutter_websocket_manager/flutter_websocket_manager.dart';
import 'package:flutter_websocket_manager/flutter_websocket_manager.dart';

void main() {
  // Create a FlutterWebSocketManager instance with optional headers and query parameters
  final wsManager = FlutterWebSocketManager(
    "wss://example.com/ws",
    queryParameters: {
      'token': '12345',  // You can add query parameters here
    },
    headers: {
      'Authorization': 'Bearer your_access_token',  // You can add custom headers here
    },
  );

  // Connect to the WebSocket
  wsManager.connect();

  // Set a callback for receiving messages
  wsManager.onMessage((message) {
    print("Received message: $message");
  });

  // Set a callback for handling errors
  wsManager.onError((error) {
    print("Error: $error");
  });

  // To send a text message
  wsManager.sendMessage("Hello, server!");

  // To send a data message (JSON)
  wsManager.sendDataMessage({'type': 'ping'});

  // To get the current connection state
  switch (wsManager.state) {
    case SocketConnectionState.connected:
      print("WebSocket is connected.");
      break;
    case SocketConnectionState.disconnected:
      print("WebSocket is disconnected.");
      break;
    case SocketConnectionState.none:
      print("WebSocket connection state is none.");
      break;
  }

  // To disconnect
  wsManager.disconnect();
}
2
likes
0
points
215
downloads

Publisher

unverified uploader

Weekly Downloads

Simplifies WebSocket management in Flutter, handling messages, errors, and connection states seamlessly.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, mockito, web_socket_channel

More

Packages that depend on flutter_websocket_manager