我目前有一个设置,在其中运行一个Flask服务器,处理安卓手机和树莓派Node.js Socket.IO客户端之间的Websocket通信。安卓Socket.IO可以连接到Flask服务器,Node.js客户端也可以连接,并且我可以在所有三者之间发出消息,但是我的Android Studio Socket.IO客户端每5-10秒就会断开并重新连接。即使我离开导致它连接的活动,它仍然会不断地连接和断开与服务器的连接。以下是Android Studio中的Java代码:
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
public class ControlPage extends AppCompatActivity {
Button greenButton;
Button redButton;
private Socket mSocket; {
try {
mSocket = IO.socket("http://192.168.1.2:5000");
} catch (URISyntaxException e) {}
}
//private EditText mInputMessageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_control_page);
mSocket.connect();
mSocket.on(Socket.EVENT_CONNECT, onConnect);
greenButton = (Button) findViewById(R.id.sendButtonGreen);
greenButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
attemptSendGreen();
}
});
redButton = (Button) findViewById(R.id.sendButtonRed);
redButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
attemptSendRed();
}
});
}
在我的Flask代码中,服务器端的代码如下:
import flask
from flask_socketio import SocketIO, emit
from flask import request
import json
app = flask.Flask(__name__)
socketio = SocketIO(app)
clients = {}
sessions = {}
@app.route('/')
def hello():
return "<h1> Hello World! </h1>"
@socketio.on('connect')
def testConnect():
print('connected')
emit('connected')
@socketio.on('disconnect')
def testDisconnect():
userSID = request.sid
print('disconnected ' + str(userSID))
UserID = sessions[str(userSID)][0]
UserType = sessions[str(userSID)][1]
clients[UserID][UserType]=''
sessions.pop(str(userSID))
@socketio.on('config')
def handle_config(data):
print('received message: ' + str(data)+ str(type(data)))
userSID = request.sid
if type(data) == str:
jsonData = eval(data)
else:
jsonData = data
print(userSID)
if jsonData['USER_KEY'] in clients:
clients[jsonData['USER_KEY']][jsonData['USER_TYPE']]=userSID
else:
clients[jsonData['USER_KEY']] = {'USER':'',
'HARDWARE':''}
clients[jsonData['USER_KEY']][jsonData['USER_TYPE']]=userSID
sessions[str(userSID)] = [jsonData['USER_KEY'], jsonData['USER_TYPE']]
@socketio.on('transfer')
def transfer(data):
jsonData = eval(data)
print(jsonData)
UserID = jsonData['USER_KEY']
UserType = jsonData['USER_TYPE']
color = jsonData['MESSAGE']
#message = data['MESSAGE']
message = {'COLOR': color,
'STATE': 1
}
jsonMessage = json.dumps(message)
if UserType == 'USER':
transferID = clients[UserID]['HARDWARE']
else:
transferID = clients[UserID]['USER']
emit('serverTransfer', jsonMessage, to=transferID)
socketio.run(app,host='192.168.1.2',port='5000') #remove this line when using gunicorn deployment
我的Node.js客户端的服务器端一切正常,所以我认为问题不是来自服务器。
以下是从我的服务器返回的信息,显示Android客户端每隔几秒钟就会断开连接并重新连接:
WebSocket transport not available. Install eventlet or gevent and gevent-websocket for improved performance.
* Serving Flask app "websocketTest" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://192.168.1.2:5000/ (Press CTRL+C to quit)
192.168.1.5 - - [18/Apr/2021 18:15:23] "GET /socket.io/?EIO=4&transport=polling HTTP/1.1" 200 -
192.168.1.5 - - [18/Apr/2021 18:15:23] "POST /socket.io/?EIO=4&transport=polling&sid=mO35rQ0Mi_5ZxYFXAAAA HTTP/1.1" 200 -
192.168.1.5 - - [18/Apr/2021 18:15:23] "GET /socket.io/?EIO=4&transport=polling&sid=mO35rQ0Mi_5ZxYFXAAAA HTTP/1.1" 200 -
192.168.1.5 - - [18/Apr/2021 18:15:23] "POST /socket.io/?EIO=4&transport=polling&sid=mO35rQ0Mi_5ZxYFXAAAA HTTP/1.1" 200 -
connected
received message: { "USER_KEY": "TEST_USER", "MESSAGE": "asdfasdfasdf", "USER_TYPE":"USER"}<class 'str'>
6pKGg55-qcP9rejgAAAB
192.168.1.5 - - [18/Apr/2021 18:15:33] "POST /socket.io/?EIO=4&transport=polling&sid=mO35rQ0Mi_5ZxYFXAAAA HTTP/1.1" 200 -
192.168.1.5 - - [18/Apr/2021 18:15:33] "GET /socket.io/?EIO=4&transport=polling&sid=mO35rQ0Mi_5ZxYFXAAAA HTTP/1.1" 200 -
disconnected6pKGg55-qcP9rejgAAAB
192.168.1.6 - - [18/Apr/2021 18:15:33] "GET /socket.io/?EIO=4&transport=polling&t=NZdidCS&b64=1 HTTP/1.1" 200 -
192.168.1.6 - - [18/Apr/2021 18:15:34] "POST /socket.io/?EIO=4&transport=polling&t=NZdidEm&b64=1&sid=6pJ6UIkNrvOanjMwAAAC HTTP/1.1" 200 -
192.168.1.6 - - [18/Apr/2021 18:15:34] "GET /socket.io/?EIO=4&transport=polling&t=NZdidEx&b64=1&sid=6pJ6UIkNrvOanjMwAAAC HTTP/1.1" 200 -
192.168.1.6 - - [18/Apr/2021 18:15:34] "POST /socket.io/?EIO=4&transport=polling&t=NZdidFW&b64=1&sid=6pJ6UIkNrvOanjMwAAAC HTTP/1.1" 200 -
192.168.1.6 - - [18/Apr/2021 18:15:34] "POST /socket.io/?EIO=4&transport=polling&t=NZdidFx&b64=1&sid=6pJ6UIkNrvOanjMwAAAC HTTP/1.1" 200 -
connected
received message: {'USER_KEY': 'TEST_USER', 'USER_TYPE': 'HARDWARE'}<class 'dict'>
1nrEBb6Xny3dpELEAAAD
192.168.1.5 - - [18/Apr/2021 18:15:35] "GET /socket.io/?EIO=4&transport=polling HTTP/1.1" 200 -
192.168.1.5 - - [18/Apr/2021 18:15:35] "POST /socket.io/?EIO=4&transport=polling&sid=Vo6Qo1Suk5U6dnG0AAAE HTTP/1.1" 200 -
192.168.1.5 - - [18/Apr/2021 18:15:35] "GET /socket.io/?EIO=4&transport=polling&sid=Vo6Qo1Suk5U6dnG0AAAE HTTP/1.1" 200 -
192.168.1.5 - - [18/Apr/2021 18:15:35] "POST /socket.io/?EIO=4&transport=polling&sid=Vo6Qo1Suk5U6dnG0AAAE HTTP/1.1" 200 -
connected
received message: { "USER_KEY": "TEST_USER", "MESSAGE": "asdfasdfasdf", "USER_TYPE":"USER"}<class 'str'>
85Gy1EZr-XWm32-6AAAF
192.168.1.5 - - [18/Apr/2021 18:15:39] "POST /socket.io/?EIO=4&transport=polling&sid=Vo6Qo1Suk5U6dnG0AAAE HTTP/1.1" 200 -
192.168.1.6 - - [18/Apr/2021 18:15:39] "GET /socket.io/?EIO=4&transport=polling&t=NZdidFj&b64=1&sid=6pJ6UIkNrvOanjMwAAAC HTTP/1.1" 200 -
{'USER_KEY': 'TEST_USER', 'MESSAGE': 'GREEN', 'USER_TYPE': 'USER'}
192.168.1.6 - - [18/Apr/2021 18:15:40] "GET /socket.io/?EIO=4&transport=polling&t=NZdieaT&b64=1&sid=6pJ6UIkNrvOanjMwAAAC HTTP/1.1" 200 -
192.168.1.5 - - [18/Apr/2021 18:15:40] "POST /socket.io/?EIO=4&transport=polling&sid=Vo6Qo1Suk5U6dnG0AAAE HTTP/1.1" 200 -
{'USER_KEY': 'TEST_USER', 'MESSAGE': 'GREEN', 'USER_TYPE': 'USER'}
192.168.1.5 - - [18/Apr/2021 18:15:41] "POST /socket.io/?EIO=4&transport=polling&sid=Vo6Qo1Suk5U6dnG0AAAE HTTP/1.1" 200 -
192.168.1.6 - - [18/Apr/2021 18:15:41] "GET /socket.io/?EIO=4&transport=polling&t=NZdiejT&b64=1&sid=6pJ6UIkNrvOanjMwAAAC HTTP/1.1" 200 -
{'USER_KEY': 'TEST_USER', 'MESSAGE': 'RED', 'USER_TYPE': 'USER'}
192.168.1.5 - - [18/Apr/2021 18:15:42] "POST /socket.io/?EIO=4&transport=polling&sid=Vo6Qo1Suk5U6dnG0AAAE HTTP/1.1" 200 -
192.168.1.6 - - [18/Apr/2021 18:15:42] "GET /socket.io/?EIO=4&transport=polling&t=NZdif2X&b64=1&sid=6pJ6UIkNrvOanjMwAAAC HTTP/1.1" 200 -
{'USER_KEY': 'TEST_USER', 'MESSAGE': 'RED', 'USER_TYPE': 'USER'}
192.168.1.5 - - [18/Apr/2021 18:15:45] "POST /socket.io/?EIO=4&transport=polling&sid=Vo6Qo1Suk5U6dnG0AAAE HTTP/1.1" 200 -
192.168.1.5 - - [18/Apr/2021 18:15:45] "GET /socket.io/?EIO=4&transport=polling&sid=Vo6Qo1Suk5U6dnG0AAAE HTTP/1.1" 200 -
disconnected85Gy1EZr-XWm32-6AAAF
192.168.1.5 - - [18/Apr/2021 18:15:47] "GET /socket.io/?EIO=4&transport=polling HTTP/1.1" 200 -
192.168.1.5 - - [18/Apr/2021 18:15:47] "POST /socket.io/?EIO=4&transport=polling&sid=Axb2tK8BaYqGqrgGAAAG HTTP/1.1" 200 -
192.168.1.5 - - [18/Apr/2021 18:15:47] "GET /socket.io/?EIO=4&transport=polling&sid=Axb2tK8BaYqGqrgGAAAG HTTP/1.1" 200 -
192.168.1.5 - - [18/Apr/2021 18:15:47] "POST /socket.io/?EIO=4&transport=polling&sid=Axb2tK8BaYqGqrgGAAAG HTTP/1.1" 200 -
connected
received message: { "USER_KEY": "TEST_USER", "MESSAGE": "asdfasdfasdf", "USER_TYPE":"USER"}<class 'str'>
RLGgcjYRr3G8437zAAAH
192.168.1.6 - - [18/Apr/2021 18:15:58] "GET /socket.io/?EIO=4&transport=polling&t=NZdifE4&b64=1&sid=6pJ6UIkNrvOanjMwAAAC HTTP/1.1" 200 -
disconnected1nrEBb6Xny3dpELEAAAD
192.168.1.5 - - [18/Apr/2021 18:16:12] "GET /socket.io/?EIO=4&transport=polling&sid=Axb2tK8BaYqGqrgGAAAG HTTP/1.1" 200 -
disconnectedRLGgcjYRr3G8437zAAAH
我在Java中使用处理socket.io的库,导入方式如下:
implementation ('io.socket:socket.io-client:2.0.0') {
exclude group: 'org.json', module: 'json'
}
任何关于我 Android 客户端断开连接的原因的见解将不胜感激。
更新: 当我在 Nginx、gunicorn、flask 栈上以端口 80 运行我的服务器的部署状态时,这个问题就消失了。只有在直接在端口 5000 上运行 flask 时问题才会持续存在。