在Flutter应用中使用Socket? [英] Using Socket in Flutter apps?

查看:735
本文介绍了在Flutter应用中使用Socket?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了Flutter应用。我需要将我的应用程序连接到本地网络套接字服务。如下所示,我可以使用telnet Connect,发送数据和从服务器接收数据。我使用Flutter web_socket插件和示例。我可以连接到服务器并发送数据,但是我无法捕获(或获取数据,它什么也没有显示。)数据。在Flutter的Google网上论坛中,有人建议我使用流而不是StreamBuilder。

I create a Flutter App. I need to connect my app to local network socket services. As shown below, I can use telnet Connect, Send data and Receive data from the server. I use Flutter web_socket plugin and example. I can connect to the server and send the data but I cannot catch (or get data, it doesn't show anything.) the data. In Flutter google groups one person advised me to use stream instead of StreamBuilder.

To send data I use;         Q101:_:49785:_:ABCDE
And receive data I get;     1:_:2:_:119351:_:NİYAZİ TOROS 

当我使用此示例时( https://flutter.io/cookbook/networking/web-sockets/ )我在我的套接字服务上遇到错误:

And when I use this example (https://flutter.io/cookbook/networking/web-sockets/) I am getting error on my socket service as:

Q: 28.06.2018 08:53:57->GET / HTTP/1.1
A: 28.06.2018 08:53:57 ->:1:_:1:_:FAIL1

示例:

Last login: Tue Jun 26 15:01:44 on ttys000
Niyazis-MBP:~ niyazitoros$ telnet
telnet> telnet 192.168.1.22 1024
Trying 192.168.1.22...
Connected to 192.168.1.22.
Escape character is '^]'.
Q101:_:49785:_:*************
1:_:2:_:119351:_:NİYAZİ TOROS

根据@Richard Heap建议:

Based on @Richard Heap suggestion:

import 'dart:async';
import 'dart:convert';
import 'dart:io';

void connect(InternetAddress clientAddress, int port) {
  Future.wait([RawDatagramSocket.bind(InternetAddress.anyIPv4, 0)]).then(
      (values) {
    RawDatagramSocket _socket = values[0];
    _socket.listen((RawSocketEvent e) {
      print(e);
      switch (e) {
        case RawSocketEvent.read:
          Datagram dg = _socket.receive();
          if (dg != null) {
            dg.data.forEach((x) => print(x));
          }
          _socket.writeEventsEnabled = true;
          break;
        case RawSocketEvent.write:
          _socket.send(
              new Utf8Codec().encode('Hello from client'), clientAddress, port);
          break;
        case RawSocketEvent.closed:
          print('Client disconnected.');
      }
    });
  });
}

main(List<String> arguments) {
  print("Connecting to server..");
  var address = new InternetAddress('192.168.1.22');
  int port = 1024;
  connect(address, port);
}

我明白了:

/Users/niyazitoros/flutter/bin/cache/dart-sdk/bin/dart --enable-asserts --enable-vm-service:59683 /Users/niyazitoros/IdeaProjects/github/untitled/bin/main.dart
Observatory listening on http://127.0.0.1:59683/

Connecting to the server.
RawSocketEvent.write


推荐答案

正如attdona所述,

As attdona mentioned,


您的服务器不会使用websocket协议,但会公开纯TCP套接字。

Your server does not speak the websocket protocol but it exposes a plain tcp socket.

因此,您需要一个TCP套接字,并且有一个关于 Sockets ServerSockets ,您可以在此处找到。

So you need a TCP socket and there is a great tutorial on Sockets and ServerSockets which you can find here.

这是一个摘要:

import 'dart:io';
import 'dart:async';

Socket socket;

void main() {
   Socket.connect("localhost", 4567).then((Socket sock) {
   socket = sock;
   socket.listen(dataHandler, 
      onError: errorHandler, 
      onDone: doneHandler, 
      cancelOnError: false);
   }).catchError((AsyncError e) {
      print("Unable to connect: $e");
   });
   //Connect standard in to the socket 
   stdin.listen((data) => socket.write(new String.fromCharCodes(data).trim() + '\n'));
}

void dataHandler(data){
   print(new String.fromCharCodes(data).trim());
}

void errorHandler(error, StackTrace trace){
   print(error);
}

void doneHandler(){
   socket.destroy();
}

这篇关于在Flutter应用中使用Socket?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