Flutter socket.io连接到Node.js socket.io

时间:2019-12-12 13:55:42

标签: node.js flutter dart socket.io

这是我的服务器

class ChatbotServer {
  private http: Server;
  private io: socketIo.Server;

  constructor(app: express.Application, private nluService: NluService, private loggerService: LoggerService) {
    this.http = createServer(app);
    this.io = socketIo(this.http);

    this.setupSocketIo();
  }

  private setupSocketIo() {
    this.io.on("connection", socket => {
      new ChatSocketConnection(socket, this.nluService, this.loggerService);
    });
  }

  listen() {
    this.http.listen(port, () => console.log(`socket.io listening on port ${port}`));
  }
}

这是我扑朔迷离的客户

class MySocketApp extends StatefulWidget {
  @override
  _MySocketAppState createState() => _MySocketAppState();
}

enum ConnectionStatus { connected, disconnected }

class _MySocketAppState extends State<MySocketApp> {
  SocketIOManager manager = SocketIOManager();
  SocketIO socket;
  var status = ConnectionStatus.disconnected;



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

  void disconnectSocketConnections() async {
    await manager.clearInstance(socket);
    status = ConnectionStatus.disconnected;
    print("disconnected");
  }
  void setupSocketConnections() async {
    print("asd");

    socket = await manager.createInstance(SocketOptions('http://localhost:3001/'));
    socket.onConnect((data) {
      status = ConnectionStatus.connected;
      print("connected...");
    });
    socket.onConnectError((data) {
      print("Connection Error");
    });
    socket.onConnectTimeout((data) {
      print("Connection Timed Out");
    });
    socket.connect();
  }

我按照教程和adhara_socket_io pub中的示例进行操作,但是仍然无法连接套接字。 我有一个响应的Web应用程序,并且套接字连接没有任何问题。这意味着服务器端还可以,所以可能我在抖动部分中缺少某些东西。如果有人知道我会很感激的!

1 个答案:

答案 0 :(得分:1)

使用socket_io_client代替adhara_socket_io

pubspec.yaml文件中,添加软件包socket_io_client: ^0.9.4flutter_simple_dependency_injection: ^1.0.1

您可以像下面的代码一样创建Singleton服务(以避免类的多个实例):

import 'package:socket_io_client/socket_io_client.dart' as IO;
import 'package:tiche_flutter/config.dart';

class SocketService {
  IO.Socket socket;

  createSocketConnection() {
    socket = IO.io(config.socketUrl, <String, dynamic>{
      'transports': ['websocket'],
    });

    this.socket.on("connect", (_) => print('Connected'));
    this.socket.on("disconnect", (_) => print('Disconnected'));
  }
}

创建文件Dependecy_injection.dart

class DependencyInjection {
  Injector initialise(Injector injector) {
    injector.map<SocketService>((i) => SocketService(), isSingleton: true);
    return injector;
  }
}

创建文件app_initializer.dart

import 'package:flutter_simple_dependency_injection/injector.dart';
class AppInitializer {
  initialise(Injector injector) async {}
}

添加到您的main.dart

Injector injector;
void main() async {
  DependencyInjection().initialise(Injector.getInjector());
  injector = Injector.getInjector();
  await AppInitializer().initialise(injector);
  runApp(MyApp());
}

您需要运行createSocketConnection函数以在dart文件中创建连接。 因此,在您的文件中,编写代码:

final SocketService socketService = injector.get<SocketService>();
socketService.createSocketConnection();

如果创建了套接字连接,您将在终端中看到"Connected"

对我有用。