Skip to content

Instantly share code, notes, and snippets.

@ishikota
Last active June 24, 2019 11:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ishikota/6baaf3be69b665eb2e53cce799d7be9d to your computer and use it in GitHub Desktop.
Save ishikota/6baaf3be69b665eb2e53cce799d7be9d to your computer and use it in GitHub Desktop.
actioncableにpythonのwebsocket-clientから接続しようとした途中経過
>>> from websocket import create_connectio
>>> ws = create_connection("ws://localhost:3000/cable")
>>> ws.send(r'{"identifier":"{\"channel\": \"RoomChannel\"}", "command": "subscribe"}')
77
>>> ws.recv()
'{"identifier":"_ping","type":"confirm_subscription"}'
>>> ws.send(r'{"identifier" : "RoomChannel", "command": "message", "data": "{\"message\" : \"hoge\", \"action\" : \"speak\" }"}')
119
@ishikota
Copy link
Author

websocket path
/usr/local/lib/python2.7/site-packages/

@ishikota
Copy link
Author

クライアント実装方針

import websocket

def on_message(ws, message):
    # ここでmessageの内容によって,適切なメソッドよんで,結果をws.sendする

def on_error(ws, error):
    print error

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

def on_open(ws):
  print "### opened ###"


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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment