Like it says, I’m implementing realtime playground to unity, but I’m hitting an error that the websocket is not connected.
I need some genius help as I am not able to figure this out. Here is the script I’m using to connect. I think maybe the endpoint is wrong, but I can’t find any info to confirm.
I am getting the responses to be sent properly and the playground works in the browser, it’s just not connecting with my scripts. I’m using websocketsharp net standard.
ANY ideas to try would be appreciated!
using UnityEngine;
using WebSocketSharp;
using System.Text;
using System;
using System.Collections.Generic;
using System.Collections;
public class GPTRealtimeHandler : MonoBehaviour
{
private WebSocket ws;
private string apiKey = "MY_API_KEY"; // Replace with your actual API key
private string endpoint = "wss://api.openai.com/v1/realtime";
void Start()
{
Connect();
}
public void Connect()
{
try
{
ws = new WebSocket(endpoint);
// Add an Authorization header manually by creating a token
ws.SetCredentials(apiKey, "", true); // For BasicAuth, 'preAuth' is true
ws.OnOpen += (sender, e) =>
{
Debug.Log("WebSocket connected.");
SendInitialMessage();
};
ws.OnMessage += (sender, e) =>
{
Debug.Log($"Message received: {e.Data}");
HandleGPTResponse(e.Data);
};
ws.OnError += (sender, e) =>
{
Debug.LogError($"WebSocket error: {e.Message}");
};
ws.OnClose += (sender, e) =>
{
Debug.Log("WebSocket closed.");
};
ws.Connect();
Debug.Log("WebSocket connection initiated.");
}
catch (Exception ex)
{
Debug.LogError($"WebSocket connection error: {ex.Message}");
}
}
private IEnumerator RetryConnection()
{
while (ws.ReadyState != WebSocketState.Open)
{
Debug.LogWarning("Retrying WebSocket connection...");
Connect();
yield return new WaitForSeconds(5);
}
Debug.Log("WebSocket connected.");
}
private void SendInitialMessage()
{
var message = new
{
model = "gpt-4-turbo-realtime-preview", // Specify the model
//model = "gpt-4o-realtime-preview-2024-12-17", // Specify the model
prompt = "", // System-level prompt
temperature = 0.7,
max_tokens = 200
};
string json = JsonUtility.ToJson(message);
Debug.Log($"Sending initial message: {json}");
ws.Send(json);
}
public void SendPlayerInput(string playerInput)
{
if (ws != null && ws.ReadyState == WebSocketState.Open)
{
var message = new
{
model = "gpt-4-turbo-realtime-preview",
//model = "gpt-4o-realtime-preview-2024-12-17",
prompt = playerInput,
temperature = 0.7,
max_tokens = 200
};
string json = JsonUtility.ToJson(message);
Debug.Log($"Sending player input: {json}");
ws.Send(json);
}
else
{
Debug.LogError("WebSocket is not connected. Unable to send player input.");
}
}
private void HandleGPTResponse(string response)
{
Debug.Log($"GPT Response: {response}");
// Parse the JSON response (use Newtonsoft.Json or another library)
var responseData = JsonUtility.FromJson<Dictionary<string, object>>(response);
if (responseData.ContainsKey("turn_change"))
{
Debug.Log("Turn change detected.");
// Handle turn change logic here
}
if (responseData.ContainsKey("data"))
{
Debug.Log("Received GPT data.");
// Process the main GPT response
}
}
private void OnDestroy()
{
if (ws != null && ws.ReadyState == WebSocketState.Open)
{
ws.Close();
}
}
}