Медиа Центр

Websoket connection starting and stopping from the GUI application using the Binance website

You can, as a developer, have situations in which you need to control the flow of the program from the applications of the user interface (GUI). In this article, we will explore how to create a simple system based on the GUI that starts and stop a web science connection using the Binance Websocket Library.

PREVIOUS REQUIREMENTS

  • Install the Node.js on the computer (node.js.org).

  • Be sure to have an existing binance web library (NPM Install Binance-Webocket).

  • Create a new Gui application (for example, using an electron or system such as react, angle, etc.).

  • Establish the websockt connection to the Binance API.

Example of code

Ethereum: Starting a websocket in a function (Binance Websocket)

`JavaScript

// import the necessary libraries

Const {websockt} = ask ('binance-Websocket');

Const {createinterface} = ask ('readline');

// Initialize the websockt connection

Consto websockt = new web boots ({{

Host: "Your Binance- Exchange.com",

Port: 3000,

});

// Establish the GUI event loop

Const gui = createinterface ({{

Entrance: process.stdin,

Departure: Process.stdout,

});

// Function to start/stop the websocket connection

Handlstart Function () {

Websockt.on ('Connect', () => {

Console.log (the websockt connection is created. Home ... ');

// Start a new task when the user press Enter

Gui.once ('line', line => {

if (line === 'start') {

Websockt.send ({Team: 'Start'});

Settimaut (() => {

Console.log ('Stop Websockt Connection ...');

Websockt.close ();

}, 5000);

}

});

// Start a new task when the user presses Ctrl+C

Process.on ('Sigint', () => {

Gui.kill ();

});

});

}

// Function to start/stop the websockt connection from the command line

Handlstartcommand function () {

Const readline = ask ('readline');

Const rl = readline.createinterface ({{

Entrance: process.stdin,

Departure: Process.stdout,

});

RL.Question ('Start (Type "stop" to stop), answer => {

If (answer.tolowercase () === 'start') {

Websockt.send ({Team: 'Start'});

Settimaut (() => {

Console.log ('Stop Websockt Connection ...');

Websockt.close ();

handlestopcommand ();

}, 5000);

} else if (answer.tolowercase () === 'stop') {

handlestopcommand ();

} others {

Console.error ('Invalid entry. Exit ...');

Handlstartcommand ();

}

});

}

// Function to stop the web pocket connection

HandingCommand function () {

Websockt.send ({team: 'close'});

}

// The main program loop

While (True) {

Handlstart ();

}

`

How does it work?

  • “Handlstart” function creates a websockt event for the “Connect” event of the audience.

  • When the user presses, the websockt connection is configured and a new task begins when the user presses Ctrl+C.

  • The simple text -based interface is created using Readline.js, where users can record “start” to start the websockt or “stop” connection to stop it.

Tips and variations

  • For the GUI to be more interactive, consider add buttons to start/stop the websockt connection.

  • You can use a more modern GUI library, such as electron or respond to a local desktop application.

  • If you want a command line interface, modify the Handlstartcomand function to accept the user entry using a console.

  • Consider using a file database or storage to save data between sessions.

trading trading futures