Skip to content

Commit 75212df

Browse files
authored
Add files via upload
1 parent 273a163 commit 75212df

File tree

4 files changed

+410
-0
lines changed

4 files changed

+410
-0
lines changed

example/java.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# Java 请求数据示例
2+
3+
4+
5+
6+
7+
## 请求K线
8+
9+
```java
10+
HttpRequest request = HttpRequest.newBuilder()
11+
.uri(URI.create("https://api.itick.org/stock/kline?region=hk&code=700.HK&kType=1"))
12+
.header("accept", "application/json")
13+
.header("token", "you_apikey")
14+
.method("GET", HttpRequest.BodyPublishers.noBody())
15+
.build();
16+
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
17+
System.out.println(response.body());
18+
```
19+
20+
21+
22+
## 请求实时报价
23+
24+
```java
25+
HttpRequest request = HttpRequest.newBuilder()
26+
.uri(URI.create("https://api.itick.org/stock/tick?region=HK&code=700.HK"))
27+
.header("accept", "application/json")
28+
.header("token", "you_apikey")
29+
.method("GET", HttpRequest.BodyPublishers.noBody())
30+
.build();
31+
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
32+
System.out.println(response.body());
33+
```
34+
35+
36+
37+
## 订阅实时报价
38+
39+
```xml
40+
<dependency>
41+
<groupId>javax.websocket</groupId>
42+
<artifactId>javax.websocket-api</artifactId>
43+
<version>1.1</version>
44+
</dependency>
45+
```
46+
47+
48+
49+
```java
50+
import javax.websocket.ClientEndpoint;
51+
import javax.websocket.CloseReason;
52+
import javax.websocket.ContainerProvider;
53+
import javax.websocket.OnClose;
54+
import javax.websocket.OnError;
55+
import javax.websocket.OnMessage;
56+
import javax.websocket.OnOpen;
57+
import javax.websocket.Session;
58+
import javax.websocket.WebSocketContainer;
59+
import java.io.IOException;
60+
import java.net.URI;
61+
import java.net.URISyntaxException;
62+
63+
// 定义WebSocket客户端端点
64+
@ClientEndpoint
65+
public class WebSocketSubscriber {
66+
67+
// WebSocket服务器的地址
68+
private static final String WEBSOCKET_SERVER_URL = "wss://api.itick.org/sws";
69+
70+
// 用于鉴权
71+
private static final String AUTH_MESSAGE = "{\n" +
72+
" \"ac\":\"auth\",\n" +
73+
" \"params\":\"you_apikey\"\n" +
74+
"}";
75+
76+
// 用于订阅的消息格式,这里假设订阅一个名为 "your_channel" 的频道
77+
private static final String SUBSCRIBE_MESSAGE = "{\n" +
78+
" \"ac\":\"subscribe\",\n" +
79+
" \"params\":\"AM.LPL,AM.LPL\",\n" +
80+
" \"types\":\"depth,quote\"\n" +
81+
"}";
82+
83+
public static void main(String[] args) {
84+
try {
85+
// 创建WebSocket容器
86+
WebSocketContainer container = ContainerProvider.getContainer();
87+
88+
// 连接到WebSocket服务器并获取会话
89+
Session session = container.connectToServer(WebSocketSubscriber.class, new URI(WEBSOCKET_SERVER_URL));
90+
91+
// 发送鉴权消息
92+
session.getBasicRemote().sendText(AUTH_MESSAGE);
93+
94+
// 发送订阅消息
95+
session.getBasicRemote().sendText(SUBSCRIBE_MESSAGE);
96+
97+
} catch (URISyntaxException | IOException e) {
98+
e.printStackTrace();
99+
}
100+
}
101+
102+
@OnOpen
103+
public void onOpen(Session session) {
104+
System.out.println("WebSocket连接已打开");
105+
}
106+
107+
@OnMessage
108+
public void onMessage(Session session, String message) {
109+
System.out.println("收到消息: " + message);
110+
// 这里可以根据收到的消息内容进行进一步的处理,比如解析JSON数据等
111+
}
112+
113+
@OnError
114+
public void onError(Session session, Throwable error) {
115+
System.out.println("WebSocket错误: " + error.getMessage());
116+
}
117+
118+
@OnClose
119+
public void onClose(Session session, CloseReason closeReason) {
120+
System.out.println("WebSocket连接已关闭,原因: " + closeReason.getReasonPhrase());
121+
}
122+
}
123+
```
124+

