C
C#15mo ago
J.

❔ WebSocketSharp and Azure Endpoint (Error 1006)

Summary: 1) I create a UnityWebRequest which is used to call request.SendWebRequest(). 2) Use the returned request.downloadHandler.text to create a ws = new WebSocket(request.downloadHandler.text, "json.webpubsub.azure.v1"); 3) Attempt to perform ws.Connect() However, ws.Connect() never connects and instead always triggers OnClose() instead, citing error 1006. My actual code:
private IEnumerator ConnectToAzure()
{
string url = "https://myurl.azurewebsites.net/api/createprivatemessage?code=mycode";

InputData inputData = new InputData { sender = playFabId };
string data = JsonConvert.SerializeObject(inputData);
UnityWebRequest request = new UnityWebRequest(url, "POST");
byte[] bodyRaw = Encoding.UTF8.GetBytes(data);
request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");

yield return request.SendWebRequest();

if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
{
Debug.LogError(request.error);
}
else
{
ws = new WebSocket(request.downloadHandler.text, "json.webpubsub.azure.v1");
IsWebSocketUrlReachable(url);
// Set the WebSocket headers
// client.SetCredentials(azureConnection, "", false);
ws.OnOpen += OnWebSocketOpen;
ws.OnMessage += OnMessage;
ws.OnError += OnError;
ws.OnClose += OnClose;
ws.Connect();
if(ws.ReadyState!= WebSocketState.Open) {
Debug.Log("WebSocket was NOT opened. Something went wrong during initial connection in ConnectToAzure()");
}
}
request.Dispose();
}
private IEnumerator ConnectToAzure()
{
string url = "https://myurl.azurewebsites.net/api/createprivatemessage?code=mycode";

InputData inputData = new InputData { sender = playFabId };
string data = JsonConvert.SerializeObject(inputData);
UnityWebRequest request = new UnityWebRequest(url, "POST");
byte[] bodyRaw = Encoding.UTF8.GetBytes(data);
request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");

yield return request.SendWebRequest();

if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
{
Debug.LogError(request.error);
}
else
{
ws = new WebSocket(request.downloadHandler.text, "json.webpubsub.azure.v1");
IsWebSocketUrlReachable(url);
// Set the WebSocket headers
// client.SetCredentials(azureConnection, "", false);
ws.OnOpen += OnWebSocketOpen;
ws.OnMessage += OnMessage;
ws.OnError += OnError;
ws.OnClose += OnClose;
ws.Connect();
if(ws.ReadyState!= WebSocketState.Open) {
Debug.Log("WebSocket was NOT opened. Something went wrong during initial connection in ConnectToAzure()");
}
}
request.Dispose();
}
I am new to azure. How can I debug?
2 Replies
J.
J.OP15mo ago
IsWebSocketUrlReachable(url); is a ping function I implemented to test the reachability of the ws address. It always returns true, which further mystifies me.
public bool IsWebSocketUrlReachable(string url) {
try {
Uri uri = new Uri(url);
string host = uri.Host;

using (Ping ping = new Ping()) { //use SystemNetworkingInformation Ping (not Unit's object)
PingReply reply = ping.Send(host);

if (reply.Status == IPStatus.Success) {
Debug.Log("Can reach the WebSocket server!");
return true;
}
else {
Debug.Log("CANNOT reach the WebSocket server!");
return false;
}
}
}
catch (Exception) {
// An exception occurred (e.g., invalid URL)
return false;
}
}
public bool IsWebSocketUrlReachable(string url) {
try {
Uri uri = new Uri(url);
string host = uri.Host;

using (Ping ping = new Ping()) { //use SystemNetworkingInformation Ping (not Unit's object)
PingReply reply = ping.Send(host);

if (reply.Status == IPStatus.Success) {
Debug.Log("Can reach the WebSocket server!");
return true;
}
else {
Debug.Log("CANNOT reach the WebSocket server!");
return false;
}
}
}
catch (Exception) {
// An exception occurred (e.g., invalid URL)
return false;
}
}
Accord
Accord15mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server