extends Control
@onready var status_label = $StatusLabel
@onready var question_label = $QuestionLabel
@onready var buttons = [
$HBoxContainer/Btn0,
$HBoxContainer/Btn1,
$HBoxContainer/Btn2,
$HBoxContainer/Btn3
]
var ws := WebSocketClient.new()
var is_connected := false
func _ready():
status_label.text = "Connecting..."
# 1) Initiate WS connection
var err = ws.connect_to_url("ws://localhost:8080")
if err != OK:
status_label.text = "Error connecting: %s" % err
# 2) Poll the WS each frame
set_process(true)
func _process(delta):
if ws.get_connection_status() == WebSocketClient.CONNECTION_CONNECTED and not is_connected:
is_connected = true
status_label.text = "Connected. Sending READY…"
_send_text("ready")
if ws.get_connection_status() in [WebSocketClient.CONNECTION_CONNECTED, WebSocketClient.CONNECTION_CONNECTING]:
ws.poll() # let the client process incoming packets
_read_messages()
func _read_messages():
var peer = ws.get_peer(1) # single peer at index 1
while peer.get_available_packet_count() > 0:
var pkt = peer.get_packet().get_string_from_utf8()
_on_server_message(pkt)
func _on_server_message(raw):
var j = JSON.parse(raw)
if j.error != OK:
push_error("JSON parse failed: %s" % raw)
return
var msg = j.result
match msg.type:
"start":
status_label.text = "Game is starting…"
"question":
_display_question(msg.data)
func _display_question(data):
status_label.text = "New question!"
question_label.text = data.question
# Show & label each button
for i in buttons.size():
buttons[i].text = data.options[i]
buttons[i].visible = true
# Connect each button to send its index
buttons[i].disconnect_all("pressed")
buttons[i].connect("pressed", Callable(self, "_on_answer_pressed").bind(i))
func _on_answer_pressed(idx):
_send_text("answer:%d" % idx)
status_label.text = "Answer %d sent" % idx
# hide buttons
for b in buttons: b.visible = false
func _send_text(txt):
var peer = ws.get_peer(1)
if peer and peer.get_connection_status() == NetworkedMultiplayerPeer.CONNECTION_CONNECTED:
peer.put_packet(txt.to_utf8())
Steps in Godot
Create a new Godot 4.4 project.
Add a Control scene with the nodes shown above.
Save it as QuizClient.tscn, attach the script above.
Run the scene (F5). It will:
Connect to ws://localhost:8080.
Send "ready".
Display the one question from questions.json.
On button press, send back "answer:N".
3. How It Works
Server (Python)
• Listens on port 8080 for WebSocket clients
• On "ready", sends a {type:"start"} and then the first question
• Logs any "answer:N" messages
Client (Godot)
• Uses WebSocketClient to connect
• Polls in _process() for incoming data
• Parses JSON payloads: shows questions & buttons
• Sends back simple text packets for answers
From here you can expand:
Load multiple questions & iterate with timers
Track scores per ws client and broadcast a leaderboard
Add authentication (e.g. PIN) before accepting “ready”
Switch to Socket.IO by using the socket_io_client plugin on Godot’s side if you need rooms/auto-reconnect
This minimal example gives you the core plumbing—feel free to build on top for your full local-multiplayer quiz!