developers

    Forum Stats

  • 3,873,986 Users
  • 2,266,658 Discussions
  • 7,911,684 Comments

Discussions

Oracle Cloud Free Tier

User_Z2ZPD
User_Z2ZPD Member Posts: 1 Green Ribbon

Hi

Can I use WebSocket on Oracle Cloud Free Tier?

Answers

  • User_5JN1L
    User_5JN1L Member Posts: 1 Green Ribbon

    from websocket_server import WebsocketServer


    # Called for every client connecting (after handshake)

    def new_client(client, server):

      print("New client connected and was given id %d" % client['id'])

      server.send_message_to_all("Hey all, a new client has joined us")


    # Called when a client sends a message

    def message_received(client, server, message):

      print("Client(%d) said: %s" % (client['id'], message[:50]))

      server.send_message_to_all(message)



    PORT = 9001

    server = WebsocketServer(port = PORT)

    server.set_fn_new_client(new_client)

    server.set_fn_message_received(message_received)

    server.run_forever()

    ----------------------------------------------------------------------------------------

    • Up code is not connected from outside.

    Use (websockets) down code instead of (websocket) up code

    ----------------------------------------------------------------------------------------

    import asyncio

    import websockets

    CONNECTED = []

    async def handler(websocket):

    CONNECTED.append(websocket)

      async for message in websocket:

    print(message[:20])

    websockets.broadcast(CONNECTED, message)

        

    async def main():

      async with websockets.serve(handler, "", 9001):

        await asyncio.Future() # run forever


    if __name__ == "__main__":

      asyncio.run(main())

developers