【发布时间】: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