Charles
Charles

Reputation: 1

Connect to Rcon using WebSockets (ws)

I'm new to using web sockets in nodejs in my electronjs project. My goal is to connect to the connection my server is running, I have everything setup correctly, the port, password, and localhost, all seemed to work using another package. But when I try to create this on my own, I can't seem to get this to work.

const WebSocket = require('ws');

serverip = 'localhost';
serverport = 25575;
serverpass = 'password';
const ws = new WebSocket('ws://' + serverip + ':' + serverport + '/' + serverpass + '/');

ws.on('open', function open() {
    ws.send('/kick Player');
});

ws.on('message', function incoming(data) {
    console.log(data);
});

ws.on('close', function close() {
    console.log('disconnected');
});

What I have above is what I have so far, my console from the server I'm trying to connect to says that it is getting a connection from my pc, but doesn't relay any info besides that.

Server Output:
Rcon connection from: /127.0.0.1

This is an error I am getting in my electronjs console:

Uncaught Error: socket hang up
    at createHangUpError (_http_client.js:334)
    at Socket.socketOnEnd (_http_client.js:435)
    at Socket.emit (events.js:199)
    at endReadableNT (_stream_readable.js:1141)
    at processTicksAndRejections (internal/process/task_queues.js:81)

Any help would be greatly appreciated.

Upvotes: 0

Views: 1933

Answers (2)

ocean
ocean

Reputation: 1

ws:// => wss://

const ws = new WebSocket('wss://' + serverip + ':' + serverport + '/' + serverpass + '/');

const ws = new WebSocket('ws://' + serverip + ':' + serverport + '/' + serverpass + '/');

Upvotes: 0

Mathieu Payette
Mathieu Payette

Reputation: 1

A couple of issues with this as I'm trying to WebSocket a simple Source RCON myself. Fortunately, my code below compiles well.

var serverip = 'RconHost';
var serverport = 28016;
var serverpass = 'RconPassword';
    
const ws = new WebSocket('ws://' + serverip + ':' + serverport + '/' + serverpass);
console.log(ws);
    
ws.onopen = function(event) {
    console.log("WebSocket is onopen now.");
};
    
ws.onmessage = function(event) {
    console.log("WebSocket is onmessage now.");
};
    
ws.onclose = function(event) {
    console.log("WebSocket is onclose now.");
};

Uncaught Error: socket hang up at createHangUpError (_http_client.js:334) at Socket.socketOnEnd (_http_client.js:435) at Socket.emit (events.js:199) at endReadableNT (_stream_readable.js:1141) at processTicksAndRejections (internal/process/task_queues.js:81)

... Looks like an unreachable host. The trailing forward slash in the URL was problematic.

Upvotes: 0

Related Questions