R
Join ServerRailway
✋|help
how to use and configure python websockets module and stuff
.
N/A
well well well, look what sock the cat dragged in
lmao
so you know websockets module right?
https://gist.github.com/stefanotorresi/bfb9716f73f172ac4439436456a96d6f
Might be a bit out of date but this should be a good read through to work out how it works.
Might be a bit out of date but this should be a good read through to work out how it works.
If you still need help after that feel free to ask questions.
i do not
i am not a python dev
ok
same here. I just help maintain a few python projects.
ive done websockets in other languaes, so i might not be as usefull as luna, but i still know some stuff
Yeah most of my knowledge is in node but websockets are the same at the end of the day no matter the language. Hopefully that gist helps with the syntax.
websockets 🙂
wss://utilities.up.railway.app/ws
wss://utilities.up.railway.app/ws
free clock
wss?
websockets over https = wss
oh
okay
or is it websockets with tls, idk you get the point
Same diff.
real
would an everytime sending realtime movement and stuff thing would be better or one that only sends if it changes, first one is good when the person has bad internet and data still gets processed
have a minimum tick time when nothing updates, but also send constantly when positions update? best of both worlds
that way you dont use server resources streaming the same data to the clients as fast as your code can
websockets.exceptions.ConnectionClosedOK: received 1000 (OK); then sent 1000 (OK)
👀
What’s the question?
that's not a question
after sending the message to the websocket server i get this error
Gonna need more than that. Code sample?
Sounds like the connection was opened and then closed?
class Server():
def __init__(self):
self.BUFSIZ = 4096
self.loop = asyncio.get_event_loop()
self.logger = _create_logger()
self.addresses_in_game = {}
self.addresses = {}
self.last_movement_packet = {}
self.email_for_address = {}
self.listen()
def listen(self, host='0.0.0.0', port=port):
self.logger.info("listening on {}:{}".format(host, port))
ws_server = websockets.serve(self.handle_connection, host, port)
self.loop.run_until_complete(ws_server)
self._wake_up_task = asyncio.ensure_future(_wake_up())
try:
self.loop.run_forever()
except KeyboardInterrupt:
self.logger.debug('caught keyboard interrupt')
self.logger.info("exiting")
self._wake_up_task.cancel()
try:
self.loop.run_until_complete(self._wake_up_task)
except asyncio.CancelledError:
self.loop.close()
async def handle_connection(self,client):
"""Sets up handling for incoming clients."""
print("%s:%s has connected." % client.remote_address)
self.addresses[client] = client.remote_address
await self.handle_client(client,client.remote_address)
# Takes client socket as argument.
async def handle_client(self, client, client_address):
"""Handles a single client connection."""
while True:
msg = await client.recv()
if msg != b'':
await self.run_function(client, msg)
else:
await client.close()
break
async def broadcast(self, msg, id_): # prefix is for name identification.
"""Broadcasts a message to all the clients."""
for client in self.player_clients_in_game[str(id_)]:
await client.send(msg.encode())
one sec
code block
Just a tip at the start use three backticks and then py to make it highlight as python.
async def _wake_up():
while True:
await asyncio.sleep(1)
you instantly close the connection
hm
so its not blocking?
i thought client.recv is blocking
i just remove it then
i mean
the if statement
no, you are closing the connection once data is received
oh
else: client.close
i forgot to use while loop
nvm
yes
i thought its gonna block
now fixed
thanks
going very good
glad to hear it
server done, i think i can host it on railway rn
now i just need to make client work for it
see, a websocket implantation was super easy
hopefully it will pay out too with the performance because if it doesnt i am lost
if theres performance issues, it would 100% be on you, not the protocol
ah
forgot to delete the port env variable
restart works?
werent their issues with it or smth?
nvm
what
it auto redeployed it
restart on a service deployment but it auto redeployed it so doesnt matter
lets see if it works...
it worked 

ah its using memory again but still not a cpu lol
i got charged $0.0001 out of my free credit

railway remove free credit when
no no pls no lol
it would be such a shame if the good actors had to pay their 1-4$ usage
now i just unlock developer plan and it will be very good
finally, i just went instant for dev without a second thought when i first signed up
checking on usage
one sec
ah why its using even a gb of memory?
don't look at that, that data is unreliable, use the metrics graphs in the service
or is memory calculated from fractions too?
ok
it's a display bug that they haven't fixed in months
brody
then how?
there you go
the metrics graphs in the service itself is accurate, the graphs in the account usage are not accurate, use graphs in the service
it's purely a display bug, you won't be charged for 1gb
then how do i know billing?
ah okay
the price estimates should still be relatively accurate, but of course they are only estimates
bruh
lolololol
it will climb