smogon / pokemon-showdown

Pokémon battle simulator.
https://pokemonshowdown.com
MIT License
4.76k stars 2.78k forks source link

Testing WebSocket connections on Personal Server #8601

Closed curtdissel closed 2 years ago

curtdissel commented 2 years ago

Hullo Staff,

I have a partially running server here. It works on http, but not https. My server isn't registered. I wanted to test it out before applying. http://raw-showdown-test.herokuapp.com-80.psim.us

I am testing websocket connections so that can see if my bots can connect.

I am able to connect to localhost with this path: ws://localhost:8000/showdown/906/rfwopjis/websocket

I am able to connect to the official showdown with this path: ws://sim3.psim.us:8000/showdown/906/rfwopjis/websocket

However none of these paths for my server doesn't yield me a web socket connection: ws://raw-showdown-test.herokuapp.com-80.psim.us:8000/showdown/906/rfwopjis/websocket ws://raw-showdown-test.herokuapp.com-80:8000/showdown/906/rfwopjis/websocket ws://raw-showdown-test.herokuapp.com:8000/showdown/906/rfwopjis/websocket

Given my investigation, I am under the impression that there are 3 possibilities. 1) I setup my server wrong 2) I used the wrong url for web sockets 3) Pokemon Showdown has a way to restrict Web Socket access to various servers.

I am hoping there is a direct url that I can use since my server isn't registered.

Slayer95 commented 2 years ago

Since your server isn't registered, you can only use a heroku url for the WS connection, and it's outside Smogon's competence.

Please note that shared hosting will not necessarily allow inbound connections in all ports. Check your PORT env variable so you match your permissions.

Also, deleting the port entry in config.js will fall back to env variables.

Once you figure your ws server out, use http://HOSTNAME-PORT.psim.us to connect to it using the main client.

curtdissel commented 2 years ago

Long story short, I needed to remove the psim.us from the url to get web sockets to connection. Below is my investigation.

I did notice something. If I enter: http://raw-showdown-test.herokuapp.com-80.psim.us/showdown/733/35hcypl4/websocket

it actually becomes: https://play.pokemonshowdown.com/showdown/733/35hcypl4/websocket

That made me think that the problem is everything is redirecting to the official pokemon showdown stuff, and since my server isn't registered, nothing is getting forwarded. So I changed the url to: ws://raw-showdown-test.herokuapp.com/showdown/733/35hcypl4/websocket

And got back

