R

Railway

βœ‹ο½œhelp

Join Server

how to use and configure python websockets module and stuff

Ccsdaniel5/14/2023
.
Ccsdaniel5/14/2023
N/A
Bbrody5/14/2023
well well well, look what sock the cat dragged in
Ccsdaniel5/14/2023
lmao
Ccsdaniel5/14/2023
so you know websockets module right?
IImLunaHey5/14/2023
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.
IImLunaHey5/14/2023
If you still need help after that feel free to ask questions.
Bbrody5/14/2023
i do not
Bbrody5/14/2023
i am not a python dev
Ccsdaniel5/14/2023
ok
IImLunaHey5/14/2023
same here. I just help maintain a few python projects.
Bbrody5/14/2023
ive done websockets in other languaes, so i might not be as usefull as luna, but i still know some stuff
IImLunaHey5/14/2023
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.
Bbrody5/14/2023
Bbrody5/14/2023
free clock
Ccsdaniel5/14/2023
wss?
Bbrody5/14/2023
websockets over https = wss
Ccsdaniel5/14/2023
oh
Ccsdaniel5/14/2023
okay
Bbrody5/14/2023
or is it websockets with tls, idk you get the point
IImLunaHey5/14/2023
Same diff.
Bbrody5/14/2023
real
Ccsdaniel5/14/2023
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
Bbrody5/14/2023
have a minimum tick time when nothing updates, but also send constantly when positions update? best of both worlds
Bbrody5/14/2023
that way you dont use server resources streaming the same data to the clients as fast as your code can
Ccsdaniel5/14/2023
websockets.exceptions.ConnectionClosedOK: received 1000 (OK); then sent 1000 (OK)
IImLunaHey5/14/2023
πŸ‘€
IImLunaHey5/14/2023
What’s the question?
Bbrody5/14/2023
that's not a question
Ccsdaniel5/14/2023
after sending the message to the websocket server i get this error
IImLunaHey5/14/2023
Gonna need more than that. Code sample?
IImLunaHey5/14/2023
Sounds like the connection was opened and then closed?
Ccsdaniel5/14/2023
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())
Ccsdaniel5/14/2023
one sec
Ccsdaniel5/14/2023
code block
IImLunaHey5/14/2023
Just a tip at the start use three backticks and then py to make it highlight as python.
Ccsdaniel5/14/2023
async def _wake_up():
    while True:
        await asyncio.sleep(1)
Bbrody5/14/2023
you instantly close the connection
Ccsdaniel5/14/2023
hm
Ccsdaniel5/14/2023
so its not blocking?
Ccsdaniel5/14/2023
i thought client.recv is blocking
Ccsdaniel5/14/2023
i just remove it then
Ccsdaniel5/14/2023
i mean
Ccsdaniel5/14/2023
the if statement
Bbrody5/14/2023
no, you are closing the connection once data is received
Ccsdaniel5/14/2023
oh
Bbrody5/14/2023
else: client.close
Ccsdaniel5/14/2023
i forgot to use while loop
Ccsdaniel5/14/2023
nvm
Ccsdaniel5/14/2023
yes
Ccsdaniel5/14/2023
i thought its gonna block
Ccsdaniel5/14/2023
now fixed
Ccsdaniel5/14/2023
thanks
Ccsdaniel5/14/2023
going very good
Bbrody5/14/2023
glad to hear it
Ccsdaniel5/14/2023
server done, i think i can host it on railway rn
Ccsdaniel5/14/2023
now i just need to make client work for it
Bbrody5/14/2023
see, a websocket implantation was super easy
Ccsdaniel5/14/2023
hopefully it will pay out too with the performance because if it doesnt i am lost
Bbrody5/14/2023
if theres performance issues, it would 100% be on you, not the protocol
Ccsdaniel5/14/2023
ah
Ccsdaniel5/14/2023
forgot to delete the port env variable
Ccsdaniel5/14/2023
restart works?
Ccsdaniel5/14/2023
werent their issues with it or smth?
Ccsdaniel5/14/2023
nvm
Bbrody5/14/2023
what
Ccsdaniel5/14/2023
it auto redeployed it
Ccsdaniel5/14/2023
restart on a service deployment but it auto redeployed it so doesnt matter
Ccsdaniel5/14/2023
lets see if it works...
Ccsdaniel5/14/2023
it worked :poggers:
Ccsdaniel5/14/2023
ah its using memory again but still not a cpu lol
Ccsdaniel5/14/2023
i got charged $0.0001 out of my free credit
Ccsdaniel5/14/2023
:kekw:
Bbrody5/14/2023
railway remove free credit when
Ccsdaniel5/14/2023
no no pls no lol
Bbrody5/14/2023
it would be such a shame if the good actors had to pay their 1-4$ usage
Ccsdaniel5/14/2023
now i just unlock developer plan and it will be very good
Bbrody5/14/2023
finally, i just went instant for dev without a second thought when i first signed up
Ccsdaniel5/14/2023
checking on usage
Ccsdaniel5/14/2023
one sec
Ccsdaniel5/14/2023
Ccsdaniel5/14/2023
ah why its using even a gb of memory?
Bbrody5/14/2023
don't look at that, that data is unreliable, use the metrics graphs in the service
Ccsdaniel5/14/2023
or is memory calculated from fractions too?
Ccsdaniel5/14/2023
ok
Bbrody5/14/2023
it's a display bug that they haven't fixed in months
Ccsdaniel5/14/2023
brody
Ccsdaniel5/14/2023
Ccsdaniel5/14/2023
then how?
Bbrody5/14/2023
there you go
Bbrody5/14/2023
the metrics graphs in the service itself is accurate, the graphs in the account usage are not accurate, use graphs in the service
Bbrody5/14/2023
it's purely a display bug, you won't be charged for 1gb
Ccsdaniel5/14/2023
then how do i know billing?
Ccsdaniel5/14/2023
ah okay
Bbrody5/14/2023
the price estimates should still be relatively accurate, but of course they are only estimates
Ccsdaniel5/14/2023
bruh
Ccsdaniel5/14/2023
Ccsdaniel5/14/2023
lolololol
Bbrody5/14/2023
it will climb