我目前正在运行一个Python SocketIO服务器,与我的JavaScript客户端连接完美。我正在使用socketio android example chat app编写Android代码,它在使用NodeJS服务器时运行良好,但当我改用Python服务器时,无法连接。
如何从Android连接到Ptyhon-SocketIO服务器?
Android代码:
以下错误显示:
SSLError:[SSL:SSL_HANDSHAKE_FAILURE] ssl握手失败(_ssl.c:1754)
如何从Android连接到Ptyhon-SocketIO服务器?
Android代码:
public class HomeActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private final String TAG = "MainActivity";
Button btnCore0, btnCore1, btnCPUUsage;
private ProgressBar progressBar;
private Socket mSocket;
{
try {
mSocket = IO.socket(Constants.SERVER_URL);
} catch (URISyntaxException e) {
Log.e(TAG, e.getMessage());
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
btnCore0 = (Button) findViewById(R.id.btnCore0);
btnCore1 = (Button) findViewById(R.id.btnCore1);
btnCPUUsage = (Button) findViewById(R.id.btnCPUUsage);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
// Make buttons invisible
btnCore0.setVisibility(View.INVISIBLE);
btnCore1.setVisibility(View.INVISIBLE);
btnCPUUsage.setVisibility(View.INVISIBLE);
// Make progress bar visible
progressBar.setVisibility(View.VISIBLE);
mSocket.on("status-update", onNewMessage);
mSocket.on(Socket.EVENT_DISCONNECT, onSocketDisconnected);
mSocket.connect();
}
private Emitter.Listener onNewMessage = new Emitter.Listener() {
@Override
public void call(final Object... args) {
HomeActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d(TAG, "New message 090909***");
JSONObject data = (JSONObject) args[0];
int core0 = 0;
int core1 = 0;
int cpu_usage_in = 0;
try {
core0 = data.getInt("core0_in");
core1 = data.getInt("core1_in");
cpu_usage_in = data.getInt("cpu_usage_in");
} catch (JSONException e) {
Log.e(TAG, e.getMessage());
}
btnCore0.setText(getResources().getString(R.string.core0, String.valueOf(core0)));
btnCore1.setText(getResources().getString(R.string.core1, String.valueOf(core1)));
btnCPUUsage.setText(getResources().getString(R.string.cpu_usge, String.valueOf(cpu_usage_in)));
updateButtonBackgroundColor(btnCore0, core0);
updateButtonBackgroundColor(btnCore1, core1);
updateButtonBackgroundColor(btnCPUUsage, cpu_usage_in);
onServerDataReceived();
}
});
}
};
接下来是每秒钟发出的Python服务器。我知道这个很好用,因为我可以从JavaScript应用程序连接到它。 Python 代码:
from flask import Flask, render_template
from flask_socketio import SocketIO
from gcm import GCM
eventlet.monkey_patch()
app = Flask(__name__)
socket = SocketIO(app, logger=True, engineio_logger=True)
class Server(threading.Thread):
def __init__(self, thread_id):
threading.Thread.__init__(self)
self.threadID = thread_id
def run(self):
print("Starting " + self.name)
serve()
print("Exiting " + self.name)
def serve():
if __name__ == '__main__':
eventlet.wsgi.server(eventlet.wrap_ssl(eventlet.listen(('', 8000)), certfile='/home/garthtee/cert.pem', keyfile='/home/garthtee/privkey.pem'), app)
server_thread = Server("Server-thread")
server_thread.start()
threads.append(server_thread)
print("Started @ " + str(get_time()))
while True:
sensors.init()
try:
for chip in sensors.iter_detected_chips():
# print('%s at %s' % (chip, chip.adapter_name))
for feature in chip:
if feature.label == 'Core 0':
core0 = feature.get_value()
elif feature.label == 'Core 1':
core1 = feature.get_value()
for x in range(1):
cpu_usage = str(psutil.cpu_percent(interval=1))
finally:
socket.emit('status-update', {'core0_in': core0, 'core1_in': core1, 'cpu_usage_in': cpu_usage, 'users': users})
alert_checker(avg_temp, users)
sensors.cleanup()
time.sleep(1)
以下错误显示:
SSLError:[SSL:SSL_HANDSHAKE_FAILURE] ssl握手失败(_ssl.c:1754)