簡體   English   中英

flask-socketio不允許從android設備連接

[英]flask-socketio doesn't allow connection from android device

好的,所以我將按照這些教程來使用flask-socketIO和適用於android設備的客戶端應用程序來構建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

任何幫助將不勝感激,謝謝。

可悲的是,我也遇到同樣的問題,不知道如何解決。 在這里等待答案時,我嘗試了另一個庫https://github.com/koush/AndroidAsync ,幸運的是它可以工作。 也許您可以嘗試一下。

我有一個類似的問題,它與與Android通信時flask-socketio使用的編碼有關。 嘗試:

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

有關更多詳細信息,請參見此線程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM