我想實現一個FlaskSocketIO與前端和我的網絡套接字服務器之間socket.io.js應用實時通信SocketIO在JS
的前端代碼看起來像這樣:
$(document).ready(function() {
namespace = '/test';
var socket = io.connect('http://' + document.domain + ':' + location.port + namespace);
socket.on('connect', function() {
socket.emit('my event', {data: 'Client connected!'});
});
socket.on('my response', function(msg) {
$('#log').append('<br>' + $('<div/>').text('Received #' + msg.count + ': ' + msg.data).html());
}); });
我使用的瓶的代碼是:
@socketio.on('my event', namespace='/test')
def receive__message(message):
socketio.emit('my response', {'data': 'Backend saw "' + json.dumps(message['data']) + '" from the frontend'})
print("Received from message data:" + json.dumps(message['data']))
在FlaskSocketIO方面我可以成功地收到消息,但是當我發出「我respose」的消息,我可以看到日È消息從谷歌瀏覽器網絡選項卡內列出像下面但是不採取任何行動,根據我的JavaScript代碼以上關於「我的響應」事件:
ÿ42["my response",{"data":"Backend saw \"{\"state\": \"Client Connected \"}\" from the frontend"}]
話雖如此,前端和flasksocketsio之間的通信被成功地進行了但沒有任何反應上的代碼,這部分...
socket.on('my response', function(msg) {
$('#log').append('<br>' + $('<div/>').text('Received #' + msg.count + ': ' + msg.data).html());
});
我使用警報(味精),甚至執行console.log(味精)嘗試,但他們沒有告訴我任何事情,甚至我看到收到的郵件。任何線索什麼可能是錯誤的?可能Flask爲SocketIO JS插件生成錯誤格式的消息?
工作就像一個魅力!非常感謝! – Mastermind