> Log will appear here
> Connecting to: ws://raw-showdown-test.herokuapp.com/showdown/733/35hcypl4/websocket
> Connection established
> RECEIVED: o
> RECEIVED: a["|updateuser| Guest 4|0|101|{\"blockChallenges\":false,\"blockPMs\":false,\"ignoreTickets\":false,\"hideBattlesFromTrainerCard\":false,\"blockInvites\":false,\"doNotDisturb\":false,\"hiddenNextBattle\":false,\"inviteOnlyNextBattle\":false,\"language\":null}\n|customgroups|[{\"symbol\":\"&\",\"name\":\"Administrator\",\"type\":\"leadership\"},{\"symbol\":\"#\",\"name\":\"Room Owner\",\"type\":\"leadership\"},{\"symbol\":\"★\",\"name\":\"Host\",\"type\":\"leadership\"},{\"symbol\":\"@\",\"name\":\"Moderator\",\"type\":\"staff\"},{\"symbol\":\"%\",\"name\":\"Driver\",\"type\":\"staff\"},{\"symbol\":\"*\",\"name\":\"Bot\",\"type\":\"normal\"},{\"symbol\":\"☆\",\"name\":\"Player\",\"type\":\"normal\"},{\"symbol\":\"+\",\"name\":\"Voice\",\"type\":\"normal\"},{\"symbol\":\"^\",\"name\":\"Prize Winner\",\"type\":\"normal\"},{\"symbol\":\"whitelist\",\"name\":\"Whitelist\",\"type\":\"normal\"},{\"symbol\":\" \",\"name\":null,\"type\":\"normal\"},{\"symbol\":\"‽\",\"name\":\"Locked\",\"type\":\"punishment\"},{\"symbol\":\"!\",\"name\":\"Muted\",\"type\":\"punishment\"}]\n|formats|,LL|,1|Sw/Sh Singles|[Gen 8] Random Battle,f|[Gen 8] Unrated Random Battle,b|[Gen 8] OU,e|[Gen 8] OU (Blitz),e|[Gen 8 DLC 1] OU,c|[Gen 8] Ubers,e|[Gen 8] UU,e|[Gen 8] RU,e|[Gen 8] NU,e|[Gen 8] PU,e|[Gen 8] LC,e|[Gen 8] Monotype,e|[Gen 8] 1v1,e|[Gen 8] Anything Goes,e|[Gen 8] ZU,e|[Gen 8] CAP,e|[Gen 8] Battle Stadium Singles,1e|[Gen 8] Tundra Tourney,1e|[Gen 8] Custom Game,c|,1|Sw/Sh Doubles|[Gen 8] Random Doubles Battle,f|[Gen 8] Doubles OU,e|[Gen 8 DLC 1] Doubles OU,c|[Gen 8] Doubles Ubers,e|[Gen 8] Doubles UU,e|[Gen 8] VGC 2021,1e|[Gen 8] VGC 2020,1c|[Gen 8] 2v2 Doubles,e|[Gen 8] Metronome Battle,e|[Gen 8] Doubles Custom Game,c|,1|National Dex|[Gen 8] National Dex,e|[Gen 8] National Dex UU,e|[Gen 8] National Dex Monotype,e|[Gen 8] National Dex AG,e|[Gen 8] National Dex BH,e|,1|Pet Mods|[Gen 8] ViAbilities,e|[Gen 8] CCAPM 2020,c|[Gen 6] Gen-NEXT OU,8|,2|OM of the Month|[Gen 8] Trademarked,e|[Gen 8] Scalemons,e|,2|Other Metagames|[Gen 8] Balanced Hackmons,e|[Gen 8] Mix and Mega,e|[Gen 8] Almost Any Ability,e|[Gen 8] STABmons,e|[Gen 8] Camomons,e|[Gen 8] NFE,e|[Gen 8] Cross Evolution,c|[Gen 8] Godly Gift,c|[Gen 8] Inheritance,c|[Gen 8] Pure Hackmons,c|[Gen 8] Shared Power,c|[Gen 8] Tier Shift,c|[Gen 7] Balanced Hackmons,c|,2|Randomized Metas|[Gen 8] Monotype Random Battle,f|[Gen 8] Challenge Cup 1v1,f|[Gen 8] Challenge Cup 2v2,d|[Gen 8] Hackmons Cup,f|[Gen 8] Doubles Hackmons Cup,d|[Gen 8] CAP 1v1,f|[Gen 7] Random Battle,f|[Gen 7] Random Doubles Battle,d|[Gen 7] Battle Factory,f|[Gen 7] BSS Factory,d|[Gen 7] Hackmons Cup,d|[Gen 7] Super Staff Bros Brawl,f|[Gen 7 Let's Go] Random Battle,d|[Gen 6] Random Battle,f|[Gen 6] Battle Factory,d|[Gen 5] Random Battle,f|[Gen 4] Random Battle,f|[Gen 3] Random Battle,f|[Gen 2] Random Battle,f|[Gen 1] Random Battle,f|[Gen 1] Challenge Cup,9|,3|RoA Spotlight|[Gen 6] Ubers,e|[Gen 4] NU,e|[Gen 3] Doubles OU,e|,3|Past Gens OU|[Gen 7] OU,e|[Gen 6] OU,e|[Gen 5] OU,e|[Gen 4] OU,e|[Gen 3] OU,e|[Gen 2] OU,e|[Gen 1] OU,e|,3|US/UM Singles|[Gen 7] Ubers,e|[Gen 7] UU,c|[Gen 7] RU,c|[Gen 7] NU,c|[Gen 7] PU,c|[Gen 7] LC,c|[Gen 7] Monotype,c|[Gen 7] 1v1,e|[Gen 7] Anything Goes,e|[Gen 7] ZU,c|[Gen 7] CAP,c|[Gen 7] Battle Spot Singles,1c|[Gen 7 Let's Go] OU,1c|[Gen 7] Custom Game,c|,3|US/UM Doubles|[Gen 7] Doubles OU,e|[Gen 7] Doubles UU,c|[Gen 7] VGC 2019,1c|[Gen 7] VGC 2018,1c|[Gen 7] VGC 2017,1c|[Gen 7] Battle Spot Doubles,1c|[Gen 7] Doubles Custom Game,c|,4|OR/AS Singles|[Gen 6] UU,c|[Gen 6] RU,c|[Gen 6] NU,c|[Gen 6] PU,c|[Gen 6] LC,c|[Gen 6] Monotype,c|[Gen 6] 1v1,c|[Gen 6] Anything Goes,c|[Gen 6] CAP,c|[Gen 6] Battle Spot Singles,1c|[Gen 6] Custom Game,c|,4|OR/AS Doubles/Triples|[Gen 6] Doubles OU,c|[Gen 6] VGC 2016,1c|[Gen 6] Battle Spot Doubles,1c|[Gen 6] Doubles Custom Game,c|[Gen 6] Battle Spot Triples,1c|[Gen 6] Triples Custom Game,c|,4|B2/W2 Singles|[Gen 5] Ubers,c|[Gen 5] UU,c|[Gen 5] RU,c|[Gen 5] NU,c|[Gen 5] PU,c|[Gen 5] LC,c|[Gen 5] Monotype,c|[Gen 5] 1v1,c|[Gen 5] GBU Singles,1c|[Gen 5] Custom Game,c|,4|B2/W2 Doubles|[Gen 5] Doubles OU,c|[Gen 5] GBU Doubles,1c|[Gen 5] Doubles Custom Game,c|[Gen 5] Triples Custom Game,c|,5|DPP Singles|[Gen 4] Ubers,c|[Gen 4] UU,c|[Gen 4] PU,c|[Gen 4] LC,c|[Gen 4] 1v1,c|[Gen 4] Anything Goes,c|[Gen 4] Custom Game,c|,5|DPP Doubles|[Gen 4] Doubles OU,c|[Gen 4] Doubles Custom Game,c|,5|Past Generations|[Gen 3] Ubers,c|[Gen 3] UU,c|[Gen 3] NU,c|[Gen 3] 1v1,c|[Gen 3] Custom Game,c|[Gen 3] Doubles Custom Game,c|[Gen 2] Ubers,c|[Gen 2] UU,c|[Gen 2] NU,c|[Gen 2] Custom Game,c|[Gen 1] Ubers,c|[Gen 1] UU,c|[Gen 1] OU (Tradeback),c|[Gen 1] Stadium OU,c|[Gen 1] Custom Game,c"]
> RECEIVED: a["|challstr|4|69501b8e2725b881ee0ba267581b481a914b870235d56bb2282f9dc0d0be0ce4aefb62d9438d4a003fdd5319b5c3b4cb57d880fab095b1278ea1202f9a1568d46b907d849222fb1d37f8faf2666c2f17a1cf2c9fb37ca3a4e1ab08f0d6104c63c822e6835df99782c03c0e549726186e46b9f702f930574bc20fd1673ccde121"]
Zarel commented 2 years ago

