Please note that the WebSocket server needs to use this port as well. Else no communication is possible.
Here’s a step-by-step guide to set up your WebSocket server to communicate with your cBot:
Set up your WebSocket Server:
Ensure that your WebSocket server is configured to listen and send messages on port 25345.
Here is a simple example using Node.js with the ws library:
const WebSocket = require('ws');
const server = new WebSocket.Server({ port: 25345 });
server.on('connection', socket => {
console.log('Client connected');
socket.on('message', message => {
console.log('Received:', message);
// Process the message and send a response if needed
socket.send('Message received');
});
socket.on('close', () => {
console.log('Client disconnected');
});
});
console.log('WebSocket server is listening on port 25345');
Update Your cBot to Connect to the WebSocket Server:
Ensure your cBot is configured to connect to your WebSocket server. The URI should look like this: wss://example.com:25345/.
Here is an example cBot code snippet:
using System;
using System.Text;
using cAlgo.API;
using cAlgo.API.Internals;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class WebSocketBot : Robot
{
private WebSocketClient _webSocketClient;
protected override void OnStart()
{
_webSocketClient = new WebSocketClient();
var uri = new Uri("wss://example.com:25345/");
_webSocketClient.Connect(uri);
Print("WebSocket connection opened");
}
}
}
Testing:
Start your WebSocket server and ensure it is running and accessible on port 25345.
Deploy your cBot to the cTrader Cloud instance.
Monitor the logs to ensure the cBot is connecting to the WebSocket server and receiving messages correctly.
By following these steps, your cBot should be able to communicate with your WebSocket server, even in a cTrader Cloud instance.
If you encounter any issues or have further questions, please feel free to reach out.
toolsche
22 Nov 2024, 23:49
RE: C_BOT cloud instance receiving data from websocket for placing order.
PanagiotisCharalampous said:
Thank you. Works like a charm. :)
@toolsche