[Solved] System.Net.WebSockets.ClientWebSocket fails at connecting to secure websocket server (wss)

Edit: I want to add that this is not necessarily for a WebGL build, but a standalone build and also inside the editor itself.

Unity version 2017.3.0f3

Unity is set to use .Net 4.6

When trying to connect to my own server or even echo.websocket.org in unity, I get an error:
“The authentication or decryption has failed.”

Running the exact same code in a separate C# console application project works flawlessly.

I’ve tried about every single fix I was able to find on google to no avail.

  • Manually adding sub protocols like “Tls”, “Tls11”, “Tls12”
  • Manually importing certs (mozroots, in code using X509 classes)
  • Using non-deprecated “cert-sync”

I’ve probably spent a good 10+ hours the past 1-2 weeks on this problem on and off

I’ve tried using WebSocketSharp as well, but it also gets the same kind of error “code 1015”

112260-err.png

using System;
using System.Collections;
using System.Text;
using System.Threading;

using System.Net;
using System.Net.WebSockets;
using System.Security.Cryptography.X509Certificates;

using UnityEngine;
using UnityEngine.Networking;

public class Client : MonoBehaviour
{
	public const string APIDomainWS = "wss://fury.furious.no";
	public const string APIDomain = "fury.furious.no";
	public const string APIUrl = "https://" + APIDomain;

	public static string SessionToken;

	public ClientWebSocket clientWebSocket;
	
	async void Start()
	{
		DontDestroyOnLoad(gameObject);
		
		clientWebSocket = new ClientWebSocket();

		clientWebSocket.Options.AddSubProtocol("Tls");
		
		Debug.Log("[WS]:Attempting connection.");
		try
		{
			Uri uri = new Uri(APIDomainWS);
			await clientWebSocket.ConnectAsync(uri, CancellationToken.None);
			if (clientWebSocket.State == WebSocketState.Open)
			{
				Debug.Log("Input message ('exit' to exit): ");
				
				ArraySegment<byte> bytesToSend = new ArraySegment<byte>(
					Encoding.UTF8.GetBytes("hello fury from unity")
				);
				await clientWebSocket.SendAsync(
					bytesToSend, 
					WebSocketMessageType.Text, 
					true, 
					CancellationToken.None
				);
				ArraySegment<byte> bytesReceived = new ArraySegment<byte>(new byte[1024]);
				WebSocketReceiveResult result = await clientWebSocket.ReceiveAsync(
					bytesReceived, 
					CancellationToken.None
				);
				Debug.Log(Encoding.UTF8.GetString(bytesReceived.Array, 0, result.Count));
			}
			Debug.Log("[WS][connect]:" + "Connected");
		}
		catch (Exception e)
		{
			Debug.Log("[WS][exception]:" + e.Message);
			if (e.InnerException != null)
			{
				Debug.Log("[WS][inner exception]:" + e.InnerException.Message);
			}
		}
		
		Debug.Log("End");
	}
}

this is a problem with certificates, i was having errors with webgl build on firefox then i put the same address with https and it showed a certificate error popup after accepting it build was working ok.
try to access the same link with ws instead of wss if it works its probably certificate
i don’t how to solve it a colleague fixed it by getting new certificates i guess.

Turns out this was an issue with the beta version of unity I was using (2018.1~) and that this was fixed in the 2018.2 beta.