flask websockets获取响应我的查询字符串

问题描述

我是“ from flask_sockets import Sockets”的新手

http:// localhost:5000 / invokeWebSocket?param = [{“ maths”:89},{“ English”:67}]

下面是我的app.py

如果我访问此URL http:// localhost:5000 / invokeWebSocket?param = [{“ maths :: 89},{” English“:67}] 我希望websocket响应作为浏览器中的查询字符串[{“ maths:89},{” English“:67}] 但是我不明白。请让我知道我在代码中错过了什么

from flask_sockets import Sockets
app = Flask(__name__)
app.debug = True
template1 = """
<html><body>
<h1>Flask WebSocket</h1>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
  var socket = new WebSocket("ws://" + document.domain + ":5000/api");
  socket.onmessage = function(message) {
    var data = JSON.parse(message.data);
    $("body").append(data);
  };  
});
</script>
</body></html>


app.route("/invokeWebSocket",methods=['GET'])
def invokeWebSocket():    
    params = request.args    
    response = {}     
    if 'param' in params:        
        inputParam = params.get('param')        
        response.setdefault('Get Response ','Value  is : ' + inputParam)        
        return render_template_string(template1,sample=jsonify(response))

@sockets.route('/api')
def api(socket):
    while True:
        message = socket.receive()
        socket.send(message)
        print("socket message "+message

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)