我有一个http API服务器(返回json数据)和一个发出websockets请求的客户机,所以我决定通过websockify在我的服务器和客户机之间通信。
但是我不知道如何获得websocket请求的结果。
出于测试目的,我有一个简单的hello world python API服务器和Flask:
import flask
from flask import request, jsonify
app = flask.Flask(__name__)
app.config["DEBUG"] = True
result = {"HelloWorld" : 42}
@app.route('/', methods=['GET'])
def home():
return jsonify(result)
app.run(host="0.0.0.0")在浏览器中测试,返回well {"HelloWorld": 42}。
我运行websockify是为了打开一个与websockets通信的新端口:websockify --verbose 0.0.0.0:5001 0.0.0.0:5000。
我有一个javascript客户端,它试图发出这样的websocket请求:
var socket = null;
socket = new WebSocket("ws://0.0.0.0:5001/");
socket.onerror = function(error) {
console.error(error);
};
socket.onopen = function(event) {
console.log("Connexion ok.");
socket.send("GET / HTTP/1.1");
};
socket.onmessage = function(event) {
console.log("Message: ", event.data);
};
socket.onclose = function(event) {
console.log("Connexion ko.");
};但当我尝试执行此脚本时,连接在连接后立即关闭:
Connexion ok.
Connexion ko.当我尝试发送websocket (socket.send("GET / HTTP/1.1");)时,它似乎关闭了,因为如果我注释此行,则连接仍然存在。
发布于 2021-02-17 06:14:59
我认为你需要这样的东西:
websockify --详细5001本地主机:5000
https://stackoverflow.com/questions/62448954
复制相似问题