//you can change environment parameters if needed
//cgate.env_open('subsystems=mq,replclient');
//you can run p2mqrouter with specified ini file
//cgate.run_p2mqrouter('p2mqrouter.ini');
//or with all parameters explicitly provided
//cgate.run_p2mqrouter(4001, 'user', 'password', 'spectra-t1.moex.com:3001', ['spectra-t1.moex.com:3000', 'spectra-t1.moex.com:3003', 'spectra-t1.moex.com:3004']);

var conn = cgate.conn_open('p2lrpcq://127.0.0.1:4001;app_name=replspy');
//wait for connection status update
if (!cgate.wait_for_conn_opened(conn, 5000)) 
{
    throw new Error('Failed to open the connection');
}

//open publisher
var forts_srv = cgate.pub_open(conn, 'p2mq://FORTS_SRV;category=FORTS_MSG;timeout=2000;name=test1')
if (0 == forts_srv)
{
    throw new Error('Failed to create publisher');
} // waiting until publisher actually opened
else if (!cgate.wait_for_pub_active(forts_srv, 5000))
{
    throw new Error('Failed to open publisher');
}

console.log('Publisher ready');

var AddOrderArr = ['FT00', 779504, '003', 1, 1, 1, '146700', '', '', 0, 0, '', 0, '2021.02.24 19:42:42.450', '', 0];
cgate.dump_msg(forts_srv, 'AddOrder', AddOrderArr); //msg_name + array
cgate.post_msg(forts_srv, 'AddOrder', AddOrderArr); //msg_name + array
cgate.dump_msg(forts_srv, 423, AddOrderArr); //msg_id + array
cgate.post_msg(forts_srv, 423, AddOrderArr); //msg_id + array

var AddOrderObj = {
    broker_code:'FT00', //c4
    isin_id:779504, //i4
    client_code:'003', //c3
    type:1, //i4
    dir:1, //i4
    amount:1, //i4
    price:'146700', //c17
    comment:'', //c20
    broker_to:'', //c20
    ext_id:0, //i4
    is_check_limit:0, //i4
    date_exp:'', //c8
    dont_check_money:0, //i4
    local_stamp:'2021.02.24 19:42:42.450', //t
    match_ref:'', //c10
    ncc_request:0 //i1
};

cgate.dump_msg(forts_srv, 'AddOrder', AddOrderObj); //msg_name + object
cgate.post_msg(forts_srv, 'AddOrder', AddOrderObj); //msg_name + object
cgate.dump_msg(forts_srv, 423, AddOrderObj); //msg_id + object
cgate.post_msg(forts_srv, 423, AddOrderObj); //msg_id + object

//cgate.pub_close(conn, forts_srv);

//cgate.conn_close(conn);
