Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to choose websocket protocol #20

Merged
merged 8 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading