2016-05-05 16 views
1

私はPythonアプリケーションで作業しています。基本的な実装では、ラズベリーパイをメインサーバーに接続してメッセージを共有する必要があります。サーバーはPythonアプリケーションを実行してデータを分析し、情報をRaspberry Piに返します。PythonとSocket.IO - 接続後にアプリケーションがハングアップする

私はこのシステムをpython-socketioでサーバー部分に、socketio.clientでRaspberry Piコードを実装しています。

私の問題は、サーバーに接続して応答メッセージを受け取ることができますが、プログラムがクライアント側でハングすることです。

私のサーバーのコードは次のとおりです。私はこのコードを使用していたクライアントの一部については

import socketio 
import eventlet 
from flask import Flask 

# Define Socket.IO server and application wrapper 
sio = socketio.Server() 
app = Flask(__name__) 

@sio.on('connect', namespace = '/test') 
def connect(sid, environ): 
    print 'New Connection ' + sid 
    sio.emit("server_response", sid, room = sid, namespace = '/test') 

@sio.on('message', namespace = '/test') 
def message(sid, data): 
    print 'message ' + sid + " " + data 
    sio.emit("server_reply", data = "Hello", room = sid, namespace = '/test') 

@sio.on('disconnect', namespace = '/test') 
def disconnect(sid): 
    print 'disconnect ' + sid 

if __name__ == '__main__': 
    # Wrap Flask application with Socket.IO's middleware 
    app = socketio.Middleware(sio, app) 

    # Deploy as an eventlet WSGI server 
    eventlet.wsgi.server(eventlet.listen(('',8000)), app) 

from socketIO_client import SocketIO, BaseNamespace 
global sio 
global testNamespace 
global myId 

class testNamespace(BaseNamespace): 

    def on_server_response(self, userID): 
     myId = userID 
     print "Socket connection accepted" 
     print "I was assigned the id: " + myId 

    def on_server_reply(self, data): 
     print "message response: " + data 

if __name__ == "__main__": 
    # Establish the connection 
    sio = SocketIO('localhost', 8000) 
    testNamespace = sio.define(testNamespace, '/test') 

    print "Going to emit message" 
    testNamespace.emit("message", "Hello") 

    sio.wait() 

何らかの理由で、私は、割り当てられたIDとサーバの応答を取得しますが、その後プログラムは行に到達しません

print "Going to emit message" 

私は当初それが名前空間と関係していると思っていましたしかし、印刷命令は決して起こらないので、私が間違っていることを理解していません。

EDITは

私は、サーバーにメッセージを送信し、取得することができるよ

self.emit("message", "Hello") 

とメッセージこんにちはを放出しようとon_server_response場合、それは返事だと気づきました。 私が今理解していないことは、クラス定義の外でメインプログラム上でどのようにイベントを出すことができるかです。

答えて

1

私は同じ問題を発見し、SocketIO_clientで問題をオープンしました。

https://github.com/invisibleroads/socketIO-client/issues/117

ここでは、サブクラスでは問題の部分をオーバーライドするソリューションです。次に、SocketIOの代わりにこのSocketIOClientを使用してください。

from socketIO_client import SocketIO 

class SocketIOClient(SocketIO): 
    """ 
    Fix for library bug 
    """ 

    def _should_stop_waiting(self, for_connect=False, for_callbacks=False): 
     if for_connect: 
      for namespace in self._namespace_by_path.values(): 
       is_namespace_connected = getattr(
        namespace, '_connected', False) 
       #Added the check and namespace.path 
       #because for the root namespaces, which is an empty string 
       #the attribute _connected is never set 
       #so this was hanging when trying to connect to namespaces 
       # this skips the check for root namespace, which is implicitly connected 
       if not is_namespace_connected and namespace.path: 
        return False 
      return True 
     if for_callbacks and not self._has_ack_callback: 
      return True 
     return super(SocketIO, self)._should_stop_waiting() 
関連する問題