我无法通过wss WebSocket从浏览器(反应式js)连接到龙卷风WebSocket服务器。
如果我从python WebSocket客户端连接服务器端,则它可以与龙卷风服务器成功交互,但是无法使用浏览器中完全相同的URL /端点。
浏览器代码也可在非安全环境中使用。浏览器客户端处于反应状态,实际的套接字连接仅使用香草javascript WebSocket。 服务器(龙卷风配置):
import os
import tornado.escape
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.websocket
import config
import logging
import logging.config
from alive.webserver import SocketHandler
from proc_manager import ProcManager
from tornado.options import define, options
#define("debug", default=True, help="Debug settings")
define("port", default=48939, help="Port to run the server on")
def main():
settings = {}
proc_man = ProcManager()
urls = [(r"/ws/processor$", SocketHandler, dict(proc_man=proc_man, sub_callback='run_status'))]
tornado.options.parse_command_line()
# Create Tornado application
app = tornado.web.Application(
urls,
**settings)
# Start Server
logging.info("Starting App with Debug Mode")
if config.ssl_options:
logging.info(f'App listening on secure connection {options.port}')
app.listen(options.port, ssl_options=config.ssl_options)
logging.info(f'listening on insecure connection {48938}')
app.listen(48938)
tornado.ioloop.IOLoop.current().start()
配置文件(上面导入):
ssl_options = {
'certfile': '/etc/letsencrypt/live/mysite.com/fullchain.pem',
'keyfile': '/etc/letsencrypt/live/mysite.com/privkey.pem'
}
JS websocket客户端代码:
export class WebSock extends Component {
constructor(props) {
super(props);
this.state = {};
// console.log(window.location.host)
// console.log(window.location.hostname)
this.wsURL =
(document.location.protocol === "https:" ? "wss://" : "ws://") +
window.location.hostname +
":" +
(document.location.protocol === "https:"
? props.port[1]
: props.port[0]) +
"/" +
props.endpoint;
this.connected = false;
this.socket = null;
this.reconnect = props.reconnect !== undefined ? props.reconnect : true;
this.socket = null;
this.initSock()
}
newConn = (e) => {
if (this.socket) {
while (!this.socket.CLOSED) {
if (!this.socket.CLOSING) {
this.socket.close();
}
}
delete this.socket;
this.socket = null;
}
console.log(this.wsURL);
this.socket = new WebSocket(this.wsURL);
//override from child class
};
sub = (d) => {
// console.log(d)
};
sendMessage = (data) => {
this.socket.send(JSON.stringify(data));
// console.log(data);
};
initSock = (e) => {
this.newConn();
this.socket.onclose = (e) => {
this.connected = false;
// console.log('Disconnected')
if (this.reconnect) {
this.sleep(1000);
this.initSock();
}
};
this.socket.onerror = (error) => {
// console.log('on error',error)
// console.log(error)
};
this.socket.onopen = (e) => {
this.connected = true;
console.log("Connected");
this.sendMessage({
method: "sub",
args: {
channels: "reporting",
user: window.user_name,
},
});
};
this.socket.onmessage = (d) => {
const msg = JSON.parse(d.data);
// console.log(msg)
if (msg.success) {
this[msg.method](msg);
return false;
} else {
// console.log(msg)
}
};
};
}
Chrome错误:
WebSocket connection to 'wss://mysite.com:48939/ws/processor' failed:
WebSocket opening handshake timed out
Firefox错误:
Firefox can’t establish a connection to the server at wss://mysite.com:48939/ws/processor
当我在上方打印由javascript客户端生成的URL时,它产生:wss://mysite.com:48939/ws/processor
感谢您的帮助。