[SOLVED]Need help to Open Websocket Connection with wss string

Hey all,

i am new to the Forum, but i need some Help.
For a game that we make, i need a connection to Binary.com (and other trading platforms) to get some Information about Resources and OTC prices.

In a normal C’ Project, the Websocket client is 100% functional.
But in Unity i get the Following error: The authentication or decryption has failed.

Can someone look at my Code and say where is the bug??

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;

using Newtonsoft.Json;
using WebSocket4Net;
using SuperSocket.ClientEngine;

public class BinaryDotComConnector
{
    /* Websocket for binary.com */
    WebSocket BinaryNetSocket;

    /* Websocket connection string */
    string websocketURI = "wss://ws.binaryws.com/websockets/v3?app_id=[the App ID]";

    /* Authorisation string */
    string authorization = "{\"authorize\":\"[the Autorisation Code]\"}";

    /* String for Account Info and Trading */
    //string getBalance = "{\"balance\":\"1\",\"subscribe\":\"1\"}";                           // Balance subscription to automated get changes
    //string getPortfolio = "{\"portfolio\":\"1\"}";
    //string getProfitTable = "{\"profit_table\":\"1\",\"description\":\"1\",\"limit\":\"20\"}";      //

    private bool is_connected = false;

    public BinaryDotComConnector()
    {
        BinaryNetSocket = new WebSocket(websocketURI);
     
        BinaryNetSocket.Closed += new EventHandler(websocket_Closed);
        BinaryNetSocket.Error += new EventHandler<ErrorEventArgs>(websocket_Error);
        BinaryNetSocket.MessageReceived += new EventHandler<MessageReceivedEventArgs>(websocket_MessageReceived);
        BinaryNetSocket.Opened += new EventHandler(websocket_Opened);
        BinaryNetSocket.Open();
    }

    private void websocket_Opened(object sender, EventArgs e)
    {
        is_connected = true;
        UnityEngine.Debug.Log("Send Authentication");
        sendMessage(authorization);
        UnityEngine.Debug.Log("!!!Connected!!!");
    }

    private void websocket_Error(object sender, ErrorEventArgs e)
    {
        UnityEngine.Debug.Log("Error: " + e.Exception.Message);
    }

    private void websocket_Closed(object sender, EventArgs e)
    {
        UnityEngine.Debug.Log("Connection Closed");
        is_connected = false;
        ///BinaryNetSocket.Open();
    }

    private void websocket_MessageReceived(object sender, MessageReceivedEventArgs e)
    {
        //string messageString = e.Message;
        string msgType = GetMSGType(e.Message, "\"msg_type\":\"", "\"");

        UnityEngine.Debug.Log(e.Message);
        UnityEngine.Debug.Log(msgType);

        switch (msgType)
        {
            case "balance":
                //sendMessage(getPortfolio);
                break;
            case "authorize":
                //sendMessage(getBalance);
                break;
            case "portfolio":
                //sendMessage(getProfitTable);
                break;
            case "profit_table":
                //UnityEngine.Debug.Log(e.Message);
                break;
            case "ping":
                break;
            default:
                break;
        }
    }

    public void sendMessage(string _jsonString)
    {
        UnityEngine.Debug.Log("Sending message");

        if (is_connected)
        {
            BinaryNetSocket.Send(_jsonString);
            Debug.WriteLine("Message Send: " + _jsonString);
        }
    }

    /* Get MSGType to sort*/
    private string GetMSGType(string strSource, string strStart, string strEnd)
    {
        int Start, End;
        if (strSource.Contains(strStart) && strSource.Contains(strEnd))
        {
            Start = strSource.IndexOf(strStart, 0) + strStart.Length;
            End = strSource.IndexOf(strEnd, Start);
            return strSource.Substring(Start, End - Start);
        }
        else
        {
            return "";
        }
    }

    public string GetCurrentPrice(string _Resource)
    {
        return "";
    }
}

You will really help me with thyt…

Greetings Pierre

Soho,

after a few hours of Coding and Testing, i have the result :smile:

public BinaryDotComConnector()
    {
        BinaryNetSocket = new WebSocket(websocketURI);
      
        BinaryNetSocket.Security.AllowNameMismatchCertificate = true;
        BinaryNetSocket.Security.EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls;

        BinaryNetSocket.Closed += new EventHandler(websocket_Closed);
        BinaryNetSocket.Error += new EventHandler<ErrorEventArgs>(websocket_Error);
        BinaryNetSocket.MessageReceived += new EventHandler<MessageReceivedEventArgs>(websocket_MessageReceived);
        BinaryNetSocket.Opened += new EventHandler(websocket_Opened);
        BinaryNetSocket.Open();
    }

You must set “Security.AllowNameMismatchCertificate” to “true” and “Security.EnabledSslProtocols” to “System.Security.Authentication.SslProtocols.Tls”.

Now i get the ssl socket Connection.

Hope this code will help some otzher to get a stabel Websocket Connection :slight_smile:

If the mass will have a tool in unity to do this, i will do an Asset Upload to the Store.

Greetings

Pierre

1 Like