jamiewest / signalr_core

ASP.NET Core SignalR Dart Client
https://pub.dev/packages/signalr_core
MIT License
90 stars 62 forks source link

Exception: Starting connection with transfer format 'TransferFormat.text'. #15

Closed xuanhe168 closed 4 years ago

xuanhe168 commented 4 years ago

{Error info}_20200605140836

code:

class HomeState extends State<MyHomePage> {
  StringBuffer remoteMsg = StringBuffer();
  HubConnection hubConnection = HubConnectionBuilder().withUrl('http://xxx.xxx/:8000/api/chatHub',
      HttpConnectionOptions(
        transport:HttpTransportType.none,
        logging: (level, message) => print("message: $message"),
        accessTokenFactory:() => Future.value("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiYWRtaW4iLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1laWRlbnRpZmllciI6ImFkbWluIiwianRpIjoiMSIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvZXhwaXJhdGlvbiI6IjYvNS8yMCAyOjQwOjMzIFBNIiwiaHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS93cy8yMDA4LzA2L2lkZW50aXR5L2NsYWltcy9yb2xlIjoiQWRtaW4iLCJuYmYiOjE1OTEzMzU2MzMsImV4cCI6MTU5MTMzOTIzMywiaXNzIjoiT0EuUlNJLkJhY2tlbmQiLCJhdWQiOiJ3ciJ9.gKaIBDSF0MLJ5b6j7iiWKCeVvQJPMkqYJOuwPW_Yncw")
      )).build();

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

  init() async {
    hubConnection.on('ReceiveMessage', (message) {
      setState(() {
        message.forEach((v) => remoteMsg.write(v));
        print(message.toString());
      });
    });
    try{
      await hubConnection.start();
      print("连接成功...");
    }catch(e){
      print("连接失败...");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text("message from server: ${remoteMsg.toString()}"),
          TextField(
            decoration:InputDecoration(
              hintText:"Enter some text",
            ),
            onSubmitted:((v)async => await hubConnection.invoke('SendMessage', args: ['Flutter', v])),
          ),
        ],
      ),
    );
  }
}
jamiewest commented 4 years ago

Possibly related to #14, also I don't think you need to return a Future to the accessTokenFactory.

xuanhe168 commented 4 years ago

I added ''transport: HttpTransportType.longPolling,'' to HttpConnectionOptions solved my problem.