example/node.md

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# Node 请求数据示例
2+
3+
4+
5+
6+
7+
## 请求K线
8+
9+
```node
10+
const http = require('https');
11+
12+
const options = {
13+
method: 'GET',
14+
hostname: 'api.itick.org',
15+
port: null,
16+
path: '/stock/kline?region=hk&code=700.HK&kType=1',
17+
headers: {
18+
accept: 'application/json',
19+
token: 'you_apikey'
20+
}
21+
};
22+
23+
const req = http.request(options, function (res) {
24+
const chunks = [];
25+
26+
res.on('data', function (chunk) {
27+
chunks.push(chunk);
28+
});
29+
30+
res.on('end', function () {
31+
const body = Buffer.concat(chunks);
32+
console.log(body.toString());
33+
});
34+
});
35+
36+
req.end();
37+
```
38+
39+
40+
41+
## 请求实时报价
42+
43+
```shell
44+
const http = require('https');
45+
46+
const options = {
47+
method: 'GET',
48+
hostname: 'api.itick.org',
49+
port: null,
50+
path: '/stock/tick?region=HK&code=700.HK',
51+
headers: {
52+
accept: 'application/json',
53+
token: 'you_apikey'
54+
}
55+
};
56+
57+
const req = http.request(options, function (res) {
58+
const chunks = [];
59+
60+
res.on('data', function (chunk) {
61+
chunks.push(chunk);
62+
});
63+
64+
res.on('end', function () {
65+
const body = Buffer.concat(chunks);
66+
console.log(body.toString());
67+
});
68+
});
69+
70+
req.end();
71+
```
72+
73+
74+
75+
## 订阅实时报价
76+
77+
`npm install ws`
78+
79+
```shell
80+
const WebSocket = require('ws');
81+
const url = 'wss://api.itick.org/sws';
82+
83+
# 用于鉴权
84+
const authMessage = {
85+
"ac":"auth",
86+
"params":"you_apikey"
87+
}
88+
89+
# 用于订阅的消息格式,这里假设订阅一个名为 "your_channel" 的频道
90+
const subscribeMessage = {
91+
"ac":"subscribe",
92+
"params":"AM.LPL,AM.LPL",
93+
"types":"depth,quote"
94+
}
95+
96+
// 创建WebSocket连接
97+
const ws = new WebSocket(url);
98+
99+
ws.on('open', () => {
100+
console.log('WebSocket连接已打开,正在发送鉴权消息...');
101+
102+
# 发送鉴权消息
103+
ws.send(JSON.stringify(authMessage));
104+
105+
// 将订阅消息转换为JSON格式并发送
106+
ws.send(JSON.stringify(subscribeMessage));
107+
});
108+
109+
ws.on('message', (message) => {
110+
console.log('收到消息:', message);
111+
// 这里可以根据收到的消息内容进行进一步的处理,比如解析JSON数据等
112+
const data = JSON.parse(message);
113+
if (data.hasOwnProperty('data')) {
114+
console.log('数据内容:', data['data']);
115+
}
116+
});
117+
118+
ws.on('error', (error) => {
119+
console.log('WebSocket错误:', error);
120+
});
121+
122+
ws.on('close', (code, reason) => {
123+
console.log('WebSocket连接已关闭,代码:', code, '原因:', reason);
124+
});
125+
```
126+

example/python.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Python 请求数据示例
2+
3+
4+
5+
## 请求K线
6+
7+
`python -m pip install requests`
8+
9+
```python
10+
import requests
11+
12+
url = "https://api.itick.org/stock/kline?region=hk&code=700.HK&kType=1"
13+
14+
headers = {
15+
"accept": "application/json",
16+
"token": "you_apikey"
17+
}
18+
19+
response = requests.get(url, headers=headers)
20+
21+
print(response.text)
22+
```
23+
24+
25+
26+
## 请求实时报价
27+
28+
```python
29+
import requests
30+
31+
url = "https://api.itick.org/stock/tick?region=HK&code=700.HK"
32+
33+
headers = {
34+
"accept": "application/json",
35+
"token": "you_apikey"
36+
}
37+
38+
response = requests.get(url, headers=headers)
39+
40+
print(response.text)
41+
```
42+
43+
44+
45+
## 订阅实时报价
46+
47+
`pip install websocket-client`
48+
49+
```python
50+
import websocket
51+
import json
52+
53+
# WebSocket服务器的地址
54+
websocket_url = "wss://api.itick.org/sws"
55+
56+
# 用于鉴权
57+
auth_message = {
58+
"ac":"auth",
59+
"params":"you_apikey"
60+
}
61+
62+
# 用于订阅的消息格式,这里假设订阅一个名为 "your_channel" 的频道
63+
subscribe_message = {
64+
"ac":"subscribe",
65+
"params":"AM.LPL,AM.LPL",
66+
"types":"depth,quote"
67+
}
68+
69+
def on_open(ws):
70+
"""
71+
当WebSocket连接打开时调用的函数
72+
"""
73+
print("WebSocket连接已打开,正在发送鉴权消息...")
74+
75+
# 发送鉴权消息
76+
ws.send(json.dumps(auth_message))
77+
78+
# 将订阅消息转换为JSON格式并发送
79+
ws.send(json.dumps(subscribe_message))
80+
81+
def on_message(ws, message):
82+
"""
83+
当收到WebSocket消息时调用的函数
84+
"""
85+
print(f"收到消息: {message}")
86+
# 这里可以根据收到的消息内容进行进一步的处理,比如解析JSON数据等
87+
data = json.loads(message)
88+
if "data" in data:
89+
print(f"数据内容: {data['data']}")
90+
91+
def on_error(ws, error):
92+
"""
93+
当WebSocket连接出现错误时调用的函数
94+
"""
95+
print(f"WebSocket错误: {error}")
96+
97+
def on_close(ws, close_status_code, close_msg):
98+
"""
99+
当WebSocket连接关闭时调用的函数
100+
"""
101+
print(f"WebSocket连接已关闭,状态码: {close_status_code},消息: {close_msg}")
102+
103+
if __name__ == "__main__":
104+
# 创建WebSocket对象并设置回调函数
105+
ws = websocket.WebSocketApp(websocket_url,
106+
on_open=on_open,
107+
on_message=on_message,
108+
on_error=on_error,
109+
on_close=on_close)
110+
111+
# 启动WebSocket连接,开始监听消息
112+
ws.run_forever()
113+
```
114+

0 commit comments

Comments
 (0)