0

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.

Charles
  • 1
  • 3
  • Is the project open source? – FaZe Unempl0yedd Jul 15 '19 at 01:19
  • No it is not at the moment, both the server and java are needed for this to function. I have the java executable within the projects directory so I really cant upload it. Keep in mind this is all being done within electronjs – Charles Jul 15 '19 at 01:22
  • Well thank you for letting me know. I don't even have this uploaded to GitHub yet, I want to get this functioning before I can take it open source. Then I need to create a bunch of back end features for users. But this is the part I'm stuck on. – Charles Jul 15 '19 at 01:28

2 Answers2

0

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.

Striped
  • 2,544
  • 3
  • 25
  • 31
0

ws:// => wss://

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

const ws = new WebSocket('ws://' + serverip + ':' + serverport + '/' + serverpass + '/');
ocean
  • 1
  • 1
    Hi, welcome to Stack Overflow, please consider elaborating your answer. – majkrzak Jan 18 '21 at 07:05
  • Hello and welcome to SO! Please read the [tour](https://stackoverflow.com/tour), and [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) For example you can elaborate how and why it solves the question. – Tomer Shetah Jan 18 '21 at 07:20