如何在Flutter中实现Socket IO客户端数据

时间:2018-12-06 10:07:07

标签: android dart flutter

您好,我想用浮动的方式实现Socket IO,为此,我使用下面的示例,但是我看不到向特定通道发送的数据,我在仿真器和物理设备上都使用了,但是单词“我正在向聊天频道发送的“ TEST”(测试)消息没有在控制台上打印,请帮助我。

示例:

import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:socket_flutter_plugin/socket_flutter_plugin.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

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

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    try {
      SocketFlutterPlugin myIO = new SocketFlutterPlugin();
      myIO.socket("http://10.2.2.22:9006");
      myIO.connect();
      String jsonData =
              '{"content":"test"}';
      myIO.emit("chat",jsonData);
      myIO.on("chat",(data){
        debugPrint(data.toString());
      });
    } on PlatformException {

      _platformVersion = 'Failed to get platform version.';
    }
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: const Text('Plugin example app'),
        ),
        body: new Center(
          child: new Text('Running on: $_platformVersion\n'),
        ),
      ),
    );
  }
}

输出::

Launching lib\main.dart on Android SDK built for x86 in debug mode...
Built build\app\outputs\apk\debug\app-debug.apk.
D/SocketIO (18242): Socket initialised
D/SocketIO  (18242): Connected
D/SocketIO  (18242): Pushing {"content":"test"} on topic chat
D/SocketIO  (18242): registering to chat topic
D/NetworkSecurityConfig(18242): No Network Security Config specified, using platform default
D/        (18242): HostConnection::get() New Host Connection established 0x9fc9d980, tid 18264
D/EGL_emulation(18242): eglMakeCurrent: 0x9c1f32e0: ver 2 0 (tinfo 0x9fc83550)

2 个答案:

答案 0 :(得分:0)

我想说的是您在听之前正在发出声音,所以您听不到您正在发出的声音(因为您没有在听)...

尝试像这样反转emiton的顺序

  myIO.on("chat",(data){
    debugPrint(data.toString());
  });
  myIO.emit("chat",jsonData);

并将myIO的声明移至州一级:

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  SocketFlutterPlugin myIO;

  // ...

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    try {
      myIO = new SocketFlutterPlugin();
      //...
    }
    //...
   }
}

答案 1 :(得分:0)

使用此软件包flutter_socket_io,我能够连接到Socket IO服务器。

import 'package:flutter_socket_io/flutter_socket_io.dart';
import 'package:flutter_socket_io/socket_io_manager.dart';

...

class _MyAppState extends State<MyApp> {

    SocketIO socket;

    ...

    void _onInitSocketIO() {
        socket = SocketIOManager().createSocketIO("http://10.2.2.22:9006", "/", query: "", socketStatusCallback: _onSocketStatus);
        socket.init();
        socket.connect();
    }

    void _onSocketStatus(dynamic data) {
        // If socket connects successfully
        if (data == "connect") {
            // Send message to server on the 'chat' event.
            String jsonData = '{"content":"test"}';
            socket.sendMessage('chat', jsonData);

            // Subscribe to the 'chat' event.
            socket.subscribe("chat", _onReceiveChatEvent);
        }
    }

    void _onReceiveChatEvent(dynamic data) {
        debugPrint(data);
    }

}