Your problem seems to be that Heroku is proxying requests between the URL it gives you and the PS server actually running. What you need to do is read Heroku's documentation.

PS's websocket URLs are:

ws://[LOCATION]:[PORT]/showdown/websocket

Where [LOCATION] is your server IP address (which is localhost for a local server), and [PORT] is your server's port, defaulting to 8000.

Your job is to figure out what Heroku rewrites this URL to. Heroku's documentation should tell this to you. I'm very surprised ws://raw-showdown-test.herokuapp.com/showdown/733/35hcypl4/websocket works, because it's crazy that they would just add random numbers in the middle of the URL. Where do the random tokens come from? Maybe ws://raw-showdown-test.herokuapp.com/showdown/websocket would work?

Zarel commented 2 years ago

If you visit http://[LOCATION]:[PORT]/[ROOMID], you will be redirected to http://[LOCATION]-[PORT].psim.us/[ROOMID], which serves a client that connects to ws://[LOCATION]:[PORT]/showdown/websocket. That might be the redirect you're having trouble with.

curtdissel commented 2 years ago

You are correct: ws://raw-showdown-test.herokuapp.com/showdown/websocket Also works.

Oh as for the random numbers in the url, I took that from a template of a pokemon showdown python library I was using. In their generate websocket url, they add random characters.

I took his code as a template to follow:

WEBSOCKET_URL_BASE = (
    "ws://{server_hostname}/showdown/{num_triplet}/{char_octet}/websocket"
)

https://github.com/ckw017/showdown.py/blob/master/showdown/server.py#L17

As an update, I was able to fight my trashy bot on my server. using websockets. Had to play around with the web socket and action urls, until something worked.