Skip to content

client : how to keep open connection and send messages #559

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

Closed
adriansev opened this issue Jan 21, 2019 · 13 comments
Closed

client : how to keep open connection and send messages #559

adriansev opened this issue Jan 21, 2019 · 13 comments
Labels

Comments

@adriansev
Copy link

Hi! I am trying to do some kind of interface shell to a websocket service so i need to keep the connection/application open to take the input and get the messages .. is there a way to do this?
Thank you!

@aaugustin
Copy link
Member

I don't understand what you're trying to achieve sufficiently well to understand your problem.

Perhaps you could start from the examples in the docs and explain why they don't do what you need?

@adriansev
Copy link
Author

well, i already have a working prototype for sending a single command, see : jalien_py
but, i would like to keep the connection open and send messages and receive their output .. some kind of terminal if you like ... (so i would like to ignore the websockets.exceptions.ConnectionClosed and detect somehow that the message ended)
to make matter more interesting i have a separate problem with the server that does not send ConnectionClosed .. but i have to ask these guys (that have also a java based interface to that service) how they detect the end of the json response

@aaugustin
Copy link
Member

Try using a loop?

while True:
    message = await websocket.recv()
    do_stuff(message)

@adriansev
Copy link
Author

adriansev commented Jan 21, 2019

that's perfect!!! thanks a lot!!
now, on my other issue (that after the received message the connection is not closed by server so i cannot get my message) : should i open another issue or could we continue the discussion here?
(to be brief : can i check if FIN bit of frame is enabled and decide (make recv() understand) that the message ended?)
Thank you!!

@aaugustin
Copy link
Member

The WebSocket protocol uses a persistent connection and delimits messages (it's a TLV protocol).

ConnectionClosed indicates that the connection dropped (and you need to reopen it if you want to interact with the server again), not that you reached the end of a message.

I'm not sure I understood your question very well. I hope this helps anyway.

@adriansev
Copy link
Author

Hi @aaugustin ! First let me thank you for taking you time to help me :)
So, from what i see in recv() the message will be taken and reported ONLY if:
self.state in [State.CLOSING, State.CLOSED]
the problem is that the server does not close the connection after delivering the message so i am getting stuck in await websocket.recv()
so, from my shallow understanding of websockets, can somehow the condition to deliver the message to be the FIN bit of a frame? Did i get anything right? :)
Well, putting aside my impressions and suppositions, i know this :

  1. my message reach the server
  2. the server message back (and my prototype code can get the message as tested on an public websocket echo service)
  3. awaiting the message from server of interest (that that does not close connection after sending the message) get me nothing and i get no result from awaiting.

Unfortunately, i cannot inspect the traffic as it is openssl only and i could not setup wireshark to to use private ca and certs

@aaugustin
Copy link
Member

You're misinterpreting the code: you're confusing "receiving messages" and "closing the connection when no more messages will be received". You do not need the server to close the connection to receive messages.

@adriansev
Copy link
Author

@aaugustin I see.. then i am stuck because i don't know how to continue ... given that i can communicate just fine with an ws echo server can you help me with an idea avout how can i debug why i receive nothing from this particular server even if the server says that it received my message and sent the answer?
Thank you very much!!

@aaugustin
Copy link
Member

Try enabling logging? https://websockets.readthedocs.io/en/stable/cheatsheet.html#debugging

I'm familiar with some common issues — that's how I knew to tell you "write a while loop" — but I never heard something like what you describe.

@adriansev
Copy link
Author

i had the logging but it was only INFO not debug ...
the code is at jalien_py
and the output looks like this :

./j
Prepare to connect : wss://<remote_ip>:8097/websocket/json
Enter PEM pass phrase:
client - state = CONNECTING
client - event = connection_made(<asyncio.sslproto._SSLProtocolTransport object at 0x7fbf070020f0>)
client - state = OPEN
JalienShPy Cmd: pwd
client > Frame(fin=True, opcode=1, data=b'{"command": "pwd", "options": []}', rsv1=False, rsv2=False, rsv3=False)
client > Frame(fin=True, opcode=9, data=b'\xd4BD\xca', rsv1=False, rsv2=False, rsv3=False)
client < Frame(fin=True, opcode=10, data=b'\xd4BD\xca', rsv1=False, rsv2=False, rsv3=False)
client - received solicited pong: d44244ca
client > Frame(fin=True, opcode=9, data=b':\xe5\x10\xe3', rsv1=False, rsv2=False, rsv3=False)
client < Frame(fin=True, opcode=10, data=b':\xe5\x10\xe3', rsv1=False, rsv2=False, rsv3=False)
client - received solicited pong: 3ae510e3
^C
You pressed Ctrl+C!

Any idea about this? Can i assure the upstream service that they actually they did not send anything back?
Thank you!!

@aaugustin
Copy link
Member

Indeed, they didn't.

@aaugustin
Copy link
Member

I'm going to close this issue as we haven't found a bug in websockets. Please follow-up if you do!

@adriansev
Copy link
Author

thanks a lot for your help and your great work!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants