import 'package:web_socket_channel/web_socket_channel.dart'; import 'package:web_socket_channel/status.dart' as status; import 'package:flutter/material.dart'; class WebSocketScreen extends StatelessWidget { const WebSocketScreen({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('WebSocket Example'), leading: IconButton( icon: const Icon(Icons.arrow_back), onPressed: () => Navigator.pop(context), ), ), body: Center( child: Column( children: [ ElevatedButton( onPressed: () async { connectButtonClicked(); }, child: const Text('Connect to WebSocket'), ), ElevatedButton( onPressed: () async { sendButtonClicked(); }, child: const Text('Send Message'), ), ], ), ), ); } } void connectButtonClicked() async { final wsUrl = Uri.parse('ws://47.76.151.238:82/ws'); final channel = WebSocketChannel.connect(wsUrl); await channel.ready; channel.stream.listen((message) { print('Received: $message'); }); await Future.delayed(Duration(seconds: 2)); channel.sink.add('Hello World!'); print('Sent: Hello World!'); await Future.delayed(Duration(seconds: 2)); channel.sink.close(status.goingAway); print('Connection closed'); } void sendButtonClicked() async {}