Websocket how? - page 4

 
Алексей Барбашин:

Fedor, where were you?

I'm actually sitting here, stumped.

A window popped up with Usercontrol. That's all.

 

Found this example:

using System;
using System.Text;
using System.Threading.Tasks;
using System.Net.WebSockets;
using System.Threading;
using System.Net;

namespace BinaryWSDemo
{
    class BinaryWS
    {
        private ClientWebSocket ws = new ClientWebSocket();
        private Uri uri = new Uri("wss://ws.binaryws.com/websockets/v3?app_id=1089");
            
        public async Task SendRequest(string data)
        {
                
                while(this.ws.State == WebSocketState.Connecting){};
            if (this.ws.State != WebSocketState.Open)
            {
                throw new Exception("Connection is not open.");
            }
            
            var reqAsBytes = Encoding.UTF8.GetBytes(data);
            var ticksRequest = new ArraySegment<byte>(reqAsBytes);

            await this.ws.SendAsync(ticksRequest,
                WebSocketMessageType.Text,
                true,
                CancellationToken.None);
            
            Console.WriteLine("The request has been sent: ");
            Console.WriteLine(data);
            Console.WriteLine("\r\n \r\n");

            
        }
        
        public async Task StartListen(){
                WebSocketReceiveResult result;
                while (this.ws.State == WebSocketState.Open){
                var buffer = new ArraySegment<byte>(new byte[1024]);
                    do
                    {
                        result = await this.ws.ReceiveAsync(new ArraySegment<byte>(buffer.Array), CancellationToken.None);

                        if (result.MessageType == WebSocketMessageType.Close)
                        {
                                Console.WriteLine("Connection Closed!");
                                break;
                        }
                        else
                        {
                            var str = Encoding.UTF8.GetString(buffer.Array, 0, result.Count);
                            Console.WriteLine("Received Data at: " + DateTime.Now);
                            Console.WriteLine(str);
                            Console.WriteLine("\r\n");
                        }

                    } while (!result.EndOfMessage);
                }
        }
        
        public async Task Connect(){
                Console.WriteLine("Prepare to connect to: " + this.uri.ToString());
                Console.WriteLine("\r\n");
                
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
                await ws.ConnectAsync(uri, CancellationToken.None);
                
                Console.WriteLine("The connection is established!");
                Console.WriteLine("\r\n");
        }

        static void Main(string[] args)
        {
            
                        string data = "{\"ticks\":\"R_100\"}";
            
                        var bws = new BinaryWS();
                        bws.Connect().Wait();
                        
                        bws.SendRequest(data).Wait();
                        bws.StartListen();
            
            Console.ReadLine();
        }
    }
}

Trying to run it in Visual Studio code is C#

 
Алексей Барбашин:

This won't work.ClientWebSocket is not supported in all windows versions.

I've offered you a proven way, but you want to go your own way and get your own bumps. That's your right.

Then you don't need to get into sharp. If you're good at java, you can use it to write dlls.

I didn't say I was good at java. I installed it, created a project, I don't know what to do next. I need to look for examples.

 
Алексей Барбашин:

Fedor, what is the purpose of asking for help on the forum? No one will give you a ready-made example, as every project is unique. Only the bricks can be the same. That's exactly what they offer you.

Let's start with an easy one: Do you have any programming experience? At least in mql? Do you know what a "class" is in programming?

Yes, of course.

 
Fedor Arkhipov:

Hi all!

I've got a task to receive quotes from huobi via websocket and build an offline chart using these quotes.

I realized that this cannot be done using standard means.

I have an idea to write a dll in java and use it to receive quotes.

Is such an implementation possible?

I have never done anything like that before, so I will be very grateful for your help!

If you want to use Mql DLL to call Java, it's cooler than tonsils via gp :-)

but if you really want to, you may. But additionally, you have to know C at a sufficient level and understand how threads are arranged inside MT, how and when they are created/transformed.

there are many articles on how to be friends with Java and C/C++, the first google search gives https://www.codeproject.com/Articles/993067/Calling-Java-from-Cplusplus-with-JNI.

see local articles on how to be friends with C/C++ and Mql

you can once sit down and do the Mql->(c/c++ dll)->Java layer. It seems very maternal in terms of debugging (emotions have to be kept in check), but once done then use.

PS/ and there is a feeling that Mql is going towards Sharp, and it is not friendly with Java at all...so in version 6 efforts will be wasted

Calling Java from C++ with JNI
  • cth027
  • www.codeproject.com
How do we return an array of integers from Java to C++. And this question follows up to 2d and 3d arrays as well. Also what would be the method to call Java function (assuming we are calling static function). Example: ID: method = m_pJVMInstance->m_pEnv->GetStaticMethodID(class, "getIntDataS", "([Ljava/lang/String;)[I"); Calling: jint...
 
Fedor Arkhipov:

I didn't say I knew java very well. I installed it, created a project, I don't know what to do next. I need to look for examples.

Be realistic, can you handle the protocol between the client/server? Get a ready-made libu for the exchange, otherwise you will drown.

 
Алексей Барбашин:

Yes, the Russian version.


 
Алексей Барбашин:

Shall we take it one step at a time? :)

Well, if you help me.
 
Алексей Барбашин:

Of course I will. I hope I'm not the only one. Just try to respond more actively to messages.

ok
 

Made by