socket.io-client-dart icon indicating copy to clipboard operation
socket.io-client-dart copied to clipboard

Emit not working in loops

Open yashwant1999 opened this issue 4 years ago • 1 comments

The problem : when i use emit inside loop it only trigger for first time and after that it suck inside the loop need to be breaked in order to get all those to the server [but not in contineous manner].

The same problem occur in those vanilla javascript libraries and some of them insist to use socketio.sleep(0) in avoid the blocking. But i don't know in referance of dart.

for referance : https://github.com/miguelgrinberg/python-socketio/issues/413 Server :

import 'package:socket_io/socket_io.dart';
import 'dart:io';


main() {
  // Dart server
  var io = Server();
  io.on('connection', (client) {
    print('Client is Connected !!');
    
     client.on('msg', (data) {
      print('Data is : $data');
           });
    });

  io.listen(8080);
}

client :

import 'package:socket_io_client/socket_io_client.dart' as IO;
import 'dart:io';

main() {
  // Dart client
  var data ;
  var socket = IO.io('http://localhost:8080', <String, dynamic>{
    'transports': ['websocket'],
  });

 
  socket.onConnect((_) {
    print('Connected to Server !!');

    while (true) {
      stdout.wrtie('Enter your message : ');
      data = stdin.readLineSync()!;
      socket.emit('msg', data);
      if(data == 'q'){
      	break;
      }
    }
  });

  socket.onDisconnect((_) => print('disconnect'));
}

yashwant1999 avatar Aug 10 '21 11:08 yashwant1999

FYI: https://api.dart.dev/stable/2.13.4/dart-async/Future/Future.delayed.html

jumperchen avatar Aug 20 '21 07:08 jumperchen