This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote

Hi !

I'm trying to use the twitch PubSub API with websocket connection and I can't manage to receive the basic PONG response after sending a PING

Here's the log I got after running the code:

> Connecting...
> Socket Open with 
> SENT {"type":"PING"}
> Socket Closed, clean : False
--- Debugging process stopped ---

I don't know if I'm sending the {"type" : "PING"} the right way, any ideas ?

Here's my code with a fake client_id that correspond to my App's token registered inside the twitch dev dashboard.

extends Node


var client_ID := "helloworld123456789"
var socket := WebSocketClient.new()


# Called when the node enters the scene tree for the first time.
func _ready():
    socket.connect("connection_closed", self, "_on_connection_closed")
    socket.connect("connection_error", self, "_on_connection_error")
    socket.connect("connection_established", self, "_on_connected")
    socket.connect("data_received", self, "_on_data")
    socket.connect("server_close_request", self, "_on_ask_to_close")

    socket.verify_ssl = true
    socket.get_peer(1).set_write_mode(WebSocketPeer.WRITE_MODE_TEXT)

    var err = socket.connect_to_url("wss://pubsub-edge.twitch.tv")

    if err == OK:
        Logger.log("Connecting...")
        set_process(true)
    elif err != OK:
        Logger.log("Error Connecting")
        set_process(false)


func _process(delta):
    if(socket.get_connection_status() != NetworkedMultiplayerPeer.CONNECTION_DISCONNECTED):
        socket.poll()


# SIGNALS
func _on_connection_closed(was_clean := false):
    Logger.log("Socket Closed, clean : " + str(was_clean))
    set_process(false)

func _on_connection_error():
    Logger.log("Socket Error.")

func _on_connected(protocol := ""):
    Logger.log("Socket Open with " + protocol)
    send_data({"type" : "PING"})

func _on_ask_to_close(code: int, reason: String):
    Logger.log("Ask to close connection. " + str(code) + " " + reason)


# DATA EXCHANGE FUNC 
func send_data(command):
    var cmd_to_send = JSON.print(command)
    Logger.log("SENT " + cmd_to_send)
    var x = socket.get_peer(1).put_packet(cmd_to_send.to_utf8())

func _on_data():
    var payload = JSON.parse(socket.get_peer(1).get_packet().get_string_from_utf8()).result
    Logger.log("DATA RECEIVED " + str(payload))
Godot version v3.5.1.stable.official [6fed1ffa3]
in Engine by (34 points)
edited by

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.