[TOC]
Với sự tăng trưởng nhanh chóng của khối lượng giao dịch tiền điện tử, hiệu quả và tính ổn định của việc đẩy dữ liệu theo thời gian thực đã trở nên vô cùng quan trọng. Giải pháp đẩy WebSocket mới ra mắt của MEXC Exchange dựa trên Protocol Buffers (protobuf) mang lại hiệu suất truyền tải cao hơn và độ trễ thấp hơn so với các định dạng JSON truyền thống. Bài viết này trình bày chi tiết cách truy cập và xử lý dữ liệu bằng giao diện WebSocket protobuf mới của MEXC trên nền tảng giao dịch định lượng FMZ.
Protocol Buffers là một phương pháp tuần tự hóa các cấu trúc dữ liệu độc lập với ngôn ngữ và nền tảng do Google phát triển. So với định dạng JSON, protobuf có những ưu điểm sau:
Giao diện đẩy protobuf WebSocket mới được thêm vào của MEXC chủ yếu cung cấp các luồng dữ liệu sau:
https://www.mexc.com/zh-MY/api-docs/spot-v3/websocket-market-streams
Private PrivateAccountV3Api PrivateDealsV3Api PrivateOrdersV3Api
Public PublicAggreBookTickerV3Api PublicAggreDealsV3Api PublicBookTickerV3Api …
Việc triển khai giao diện WebSocket protobuf MEXC trên nền tảng FMZ yêu cầu phải chuẩn bị những điều sau:
Để thiết lậpPublicAggreDealsV3ApiLấy kết nối kênh làm ví dụ:
function main() {
var payload = {
"method": "SUBSCRIPTION",
"params": [
"[email protected]@100ms@BTCUSDT"
]
}
var conn = Dial("wss://wbs-api.mexc.com/ws|payload=" + JSON.stringify(payload))
while (true) {
var ret = conn.read()
if (ret) {
Log(ret)
}
}
}
Tìm tệp .proto cần thiết:
https://github.com/mexcdevelop // Bạn có thể tìm thấy các tệp .proto của sàn giao dịch tại đây
syntax = "proto3";
option java_package = "com.mxc.push.common.protobuf";
option optimize_for = SPEED;
option java_multiple_files = true;
option java_outer_classname = "PushDataV3ApiWrapperProto";
message PublicAggreDealsV3Api {
repeated PublicAggreDealsV3ApiItem deals = 1;
string eventType = 2;
}
message PublicAggreDealsV3ApiItem {
string price = 1;
string quantity = 2;
int32 tradeType = 3;
int64 time = 4;
}
message PushDataV3ApiWrapper {
string channel = 1;
oneof body {
PublicAggreDealsV3Api publicAggreDeals = 314;
}
optional string symbol = 3;
optional string symbolId = 4;
optional int64 createTime = 5;
optional int64 sendTime = 6;
}
Vì nền tảng FMZ dựa trên JavaScript nên chúng ta cần sử dụng thư viện protobuf.js để xử lý dữ liệu nhị phân:
Địa chỉ tải thư viện JS:
https://cdnjs.cloudflare.com/ajax/libs/protobufjs/7.5.3/protobuf.js
Dữ liệu được đẩy bởi giao diện WebSocket là dữ liệu nhị phân và cần được giải mã:
while (true) {
var ret = conn.read()
if (ret) {
const uint8arrayData = new Uint8Array(ret)
const message = PushDataV3ApiWrapper.decode(uint8arrayData)
data = PushDataV3ApiWrapper.toObject(message, {
longs: String,
enums: String,
bytes: String,
defaults: true,
arrays: true,
objects: true
})
Log("data:", data)
}
LogStatus(_D(), data)
}
let strPushDataV3ApiWrapper = `syntax = "proto3";
option java_package = "com.mxc.push.common.protobuf";
option optimize_for = SPEED;
option java_multiple_files = true;
option java_outer_classname = "PushDataV3ApiWrapperProto";
message PublicAggreDealsV3Api {
repeated PublicAggreDealsV3ApiItem deals = 1;
string eventType = 2;
}
message PublicAggreDealsV3ApiItem {
string price = 1;
string quantity = 2;
int32 tradeType = 3;
int64 time = 4;
}
message PushDataV3ApiWrapper {
string channel = 1;
oneof body {
PublicAggreDealsV3Api publicAggreDeals = 314;
}
optional string symbol = 3;
optional string symbolId = 4;
optional int64 createTime = 5;
optional int64 sendTime = 6;
}`
let code = HttpQuery("https://cdnjs.cloudflare.com/ajax/libs/protobufjs/7.5.3/protobuf.js")
let exports = {}
let module = { exports }
new Function("module", "exports", code)(module, exports)
let protobuf = module.exports
function main() {
const PushDataV3ApiWrapper = protobuf.parse(strPushDataV3ApiWrapper).root.lookupType("PushDataV3ApiWrapper")
var payload = {
"method": "SUBSCRIPTION",
"params": [
"[email protected]@100ms@BTCUSDT"
]
}
// proxy=socks5://x.x.x.x:xxxx
var conn = Dial("wss://wbs-api.mexc.com/ws|payload=" + JSON.stringify(payload))
var data = null
while (true) {
var ret = conn.read()
if (ret) {
const uint8arrayData = new Uint8Array(ret)
const message = PushDataV3ApiWrapper.decode(uint8arrayData)
data = PushDataV3ApiWrapper.toObject(message, {
longs: String,
enums: String,
bytes: String,
defaults: true,
arrays: true,
objects: true
})
Log("data:", data)
}
LogStatus(_D(), data)
}
}

Như bạn có thể thấy, dữ liệu nhị phân được giao diện đưa vào sẽ được nhận và sau khi giải mã, dữ liệu có thể đọc được sẽ được in ra:
{
"channel":"[email protected]@100ms@BTCUSDT",
"symbol":"BTCUSDT",
"sendTime":"1754487330985",
"publicAggreDeals":{
"deals":[
{"price":"113897.97","quantity":"0.00003103","tradeType":2,"time":"1754487330933"},
{"price":"113897.97","quantity":"0.00095331","tradeType":2,"time":"1754487330934"},
{"price":"113897.97","quantity":"0.00154766","tradeType":2,"time":"1754487330935"}
],
"eventType":"[email protected]@100ms"
}
}
Bằng cách triển khai giao diện WebSocket protobuf của MEXC trên nền tảng FMZ, chúng tôi đạt được những lợi thế sau:
Chương trình này đặc biệt phù hợp với:
Bài viết này được viết dựa trên đặc tả giao diện protobuf WebSocket mới nhất của sàn giao dịch MEXC. Các ví dụ mã đã được kiểm tra và xác minh trên nền tảng FMZ.