Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better API for WebSocketApp class #349

Closed
mckelvin opened this issue Sep 1, 2017 · 3 comments
Closed

Better API for WebSocketApp class #349

mckelvin opened this issue Sep 1, 2017 · 3 comments

Comments

@mckelvin
Copy link
Contributor

mckelvin commented Sep 1, 2017

I found it's pretty wired to write a ws app like: https://github.com/websocket-client/websocket-client/blob/master/examples/echoapp_client.py . Personally I'd prefer to use an API like this(AS-IS code):

import websocket
from threading import Thread
import time
import sys


class DemoWebSocketApp(websocket.WebSocketApp):

    def on_message(self, message):
        print(message)

    def on_error(self, error):
        print(error)

    def on_close(self):
        print("### closed ###")

    def on_open(self):
        def run(*args):
            for i in range(3):
                # send the message, then wait
                # so thread doesn't exit and socket
                # isn't closed
                self.send("Hello %d" % i)
                time.sleep(1)

            time.sleep(1)
            self.close()
            print("Thread terminating...")
        Thread(target=run).start()


if __name__ == "__main__":
    websocket.enableTrace(True)
    if len(sys.argv) < 2:
        host = "ws://echo.websocket.org/"
    else:
        host = sys.argv[1]
    ws = DemoWebSocketApp(host)
    ws.run_forever()

Unfortunately, it's not possible to extend the websocket.WebSocketApp class like what I've written above. Please consider re-design the API, which should lead to an incompatible version bump.

@ebrensi
Copy link

ebrensi commented Nov 10, 2017

Why is it not possible? I have also tried this. It seems like the intuitive way to do it.

@bashlakov
Copy link

Confirm that this example didn't working.
Also tried with this approach with no success.
Python 3.5.2
App hung with no output to console

@engn33r
Copy link
Collaborator

engn33r commented Feb 15, 2021

I'm going to close this old issue for now because the attempted solution of PR #442 caused various issues and ended up being reverted. If someone would like to provide a PR that enables such functionality, I would be happy to review. Otherwise, while the current library design might not be for everyone, it has been working well. I would like to avoid large codebase modifications for the time being unless this can be done with fewer codebase edits than I am imagining. If there are aspects of this topic that I may have overlooked, you can reopen the issue.

@engn33r engn33r closed this as completed Feb 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants