flask-socketio不允许从Android设备

时间:2015-08-06 06:22:22

标签: android python-2.7 sockets

好的,所以我按照这些教程使用flask-socketIOandroid devices的客户端应用程序构建了一个socketIO服务器。我的python设置非常简单:

Python服务器:

  

init.py:

from flask import Flask, render_template
from flask.ext.socketio import SocketIO, emit
import logging
from os import getcwd
from datetime import datetime 

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)

def openDir(folder):
    import os, errno

    try:
        os.makedirs(folder)
    except OSError, e:
        if e.errno != errno.EEXIST:
            raise

openDir('logs')

def fileAndChannelLogger(file_location,name):
    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)
    fh = logging.FileHandler("".join([getcwd(),'/',file_location,'/',datetime.today().strftime("%Y%m%d_%H_%M"),"_",name,".log"]))
    fh.setLevel(logging.DEBUG)
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    formatter = logging.Formatter('%(asctime)s - %(levelname)s | %(message)s')
    fh.setFormatter(formatter)
    ch.setFormatter(formatter)

    logger.addHandler(fh)
    logger.addHandler(ch)

    return logger

logger = fileAndChannelLogger('logs', 'test')

@app.route('/')
def index():
    logger.debug("Someone accessed the root page")
    return render_template('index.html')

@socketio.on('connect', namespace='/test')
def test_connect():
    logger.debug('Client connected')

@socketio.on('new message', namespace='/test')
def new_message(message):
    logger.debug(message)

@socketio.on('disconnect', namespace='/test')
def test_disconnect():
    logger.debug('Client disconnected')
  

run.py

from app import app, socketio
if __name__ == '__main__':
    socketio.run(app,host='My_cool_host_name')

Android客户端:

  

MainActivity.java

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.net.URISyntaxException;
import java.util.Objects;

import com.github.nkzawa.emitter.Emitter;
import com.github.nkzawa.socketio.client.IO;
import com.github.nkzawa.socketio.client.Socket;

import org.json.JSONException;
import org.json.JSONObject;

import static android.app.PendingIntent.getActivity;

public class MainActivity extends Activity implements View.OnClickListener {
    EditText userID;
    EditText statement;

    private Socket mSocket;
    {
        try {
            mSocket = IO.socket("http://My_cool_host_name:5000/test");
        } catch (URISyntaxException e) {}
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSocket.connect();
        Button send = (Button) findViewById(R.id.sendBtn);
        send.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        String ID_data = userID.getText().toString();
        String statement_data = statement.getText().toString();
        if (TextUtils.isEmpty(ID_data)) {
            return;
        }
        if (TextUtils.isEmpty(statement_data)) {
            return;
        }

        statement.setText("");
        JSONObject json = new JSONObject();
        try {
            json.put("user",ID_data);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            json.put("statement",statement_data);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        mSocket.emit("new message",json);
    }
}

基本上它需要两个编辑框,然后将信息传递给服务器,没什么特别的,只是足以得到这一切的基本基础。我启动服务器,当我使用指定端口转到地址时,我可以从浏览器中看到index.html,当我在模拟设备中运行客户端应用程序时,我在控制台中收到此错误:

