0

I have an http API server (returning json data) and a client who make websockets requests, so I decided to go over websockify to communicate between my server and my client.

But I don't figure how to get the result of a websocket request.

For testing purpose, I have a simple hello world python API server with 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")

Tested in a browser, returning well {"HelloWorld": 42}.

I run websockify in order to open a new port which communicate with websockets like this : websockify --verbose 0.0.0.0:5001 0.0.0.0:5000.

And I have a javascript client who try to make a websocket request like this :

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.");
};

But when I try to execute this script, the connection close immediately after the connection :

Connexion ok.
Connexion ko.

It seems that it close when I try to send the websocket (socket.send("GET / HTTP/1.1");) because if I comment this line then the connection persists.

Mathis
  • 53
  • 7

1 Answers1

-1

I think you need something like this:

websockify --verbose 5001 localhost:5000

st68
  • 1