<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Olaf 007</title>
    <description>The latest articles on DEV Community by Olaf 007 (@olaf007).</description>
    <link>https://dev.to/olaf007</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1563641%2F36f630bd-0d99-4525-87c4-e0893764bef3.jpeg</url>
      <title>DEV Community: Olaf 007</title>
      <link>https://dev.to/olaf007</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/olaf007"/>
    <language>en</language>
    <item>
      <title>How to set up a Python WebSocket Server with Godot 4's WebSocketPeer Client. [Tutorial]</title>
      <dc:creator>Olaf 007</dc:creator>
      <pubDate>Tue, 04 Jun 2024 09:38:59 +0000</pubDate>
      <link>https://dev.to/olaf007/how-to-set-up-a-python-websocket-server-with-godot-4s-websocketpeer-client-tutorial-1a5f</link>
      <guid>https://dev.to/olaf007/how-to-set-up-a-python-websocket-server-with-godot-4s-websocketpeer-client-tutorial-1a5f</guid>
      <description>&lt;p&gt;Since Godot 4 WebSocketServer and WebSocketClient have been disappeared from the API.&lt;br&gt;
I tried to explain it to myself how Godot 4's new API works since the docs have not been updated using the downloadable example project you can find here: &lt;a href="https://godotengine.org/asset-library/asset/2800"&gt;https://godotengine.org/asset-library/asset/2800&lt;/a&gt;&lt;br&gt;
However, it seems that only one WebSocketPeer can connect to each other, consequentially, when the server connected to one peers, its over, other peers dont get the chance to connect, too!&lt;br&gt;
Now, I found a pretty solution using python's websockets module.&lt;br&gt;
1) Set up a client with Godot 4's WebSocketPeer&lt;/p&gt;

&lt;p&gt;Use the example project (link mentioned above) to set up the client; or use this script on Node:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;extends Node

# The URL we will connect to.
var websocket_url = "ws://localhost:12345" # Replace with actual server address and port
var socket := WebSocketPeer.new()

func _ready():
    if socket.connect_to_url(websocket_url) != OK:
        print("Could not connect.")
        set_process(false)

func _process(_delta):
    socket.poll()

    if socket.get_ready_state() == WebSocketPeer.STATE_OPEN:
        while socket.get_available_packet_count():
            print("Recv. &amp;gt;",socket.get_packet().get_string_from_ascii(),"&amp;lt;")


func _exit_tree():
    socket.close()

func _input(event):
    # Send "Ping!" to the server when Enter is pressed.
    if event is InputEventKey and event.pressed and event.keycode == KEY_ENTER:
        if socket.get_ready_state() != WebSocketPeer.STATE_OPEN:
            print("You are currently not connected to the server.")
        else:
            socket.send_text("Ping!")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2) Set up a python server using websockets module&lt;/p&gt;

&lt;p&gt;Follow the short tutorial on &lt;a href="https://pythonexamples.org/python-websockets-example/"&gt;https://pythonexamples.org/python-websockets-example/&lt;/a&gt; how to set up a websocket server. I have not done anything else but copy-pasting the example code and it worked. Use this for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import asyncio
import websockets

# Define a callback function to handle incoming WebSocket messages
async def handle_websocket(websocket, path):
    try:
        while True:
            message = await websocket.recv()
            print(f"Received message: &amp;gt;{message}&amp;lt;")

            # XXX: Do some stuff here with the message.

            response = "Pong!"
            await websocket.send(response)
    except websockets.ConnectionClosed:
        pass

if __name__ == "__main__":
    # Start the WebSocket server
    start_server = websockets.serve(handle_websocket, "localhost", 12345)

    asyncio.get_event_loop().run_until_complete(start_server)
    asyncio.get_event_loop().run_forever()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