 * Running on http://My_cool_host_name:5000/
2015-08-05 22:37:31,311 - DEBUG | Someone accessed the root page
192.168.1.163 - - [2015-08-05 22:37:31] "GET / HTTP/1.1" 200 210 0.022884
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/gevent/pywsgi.py", line 508, in handle_                                                                                                                                                             one_response
    self.run_application()
  File "/usr/lib/python2.7/site-packages/gevent/pywsgi.py", line 494, in run_app                                                                                                                                                             lication
    self.result = self.application(self.environ, self.start_response)
  File "/usr/lib/python2.7/site-packages/flask_socketio/__init__.py", line 27, i                                                                                                                                                             n __call__
    raise RuntimeError('You need to use a gevent-socketio server.')
RuntimeError: You need to use a gevent-socketio server.
{'GATEWAY_INTERFACE': 'CGI/1.1',
 'HTTP_ACCEPT_ENCODING': 'gzip',
 'HTTP_CONNECTION': 'Keep-Alive',
 'HTTP_HOST': 'My_cool_host_name:5000',
 'HTTP_USER_AGENT': 'Dalvik/2.1.0 (Linux; U; Android 5.1; Android SDK built for                                                                                                                                                              x86 Build/LKY45)',
 'PATH_INFO': '/socket.io/',
 'QUERY_STRING': 'EIO=3&transport=polling',
 'REMOTE_ADDR': '192.168.1.163',
 'REMOTE_PORT': '57754',
 'REQUEST_METHOD': 'GET',
 'SCRIPT_NAME': '',
 'SERVER_NAME': 'server_name',
 'SERVER_PORT': '5000',
 'SERVER_PROTOCOL': 'HTTP/1.1',
 'SERVER_SOFTWARE': 'gevent/1.0 Python/2.7',
 'wsgi.errors': <open file '<stderr>', mode 'w' at 0x7f344d15b1e0>,
 'wsgi.input': <gevent.pywsgi.Input object at 0x7f34461ddd10>,
 'wsgi.multiprocess': False,
 'wsgi.multithread': False,
 'wsgi.run_once': False,
 'wsgi.url_scheme': 'http',
 'wsgi.version': (1, 0)} failed with RuntimeError

192.168.1.163 - - [2015-08-05 22:39:08] "GET /socket.io/?EIO=3&transport=polling HTTP/1.1" 500 161 0.010306

正如你所看到的那样,

  

您需要使用gevent-socketio服务器。

但是我想我可以像这样运行它,我是否需要转身并将其拖放到gevent-socketio服务器后面,仅用于这个简单的测试?如果是这样,有人可以推荐方向吗?这些是我安装的python库:

alembic==0.6.2
Babel==1.3
Beaker==1.7.0.dev0
BeautifulSoup==3.2.1
blinker==1.3
decorator==3.4.0
Flask==0.10.1
Flask-Babel==0.9
Flask-Bootstrap==3.0.3.1
Flask-Login==0.2.9
Flask-Mail==0.9.0
Flask-Migrate==1.1.0
Flask-Mobility==0.1.1
Flask-Passlib==0.1
Flask-Script==0.6.6
Flask-Scrypt==0.1.3.1
Flask-SocketIO==0.6.0
Flask-SQLAlchemy==1.0
Flask-WhooshAlchemy==0.55a0
Flask-WTF==0.9.4
flup==1.0.2
gevent==1.0
gevent-socketio==0.3.6
gevent-websocket==0.9.2
greenlet==0.4.2
itsdangerous==0.24
Jinja2==2.8
lxml==3.4.4
Mako==0.9.1
MarkupSafe==0.23
MySQL-python==1.2.5
netsnmp-python==1.0a1
numpy==1.9.2
parse==1.6.3
passlib==1.6.2
pycrypto==2.6.1
python-dateutil==2.4.2
pytz==2013.9
PyYAML==3.11
scipy==0.15.1
scrypt==0.6.1
selenium==2.45.0
six==1.9.0
speaklater==1.3
SQLAlchemy==0.7.10
sqlalchemy-migrate==0.7.2
team==1.0
Tempita==0.5.1
Twisted==15.2.1
ujson==1.33
uWSGI==2.0.10
Werkzeug==0.10.4
Whoosh==2.5.6
WTForms==1.0.5
XlsxWriter==0.7.3
yamlog==0.9
zope.interface==4.1.2

非常感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

我有一个类似的问题,它最终是关于flask-socketio与Android通信时使用的编码。试试:

 mSocket = IO.socket("http://My_cool_host_name:5000/test?b64=1");

有关更多详细信息,请参见this thread