扑听本地nodejs的socket.io通道

时间:2019-12-24 22:12:52

标签: node.js flutter socket.io-1.0

我正在尝试收听socket.io公共频道(从nodejs本地服务器创建)。

我正在使用android模拟器进行测试。

main.dart中的代码段:

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

    IO.Socket socket = IO.io('http://10.0.2.2:3000');

        socket.on('connect', (_) {
         print('connect');
         socket.emit('msg', 'test');
        });
        socket.on('event', (data) => print(data));
        socket.on('disconnect', (_) => print('disconnect'));
        socket.on('fromServer', (_) => print(_));

这是我要收听的频道:

global.io.emit('news', { hello: 'world' });

我尝试了adhara_socket_io.dart'和该程序包(https://github.com/kakajansh/echo),但没有成功。

感谢您的帮助。

更新

我正尝试使用此软件包(https://github.com/infitio/flutter_socket_io/blob/master/example/lib/main.dart

const String uri = 'http://127.0.0.1:3000/';


class _MyHomePageState extends State<MyHomePage> {
  _MyHomePageState();

  List<String> toPrint = ["trying to connect"];
  SocketIOManager manager;
  Map<String, bool> _isProbablyConnected = {};
  Map<String, SocketIO> sockets = {};

  @override
  void initState() {
    super.initState();
    manager = SocketIOManager();
    initSocket("default");

  }

initSocket(String identifier) async {
    setState(() => _isProbablyConnected[identifier] = true);
    SocketIO socket = await manager.createInstance(SocketOptions(
      //Socket IO server URI
        uri,
        nameSpace: (identifier == "namespaced")?"/adhara":"/",
        //Query params - can be used for authentication
        query: {
          "auth": "--SOME AUTH STRING---",
          "info": "new connection from adhara-socketio",
          "timestamp": DateTime.now().toString()
        },
        //Enable or disable platform channel logging
        enableLogging: false,
        transports: [Transports.WEB_SOCKET/*, Transports.POLLING*/] //Enable required transport
    ));
    socket.onConnect((data) {
      pprint("connected...");
      pprint(data);
      sendMessage(identifier);
    });
     socket.on('news', (data){   // hera is where you receive from `serve`
      print("news");
      print(data);
    });
    socket.onConnectError(pprint);
    socket.onConnectTimeout(pprint);
    socket.onError(pprint);
    socket.onDisconnect(pprint);
    socket.on("type:string", (data) => pprint("type:string | $data"));
    socket.on("type:bool", (data) => pprint("type:bool | $data"));
    socket.on("type:number", (data) => pprint("type:number | $data"));
    socket.on("type:object", (data) => pprint("type:object | $data"));
    socket.on("type:list", (data) => pprint("type:list | $data"));
    socket.on("message", (data) => pprint(data));
    socket.connect();
    sockets[identifier] = socket;
  }
  sendMessage(identifier) {
    if (sockets[identifier] != null) {
      pprint("sending message from '$identifier'...");
      sockets[identifier].emit("message", [
        "Hello world!",
        1908,
        {
          "wonder": "Woman",
          "comics": ["DC", "Marvel"]
        },
        {
          "test": "=!./"
        },
        [
          "I'm glad",
          2019,
          {
            "come back": "Tony",
            "adhara means": ["base", "foundation"]
          },
          {
            "test": "=!./"
          },
        ]
      ]);
      pprint("Message emitted from '$identifier'...");
    }
  }

   bool isProbablyConnected(String identifier){
    return _isProbablyConnected[identifier]??false;
  }


pprint(data) {
    setState(() {
      if (data is Map) {
        data = json.encode(data);
      }
      print(data);
      toPrint.add(data);
    });
  }

但是,我遇到了以下错误:

io.socket.engineio.client.EngineIOException:websocket错误

谢谢

1 个答案:

答案 0 :(得分:0)

在android / app / src / main / AndroidManifest.xml中将android:usesCleartextTraffic =“ true”配置为标签的属性

例如:

<application
    android:name="io.flutter.app.FlutterApplication"
    android:label="adhara_socket_io_example"
    android:usesCleartextTraffic="true"
    android:icon="@mipmap/ic_launcher">
    <activity
        android:name=".MainActivity"...>...</activity>