how to use dart WebSocket on Flutter

601 views
Skip to first unread message

Niyazi Toros

unread,
Jun 28, 2018, 3:07:47 AM6/28/18
to Flutter Dev


Hi,

Below is dart websocket class info. Its look perfect. My question is how to use dart WebSocket on Flutter?
What step should I take?


https://api.dartlang.org/stable/1.24.3/dart-html/WebSocket-class.html


// To use a WebSocket in your web app, first create a WebSocket object, 

// passing the WebSocket URL as an argument to the constructor.

var webSocket = new WebSocket('ws://127.0.0.1:1337/ws');


// To send data on the WebSocket, use the send method.

if (webSocket != null && webSocket.readyState == WebSocket.OPEN) {

  webSocket.send(data);

} else {

  print('WebSocket not connected, message $data not sent');

}




// To receive data on the WebSocket, register a listener for message events.

webSocket.onMessage.listen((MessageEvent e) {

  receivedData(e.data);

});

Maik Mewes

unread,
Jun 28, 2018, 3:12:15 AM6/28/18
to Niyazi Toros, Flutter Dev
This is the official sample and at least works for older android versions and in the emulator for ios and android:

--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Niyazi Toros

unread,
Jun 28, 2018, 3:18:57 AM6/28/18
to Flutter Dev
Thanks Main, the link is misleading or at least its documentation is not completed.

Below is my telnet connection. I can send data as  Q101:_:49785:_:************* and receive as  1:_:2:_:119351:_:NİYAZİ TOROS

Problem is that when I use channel: IOWebSocketChannel.connect('ws://echo.websocket.org'), I can connect to server, I can send the data and everything's halt there. It doesn't return anything. I try not use SreamBuilder and try to follow dart example and failed.

After I connect to server I will send my request based on server socket format and receive some data.

Any help on this please?




 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

Niyazi Toros

unread,
Jun 28, 2018, 3:54:35 AM6/28/18
to Flutter Dev

I try to test and find where is the error coming from. And I found that data that I send to socket is not correct:

Q: 28.06.2018 08:53:57->GET / HTTP/1.1.      (the correct send data will be: Q101::49785::ABCDE)
A: 28.06.2018 08:53:57 ->:1::1::FAIL1

So how can I emit GET / HTTP/1.1. not to send to socket?

Reply all
Reply to author
Forward
0 new messages