我在使用 socket.io(Flask 后端)时遇到了 Brython 的问题。前端代码在这里:
from browser import document, html, window, local_storage,console
import random, time
#Global variables
btn = document['btn']
out = document['out']
#Setting Local Storage
storage = local_storage.storage
#Setting Websocket
socket = window.io.connect('localhost:5000/room', {'transports': ["websocket"]})
# Diceroller
def diceroller(ev):
#Local variables
player = document['player'].value
action = document['action'].value
dice_pile = document['dice-pile'].value
dice = document['dice'].value
mod = document['mod'].value
date = time.asctime()
total = 0
salt = str(random.randint(10000, 99999)) #for the local storage
json = {'player':player, "action":action, "dice_pile":dice_pile, "dice":dice,"mod":mod,"date":date, "results":[],"total":total}
socket.emit("roll", json)
#Event Listener
btn.bind('click', diceroller)
def show_result(data):
#local storage log
storage['roll_log'+salt] = data['ouput']
#parsing output to HTML
output_html = html.P(data['output'])
out.insertAdjacentElement('afterbegin',output_html)
socket.on('roll_result', show_result)
#print the roll log
for i in storage.values():
out <= html.P(i)
#Firefox Issue
def close_socket(ev):
socket.close()
window.addEventListener('beforeunload', close_socket)
问题是 Firefox 和 Chrome 无法与 websocket 建立连接。
Firefox 说:与 ws://localhost:5000/socket.io/?EIO=4&transport=websocket 的连接在页面加载时被中断。
脚本是在页面顶部还是底部都没有关系。同样的错误。有人说您应该“在卸载之前”关闭连接。我处理这个。同样的错误。
Chrome 说:socketio.min.js:6 WebSocket 连接到 'ws://localhost:5000/socket.io/?EIO=4&transport=websocket' 失败:
顺便说一句,这是一个骰子滚轮应用程序。如果你好奇。
请问有人可以对这个问题有所了解吗?我完全没有子弹了...