1

How can I connect to pterodactyl's websocket server to fetch the terminal of a server within pterodactyl, with the websocket wrapper library for Node.js – websocket. I have done the half by obtaining the access token with the account API key but I'm having trouble to connect using that token.

Here's my code by far, feel free to edit it:

var fetch = require("node-fetch");
var WebSocketClient = require("websocket").client;

var config = class {
    static panelUrl = "http://*************.icedev.tk";
    static pterodactylUserApiKey = "********************************************FnTI";
};


// Get a websocket access token
fetch(`${config.panelUrl}/api/client/servers/5f1ad850/websocket`, {
    headers: {
        "Accept": "application/json",
        "Content-Type": "application/json",
        "Authorization": `Bearer ${config.pterodactylUserApiKey}`
    }
}).then(res => res.json())
.then((json) => {
// Assign gvariables to the tokens

    const token = json.data.token,
          socket = json.data.socket;

    var client = new WebSocketClient();


// Websocket Events
    client.on("connectFailed", function(error) {
        console.log("Connect Error: " + error.toString());
    });
    
    client.on("connect", function(connection) {
        console.log("WebSocket Client Connected");
        
        connection.on("error", function(error) {
            console.log("Connection Error: " + error.toString());
        });

        connection.on("close", function() {
            console.log("echo-protocol Connection Closed");
        });

        connection.on("message", function(message) {
            if (message.type === "utf8") {
                console.log("Received: '" + message.utf8Data + "'");
            }
        });
    });
    

// connect to the socket
    client.connect(`${socket}`, ["Sec-WebSocket-Protocol"], "*", `Authorization: Bearer ${token}`);
});
Dharman
  • 30,962
  • 25
  • 85
  • 135

0 Answers0