Skip to content

Commit

Permalink
Merge pull request #20 from singiamtel/protocol
Browse files Browse the repository at this point in the history
Add an option to choose websocket protocol
  • Loading branch information
PartMan7 authored May 18, 2024
2 parents 6875a15 + 722a5c3 commit d738eb6
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type options = {
password?: string, // The password for the username you're connecting to. Leave this blank if the account is unregistered.
server?: string, // The server to which you wish to connect to - defaults to 'sim3.psim.us'.
port?: number, // The port on which you're connecting to. Can also be specified in server as `url:port`, in which case leave this field blank.
serverProtocol?: string, // The protocol used for the websocket connection. Defaults to wss, but can be changed to ws (insecure).
connectionTimeout?: number, // The time, in milliseconds, after which your connection times out. Defaults to 20s.
loginServer?: string, // The login server. Defaults to 'https://play.pokemonshowdown.com/~~showdown/action.php'.
avatar?: string | number, // The avatar your Bot will have on connection. If not specified, PS will set one randomly.
Expand Down
1 change: 1 addition & 0 deletions client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type ClientOpts = {
server?: string,
serverid?: string,
port?: number,
serverProtocol?: string,
loginServer?: string
};

Expand Down
4 changes: 3 additions & 1 deletion client.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Client extends EventEmitter {
this.opts = {
server: 'sim3.psim.us',
serverid: 'showdown',
serverProtocol: 'wss',
connectionTimeout: 20_000,
loginServer: 'https://play.pokemonshowdown.com/action.php',
username: null,
Expand Down Expand Up @@ -122,7 +123,8 @@ class Client extends EventEmitter {
const id = ~~(Math.random() * 900) + 100;
const chars = 'abcdefghijklmnopqrstuvwxyz0123456789_'.split('');
const str = Array.from({ length: 8 }, () => chars[~~(Math.random() * 36)]).join('');
const conStr = `wss://${this.opts.server}${this.opts.port ? `:${this.opts.port}` : ''}/showdown/${id}/${str}/websocket`;
const { server, serverProtocol, port } = this.opts;
const conStr = `${serverProtocol}://${server}${port ? `:${port}` : ''}/showdown/${id}/${str}/websocket`;
this.debug(`Connecting to ${conStr}`);
webSocket.connect(conStr);
}
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dotenv.config();
const { Client, Tools, Data } = require('../client.js');

const Bot = new Client({
username: 'PS-Client',
username: process.env.PS_USERNAME ?? 'PS-Client',
password: process.env.PASSWORD,
rooms: ['botdevelopment'],
debug,
Expand Down

0 comments on commit d738eb6

Please sign in to comment.