Giter VIP home page Giter VIP logo

Comments (8)

0x7466 avatar 0x7466 commented on June 12, 2024

Hi.

I've tested exactly the code you provided and it worked without problems on my machine.
Have you tested to connect to the server with another client to see if the problem is really caused by ActionCableZwei?

Best

from python-actioncable-zwei.

nicw avatar nicw commented on June 12, 2024

Hi Tobias, thanks again. Connecting works with websocket-client and the code below. Meaning, the server shows it received a connect attempt. I'm stumped. It's the same URL. And I've allowed all remote origins in my rails server.

Debug Output when I connect using example code (I added one to the runforever area)

python3 ./my_actioncable.py
04/03/2017 09:53:14 PM my_actioncable INFO: Started
04/03/2017 09:53:14 PM monitor DEBUG: ACTIONCABLE MONITOR : Started.
04/03/2017 09:53:14 PM connection DEBUG: ACTIONCABLE CONNECTION : RUN FOREVER...
04/03/2017 09:53:14 PM connection DEBUG: ACTIONCABLE CONNECTION : Connect...
$ (end)

Here is the example code from websocket-client

import websocket
import thread
import time
import json

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

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

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

def on_open(ws):
    msg = {'identifier': 'status_channel', 'command': 'message','message': 'FROM PYTHON'}
    

    
    def run(*args):
        for i in range(3):
            time.sleep(1)

            ws.send("{\"command\":\"subscribe\",\"identifier\":{\"channel\":\"MeshRelayChannel\"}}")
        time.sleep(1)
        ws.close()
        print("thread terminating...")
    thread.start_new_thread(run, ())


if __name__ == "__main__":
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp("ws://localhost:3000/cable/",
                              on_message = on_message,
                              on_error = on_error,
                              on_close = on_close)
    ws.on_open = on_open
    ws.run_forever()

from python-actioncable-zwei.

nicw avatar nicw commented on June 12, 2024

Update: It works if I change line 54 of connection.py to call ws.run_forever() , instead of the internal function you have defined (_run_forever)

The thread isn't executing the ws instance, so nothing gets kicked off. But I'm not sure why this works for you and not me?

self.ws_thread = threading.Thread(name="APIConnectionThread_{}".format(uuid.uuid1()), target=self.ws.run_forever())

from python-actioncable-zwei.

0x7466 avatar 0x7466 commented on June 12, 2024

You actually call the run_forever method in your code. So the websocket connection will now block your main thread. You have to pass the reference to the new thread, so without parentheses.

BTW The private method I use as thread loop does actually also nothing other than calling the run_forever method of the ws. See here.

Are you trying to call this program directly from the command line like in the example above?

from python-actioncable-zwei.

nicw avatar nicw commented on June 12, 2024

I see that now, your private method does end up calling the run_forever method of the ws. And yes, it does block the main thread (undesired!) but my server does receive responses.

Yes, I am trying to call this program directly. I am copying and pasting your code directly into a file, and then running 'python3 my_actioncable.py' Should I be running it differently, and I'm not understanding how to use this example?

I am running: python 3.6.1, macOS 10.12.3

from python-actioncable-zwei.

0x7466 avatar 0x7466 commented on June 12, 2024

Then add this code at the end of your file:

while True:
  pass

The connection thread is actually a daemonic thread so it's going to be killed if the main thread finishes. If you call an infinite loop in the main thread this will actually never stop and your connection should be established.

from python-actioncable-zwei.

nicw avatar nicw commented on June 12, 2024

Ahh, I see, that makes total sense. Threading 101. Thanks so much for putting with a newbie python guy. I really appreciate that you wrote this.

from python-actioncable-zwei.

0x7466 avatar 0x7466 commented on June 12, 2024

Your welcome :) Have fun!

Best

from python-actioncable-zwei.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.