Issue
I'm mucking around building a very basic IRC bot, and am connecting to the IRC server using raw sockets. I'd written the bot in plain Javascript originally, and am redoing it in TypeScript, but have run into a bit of a weird issue when connecting over TLS (it all works fine over a non-secure connection). The source code of my bot lives on Bitbucket and the specific bit I'm having issues with is here:
[...]
this.client = this.tlsEnabled
// @ts-ignore
? new TLSSocket()
: new Socket();
this.client.connect(this.port, this.host, () => {
this.sendInitialConnectionMessages();
});
[...]
If I don't have the @ts-ignore
line, VS Code and tsc
complain about the new TLSSocket()
call:
Expected 1-2 arguments, but got 0. An argument for 'socket' was not provided.
And indeed, the type definition says it requires a socket
argument at minimum.
However, if I supply that, either via new TLSSocket(new Socket())
or const socket = new Socket()
and new TLSSocket(socket)
, the script immediately exits without hitting the this.client.connect()
call. If I tell tsc
to ignore the lack of arguments to TLSSocket()
as above, everything works perfectly and the bot successfully connects to the server via TLS!
I assume I'm doing something dumb here, but I'm just not sure what. Any help is appreciated!
Solution
Solved! After doing a bunch more searching and finding examples, I rewrote the connection functionality from what's in my original question to this:
this.client = this.tlsEnabled
? tls.connect({host: this.host, port: this.port}, () => this.sendInitialConnectionMessages())
: net.connect({host: this.host, port: this.port}, () => this.sendInitialConnectionMessages());
And everything works as expected now.
Answered By - VirtualWolf
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.