Reputation: 3369
I am using socket.io-client.java and socket.io 1.2.1 on my node server for my android project and the android socket connects with the server fine but after some minutes it automatically disconnect and reconnect repeatedly. i can't figure out the problem, can someone help me?
i am using socket.io-client-0.1.1.jar, engine.io-client-0.2.1.jar and Java-WebSocket-1.3.0.jar libraries.
here is the java code
private void socketTest() throws URISyntaxException{
socket = IO.socket("http://192.168.169.2:8082");
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.emit("test", "awesome");
}
}).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {}
});
socket.connect();
}
and here is the server side code
io.on('connection', function (socket) {
console.log('a user connected');
socket.on('disconnect', function () {
console.log('user disconnected');
});
socket.on('test',function(msg){
console.log("This is "+msg);
});
});
and here is the log screenshot
Upvotes: 8
Views: 5647
Reputation: 155
For me the problem was that I used Socket.IO incorrectly on server side (Flask) and HTTP long polling was used under the hood instead of websockets. Turns out, Flask webserver doesn't support websockets, so I had to use eventlet
WSGI server:
run.py
from myapp import create_app, socketio
import eventlet
eventlet.monkey_patch()
config = 'config.py'
app = create_app(config)
if __name__ == '__main__':
socketio.run(app)
extensions.py
from flask_jwt_extended import JWTManager
...
from flask_socketio import SocketIO
jwt = JWTManager()
...
socketio = SocketIO(logger=True, async_mode='eventlet')
__init__.py
import eventlet
from eventlet import wsgi
from myapp.extensions import jwt, socketio
def create_app(config_filename):
app = Flask(__name__, instance_relative_config=True)
app.config.from_pyfile(f"../{config_filename}")
app.config.from_pyfile(config_filename)
register_extensions(app)
wsgi.server(eventlet.listen(('', 5000)), app)
def register_extensions(app):
socketio.init_app(app)
On client side:
class MessagesRepository @Inject constructor(
private val prefs: SharedPreferences
) {
private val options = IO.Options.builder()
.setTransports(arrayOf(WebSocket.NAME))
.setExtraHeaders(
mapOf("Authorization" to listOf(prefs.getString(ACCESS_TOKEN, "")),
"Content-type" to listOf("application/json")))
.build()
private val socket = IO.socket(getBaseUrlForCurrentDevice(), options)
}
Upvotes: 0
Reputation: 350
I resolved this problem by adding an option with infinity timeout to socket.io
IO.Options options = new IO.Options();
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
.connectTimeout(0, TimeUnit.MILLISECONDS)
.readTimeout(0, TimeUnit.MILLISECONDS)
.writeTimeout(0, TimeUnit.MILLISECONDS);
options.callFactory = clientBuilder.build();
socket = IO.socket(URL, options);
Upvotes: 8
Reputation: 1014
I have fetching same issue on socket.io-client:1.0.0
client library in my Android project but after the Downgrade the socket.io
version its work fine.Used below client version might be helpful you.
compile('io.socket:socket.io-client:0.8.3') {
exclude group: 'org.json', module: 'json'
}
Upvotes: 4