-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetData.js
154 lines (148 loc) · 4.87 KB
/
GetData.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
//import {CBars,CTick} from "./Bars.js"
/*
const prompt = require('prompt');
//import * as prompt from 'prompt';
prompt.start();
prompt.get(['username', 'email'], function (err, result) {
if (err) { return onErr(err); }
console.log('Command-line input received:');
console.log(' Username: ' + result.username);
console.log(' Email: ' + result.email);
});
function onErr(err) {
console.log(err);
return 1;
}
*/
import * as readline from 'readline';
//const readline= require('readline');
const readlineInterface = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function ask(questionText) {
return new Promise((resolve, reject) => {
readlineInterface.question(questionText, resolve);
});
}
async function start() {
/*
rl.question('What do you think of Node.js? ', (answer) => {
// TODO: Log the answer in a database
console.log(`Thank you for your valuable feedback: ${answer}`);
rl.close();
process.stdin.destroy();
});
*/
let name = await ask("What's your name? ");
print(name);
print("!");
}
//await start();
//print("!!!");
function print(...args) { console.log(...args); }
//import {WebSocket.client} from 'websocket';
//import * as ws from 'websocket';
//let WebSocket= ws.client;
var WebSocket = require('ws');
//
//import * as WebSocket from 'ws';
//let z = new WebSocket("!");
//print(z);
//var WebSocketServer = require('websocket').server;
class MSRequest {
}
class MySocket {
async connect() { await this.currentTask; return this.currentTask = MySocket.connectAsync(this.socket); }
async send(msg) { await this.currentTask; this.socket.send(msg); }
static connectAsync(socket) {
return new Promise((resolve, reject) => {
socket.onopen = resolve;
socket.onerror = reject;
});
}
static sendAsync(socket) {
return new Promise((resolve, reject) => {
socket.on = resolve;
socket.onerror = reject;
});
}
}
class CDataDownloader {
constructor(onConnect, onMessage, onClose, onError) {
let socket = new WebSocket('wss://api-sb.mstrade.org/realtime/');
socket.onopen = onConnect;
socket.onmessage = onConnect;
socket.onclose = onClose;
socket.onerror = onError;
let id = CDataDownloader.__id++;
socket.id = id; // event.target.id= id;
socket.downloader = this;
this.socket = socket;
}
}
CDataDownloader.__id = 100;
subscribe(request, Readonly(), {
this: .socket.send(JSON.stringify(request))
}, public, close(), { this: .socket.close() }, get, state(), { return: this.socket.readyState },
//socketStatus.className = 'open';
//socket.send(JSON.stringify(subscribe2));
async function main(symbol) {
//var base=new CBlockC('canvas');
//function InitSocet(socket_) {socket_ = new WebSocket('wss://api-sb.mstrade.org/realtime/');};
// var socket= new WebSocket('wss://api-sb.mstrade.org/realtime/');
//print(socket);
//return;
//return;
var request = {
"op": "subscribe",
"account": "demo.demo",
"channels": "trade:" + symbol,
"schema": "margin1"
};
let onerror = function (error) { print('WebSocket Error: ' + error); };
//closeBtn.onclick = function(e) {socket.close(); return false;};
let onopen = function (event) {
print('Connected to: ' + event.target.url, " id=", event.target.id);
//socketStatus.className = 'open';
socket.send(JSON.stringify(request));
//socket.send(JSON.stringify(subscribe2));
};
socket.onmessage = function (msgEvent) {
print("message from ", msgEvent.target.id);
if (typeof msgEvent.data === "string") {
let msg = msgEvent.data; //JSON.parse(newmessage.data);
print(msg);
}
//if (mess.data!=undefined) base.Add(mess.data);
};
socket.onclose = function () {
print('Disconnected');
//socketStatus.className = 'closed';
};
var socket = new WebSocket('wss://api-sb.mstrade.org/realtime/');
/*
botonCon.onclick = function(){
//if (subscribe.channels!="trade:"+messageField.value) {socket.close();}
if (socket.readyState>=WebSocket.CLOSING) {socket.onopen();}
socket.send(JSON.stringify(subscribe));
return false;
};
*/
//await ask("asking 1");
//await ask("asking 2");
return socket;
});
//await
let sock1 = await main("btcusd");
let sock2 = await main("ETHUSD");
await ask("asking 1");
sock1.close();
sock2.close();
print("finish");
readlineInterface.close();
process.stdin.destroy();
//
// prompt("!!!!");