【问题标题】:Displaying client side values with socketIO使用 socketIO 显示客户端值
【发布时间】:2015-07-01 02:44:55
【问题描述】:

我正在使用 https://github.com/miguelgrinberg/Flask-SocketIO 开始使用烧瓶和 SocketIO。

我想将一个字符串发布到烧瓶服务器,然后通过 SocketIO,将其发送到客户端网页。

我正在使用邮递员发布令牌值。请看截图。

我的烧瓶服务器看起来像:

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
app.config['DEBUG'] = True

#turn the flask app into a socketio app
socketio = SocketIO(app)

@app.route('/')
def index():
    #only by sending this page first will the client be connected to the socketio instance
    return render_template('index.html')

@socketio.on('connect', namespace='/ul')
def ul_connect():

    # print('here is ul '+ul)
    print('Client connected')

@app.route('/posting',methods=['POST'])
def posting():
        token = request.form['token']
        test_message(dict(data=token))
        return '1'

@socketio.on('posting', namespace='/ul')
def test_message(message):
    socketio.emit('response', {'data': message['data']}, broadcast=False)


@socketio.on('disconnect', namespace='/ul')
def ul_disconnect():
    print('Client disconnected')

if __name__ == '__main__

我的客户网页包含:

$(document).ready(function(){
//connect to the socket server.
var namespace = 'http://' + document.domain + ':' + location.port + '/ul';
var socket = io.connect(namespace);
console.log('namespace ',namespace)


//var socket = io.connect('http://' + document.domain + ':' + location.port + namespace);
socket.on('connect', function() {
    socket.emit('my event', {data: 'I\'m connected!'});
});
socket.on('response', function(msg) {
    // do something with msg.data
    console.log('response', msg.data)
});

当我发布令牌时,我在控制台中什么也看不到。我做错了什么?

【问题讨论】:

    标签: javascript python flask socket.io


    【解决方案1】:

    如果我理解你的例子,你正在以一种无效的方式混合 Flask/HTTP 和 Socket.IO 数据。

    您的第一个问题是识别发送 POST 请求的用户,以便您可以找到他/她的 Socket.IO 连接。这说起来容易做起来难,HTTP 请求和 Socket.IO 连接之间没有连接,所以你必须在双方都添加某种身份验证,或者如果你喜欢更简单的东西,只需记录用户的REMOTE_ADDR (这并不总是可靠的)。

    所以第一步是跟踪通过 Socket.IO 连接的用户(为简单起见,我将使用远程地址):

    socketio_clients = {}
    
    @socketio.on('connect', namespace='/ul')
    def ul_connect():
        socketio_clients[request.remote_addr] = request.namespace
        print('Client connected')
    
    @socketio.on('disconnect', namespace='/ul')
    def ul_disconnect():
        del socketio_clients[request.remote_addr]
        print('Client disconnected')
    

    现在在您的POST 请求中,您可以找到用户并发出消息:

    @app.route('/posting',methods=['POST'])
    def posting():
        token = request.form['token']
        client_namespace = socketio_clients.get(request.remote_addr)
        if client_namespace:
            client_namespace.emit('response', {'data': token}))
            return '1'
        abort(400)  # client does not have a socket connection active
    

    【讨论】:

    • 感谢您查看此 Miguel,我正在跟进我在 stackoverflow.com/questions/31141887/… 的上一篇帖子,以及 Celeo 给出的答案
    • 我通过从 @socketio.on('posting', namespace='/ul') def test_message 前面删除 @socketio.on('posting', namerspace='ul' 解决了我的问题(消息):
    • 我认为您正在回答一个更复杂的问题,但是当我使用 socketIO 变得更高级时,我会记住它。最好的问候。
    • 好吧,我认为你仍然没有做对。 socketio.emit() 函数中的 socketio.emit() 调用将向所有连接的用户广播消息,该函数不知道用户。从多个客户端连接以检查您是否获得了预期的行为。
    • 明白,此时我只是想获得任何类型的连接,但遇到了麻烦。现在我有了一些基本的工作代码,我可以将更多的复杂性分层。再次感谢。
    猜你喜欢
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-27
    相关资源
    最近更新 更多