I am currently trying to conenct my flutter app to my socket.io server that I uploaded on heroku
I tried using both flutter_socket_io and socket_io_client but none is wokring. When I opened the logs on heroku server I am recieving a request for socket.io but for some reason nothing happens.
The Log
at=info method=GET path="/socket.io/?EIO=3&transport=websocket" host=quarter-monkey.herokuapp.com request_id=5b59d08e-1f85-4d40-b6f1-cd6b75c41ba1 fwd="41.47.20.90" dyno=web.1 connect=0ms service=2552ms status=101 bytes=129 protocol=https
My nodejs server code
require('./db/mongoose')
const express = require('express')
const bodyParser = require('body-parser')
const path = require('path')
const cors = require('cors')
const http = require('http')
const socketManager = require('./socket/socketManager');
//Different routes
const registerRouter = require('./routers/register.js')
const userRouter = require('./routers/user')
//Configuring the app
const app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}))
//#TODO Adjusting cors fore develpment
app.use(cors());
app.use('/register',registerRouter)
app.use('/user',userRouter)
const server = http.createServer(app);
socketManager(server);
server.listen(process.env.PORT, () => {
console.log('Server is Up on', process.env.PORT)
})
Socket Manger
const socketIO = require('socket.io')
module.exports = app => {
const io = socketIO(app);
io.on('connection', async socket => {
console.log("Connected");
});
return io;
};
My Flutter code
import 'package:socket_io_client/socket_io_client.dart' as client;
class SocketHandler {
static const _url = 'https://quarter-monkey.herokuapp.com/';
static client.Socket _socket;
static initialize() {
if (_socket != null) return;
_socket = client.io(
_url,
<String, dynamic>{
'transports': ['websocket'],
'autoConnect': false,
},
);
_socket.on('connect', (_) => print('Connected'));
_socket.on('reconnect_attempt', (_) => print('Reconnecting'));
_socket.on('connecting', (_) => print('Connecting'));
_socket.on('disconnect', (_) => print('Disconnected'));
_socket.on('connect_error', (_) => print(_));
_socket.on('error', (_) => print(_));
_socket.connect();
}
static emit(String event, {dynamic arguments}) {
initialize(); // Ensure it's initialized
_socket.emit(event, arguments ?? {});
}
static subscribe(String event, Function function) {
initialize(); // Ensure it's initialized
_socket.on(event, function);
}
static unsubscribe(String event, Function function) {
initialize(); // Ensure it's initialized
_socket.off(event, function);
}
}
Note
I keep getting connection timeout in flutter
question from:
https://stackoverflow.com/questions/65874815/socketio-flutter-cannot-connect-to-heroku 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…