0

I use python websocket-client to send message to client.

On client side I have:

var s = new WebSocket('http://' + location.host + ':8000/ws');
s.onopen = function(e) { 
    $("#connected3").html('open');
    console.log(e)
}
s.onclose = function(e) { 
    $("#connected").html('close') 
}
s.onmessage = function(e) {
    $("#connected2").html(e.data);
}

and on server side I have:

import websocket
ws = websocket.WebSocket()
ws = create_connection("ws://127.0.0.1:8000/ws", sslopt=  {"check_hostname": False})

I get this error: Handshake status 404

My guess is, there is a problem with web socket server: ws://127.0.0.1:8000/ws

Did I miss something in setting up my web socket?

Full code: https://github.com/Homa/weatherApp

2 Answers 2

1

You are creating a websocket client in your backend. You have to create a websocket server and connect to in your javascript code.

ip install git+https://github.com/dpallot/simple-websocket-server.git and connect to it.

from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket

class SimpleEcho(WebSocket):

    def handleMessage(self):
        # echo message back to client
        self.sendMessage(self.data)

    def handleConnected(self):
        print(self.address, 'connected')

    def handleClose(self):
        print(self.address, 'closed')

server = SimpleWebSocketServer('', 8000, SimpleEcho)
server.serveforever()
Sign up to request clarification or add additional context in comments.

Comments

0

If you use Flask-Sockets extension, you have a websocket implementation for gunicorn directly in the extension which make it possible to start with the following command line gunicorn -k flask_sockets.worker --bind 0.0.0.0:8000 app:app

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.