Last active
June 24, 2019 11:33
-
-
Save ishikota/6baaf3be69b665eb2e53cce799d7be9d to your computer and use it in GitHub Desktop.
actioncableにpythonのwebsocket-clientから接続しようとした途中経過
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> 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 |
websocket path
/usr/local/lib/python2.7/site-packages/
クライアント実装方針
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
参考URL
https://github.com/liris/websocket-client
http://d.hatena.ne.jp/seinzumtode/20141009/1412816599
http://qiita.com/wakaba260/items/b23721d5cfd73cd47ada#_reference-73e0503e080ecaea65c7