Flutter SocketIo Client TimeOut - flutter

I'm trying to add socket io to a flutter project. I'm using socket_io_client 2.0.0-beta.4-nullsafety.0 but when I try to connect to server's socket it just refuse the connection and throw a timeout error.
Here's the code im using:
_connectSocket() {
Socket socket = io('SERVER IP',
OptionBuilder()
.enableAutoConnect()
.build()
);
socket.onConnecting((data) => print("conecting socket..."));
socket.onConnectError((data) => print("error : "+data.toString()));
socket.onConnectTimeout((data) => print(data.toString()));
}
Has anyone been through this?
Thanks in advance

socket = IO.io("ip sever",<String, dynamic>{
"transports": ["websocket"],
"autoConnect": false,
'extraHeaders': {'foo': 'bar'},
}); socket.connect();
// socket = await IO.io('ip server',
// OptionBuilder()
// .setTransports(['websocket']).build());
socket.onConnect((_) => print('connect'));
socket.onConnect((_) {
print('connect');
});
socket.onConnectError((data) => print( 'error : '+ data.toString() ));

Related

How to check if the tcp socket is still connected in Flutter?

I have connected to my server as follows:
Socket socket = await Socket.connect('xxx', xxx)
But now I want to check if the socket is still connected. It would be even better if I have a listener that tells me when the connection is broken.
Thanks already!
Leonard
Listen to onDone in its stream
socket.stream.listen(
(dynamic message) {
debugPrint('message $message');
},
onDone: () {
debugPrint('socket closed');//if closed you will get it here
},
onError: (error) {
debugPrint('error $error');
},
);

Flutter not able connect to websocket

When I try to connect to my nodejs server it is showing me this error-
connect_error: [{"cause":{"detailMessage":"Control frames must be final.","stackTrace":[],"suppressedExceptions":[]},"detailMessage":"websocket error","stackTrace":[],"suppressedExceptions":[]}]
I used flutter_socket_io for socket connection
My flutter code
socketIO = SocketIOManager().createSocketIO(
websocketUrl, '/', socketStatusCallback: (status) {
print("web socket -------------status");
print(status);
if (status.toString() == "connect") {
print("connected to server with websocket------------------------");
// socketIO.sendMessage("socket request", jsonEncode({"id": id}));
}
});
socketIO.init();
socketIO.connect().then((value){
print('socket connect');
});

Snapshot return array

I've created a web socket server with Node.js to connect two Flutter apps. I can post a message to server but when I listen to it on WebApp i receive an array [79,101] instead message (Oi). How can I solve it?
Sink Message
void _sendMessage(data) {
widget.channel.sink.add('Oi');
}
Cliente Stream Builder
StreamBuilder(
stream: channel.stream,
builder: (context, snapshot) {
return Text(snapshot.hasData ? '${snapshot.data}' : 'Null');
},
)
Node.js Server
const WebSocket = require('ws');
// start the server and specify the port number
const port = 8080;
const wss = new WebSocket.Server({ port: port });
console.log(`[WebSocket] Starting WebSocket server on localhost:${port}`);
wss.on('connection', (ws, request) => {
const clientIp = request.sock.remoteAddress;
console.log(`[WebSocket] Client with IP ${clientIp} has connected`);
ws.send('Connected!');
// Broadcast aka send messages to all connected clients
ws.on('message', (message) => {
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(message); } })
console.log(`[WebSocket] Message ${message} was received`); });
});
This might not be the solution you are looking for but I would try and convert the response via String.fromCharCodesince the response might be ASCII characters to begin my way of debugging.

Flutter: simple socket.io testing - Websocket pending

I have a super simple socket.io node server like below
// Socket!
io.on("connection", (socket) => {
console.log("a user connected");
socket.on("msg", (aaa) => {
console.log(aaa);
});
socket.on("fromServer", (_) => print(_));
console.log(socket.handshake.query)
});
And, here is my Flutter code to connect to the server
// Dart client
IO.Socket socket = IO.io(
'http://192.168.219.102:7199',
IO.OptionBuilder()
.setTransports(['polling'])
.disableAutoConnect()
.setQuery({"hee": 'asdf'})
.build());
socket.connect();
socket.onConnect((_) {
print(_);
});
On the network tab, I see two requests
101 HTTP 490ms
101 WS Pending
When I connect to the socket.io server from the other node.js server, I see the console.log of a user connected on the terminal. However, I see nothing from Flutter.
What can I do to receive the socket message on the server and connect the device.

AEDES SERVER DOES NOT CONNECT TO CLIENT

I want to make a simple client server example with visual studio code. For my mqtt client instance, mosca didn't work. So I created a server with aedes. However, it is not possible to connect to client.js at the moment. I'm sure it's missing on the server side, but I'm not sure how to fix it. I'm very new to this. my codes are below.
Server;
const aedes = require('aedes')()
const server = require('net').createServer(aedes.handle)
const httpServer = require('http').createServer()
const ws = require('websocket-stream')
const port = 1883
const wsPort = 3000
server.listen(port, function () {
console.log('server started and listening on port ', port)
})
ws.createServer({ server: httpServer }, aedes.handle)
httpServer.listen(wsPort, function () {
console.log('websocket server listening on port ', wsPort)
})
Client;
var mqtt = require('mqtt');
var client = mqtt.connect('mqtt://192.168.43.40:1883');
client.subscribe('new-user');
client.on('connect', function() {
console.log('connected!');
client.publish('new-user', 'Cansu-' + Math.ceil(Math.random() * 10));
});
client.on('message', function(topic, message) {
console.log(topic, ' : ', message.toString());
client.end();
});
Thank You!!!