Issue
I'm trying to get a socket server talking to a websocket server. But I can't get it to work.
First I start a connection with sockets to a server running on localhost port 40404.
Then I start a websocket server on localhost port 1236.
I have two async functions attached to an eventloop:
One listens for incomming messages from the socket server and print them to the terminal.
Another listens for incomming messages to the websocket server and send them to the socket server.
I have a Javascript client connected to the websocket server.
When i run my code I get a connection to the socket server and some response from that side. But no websocket server is started.
I don't get any errors.
What am I doing wrong?
This is the code:
import socket
import asyncio
import websockets
#Gets any responses from the external socket server
async def get_resp():
while True:
resp = s.recv(5000).decode()
print(resp)
await asyncio.sleep(1)
#Gets any responses from the external websocket server and send it to socket server
async def listen_for_websocket(websocket, path):
for message in websocket:
json_obj = json.loads(message)
print(json_obj["Message"])
#The message from the JS client is in json format
message = json_obj["Message"]
message = message + '\n'
message = message.encode()
#Foreward the message to the socket server
s.send(message)
#Send a response back to the websocket
await websocket.send('{"Message":"Thanks for your message"}')
loop = asyncio.get_event_loop()
try:
#Start up connection with external socket server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#Stuf needed by the socket server
s.connect(('localhost',40404))
s.send(b'version [["0.1.4"],{}]\n')
s.send(b'name [["Python"],{}]\n')
#Starts connection to websocket
start_server = websockets.serve(listen_for_websocket,'localhost',1236)
asyncio.ensure_future(get_resp())
loop.run_until_complete(start_server)
loop.run_forever()
Solution
Your connection to the socket server is not async. Blocking calls to recv
are halting the event loop and preventing the websockets part from functioning properly.
Instead of calling socket.socket
and working with sockets directly, you should call asyncio.open_connection
. For example (untested):
async def get_resp(reader):
while True:
resp = await reader.read(5000)
if resp == b'':
break
print(resp.decode())
async def listen_for_websocket(websocket, path, writer):
async for message in websocket:
json_obj = json.loads(message)
message = json_obj["Message"] + '\n'
writer.send(message.encode())
await websocket.send('{"Message":"Thanks for your message"}')
async def main():
reader, writer = await asyncio.open_connection('localhost', 40404)
writer.write(b'version [["0.1.4"],{}]\n')
writer.write(b'name [["Python"],{}]\n')
await websockets.serve(
lambda w, p: listen_for_websocket(w, p, writer),
'localhost', 1236
)
# waits as long as the server is up
await get_resp(reader)
asyncio.run(main())
Answered By - user4815162342
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.