-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
49 lines (40 loc) · 1.33 KB
/
index.js
File metadata and controls
49 lines (40 loc) · 1.33 KB
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
const WebSocket = require("ws");
const websocketLink =
"wss://frontend-api.pump.fun/socket.io/?EIO=4&transport=websocket";
const ws = new WebSocket(websocketLink, {
perMessageDeflate: false,
});
ws.on("open", () => {
console.log("connected");
ws.send("40");
console.log("initialized connection");
});
ws.on("message", (data) => {
// convert buffer to string
const message = data.toString();
// Use regex to match the number (e.g., 42) and JSON separately
const match = message.match(/^(\d+)(.*)$/);
if (match) {
const messageNumber = match[1]; // The number part (e.g., "42")
const jsonString = match[2]; // The JSON part (e.g., '["tradeCreated", {...}]')
// Parse the JSON part if it's valid
try {
const json = JSON.parse(jsonString);
console.log("Message Number:", messageNumber); // Logs the number (e.g., 42)
// console.log("JSON Data:", json); // Logs the parsed JSON
const data = json[1];
console.log(data);
} catch (err) {
console.log("Failed to parse JSON:", message);
try {
const num = parseInt(messageNumber);
console.log("Message Number:", num);
ws.send(num + 1);
} catch (err) {
console.error("Failed to parse message number:", err);
}
}
} else {
console.error("Message format not recognized");
}
});