Skip to content
This repository has been archived by the owner on Oct 1, 2022. It is now read-only.

Webinterface rest and websocket #9

Open
Buccaneersdan opened this issue Nov 16, 2019 · 5 comments
Open

Webinterface rest and websocket #9

Buccaneersdan opened this issue Nov 16, 2019 · 5 comments

Comments

@Buccaneersdan
Copy link

Hello again.

As you suggested in issue #8 to update go, my dummy-self couldn't find the time to update Go manually and managed it with this helper. The "undefined: sort.SliceStable" is gone and I possess a nice "yuri2" executable now.

What I've done:

  • I've registered a bot in discord, obtained the client-id and secret.

In application.yaml:

  • server.port: 2333
  • server.address: 127.0.0.1
  • Entered password in lavalink.server.password

In config.yaml:

  • api.client_id and api.client_secret from Bot-registration
  • discord.owner_id in the format USERNAME#NUMBER
  • lavalink.address: localhost:2333
  • lavalink.password: the same as in application.yaml
  • lavalink.sounds_locations configured

Started the Lavalink.jar:
No error.

Started yuri:

  • No error.
  • Found sounds, too
  • Opened ws://localhost:2333
  • Gave an invite link for the bot

I invited yuri2 to my discord. It entered the building.

Compiled the web-folder with ng.

When I started the development-server, the userinterface is shown on localhost:4200 but it fails to connect to rest and websockets-server on port 8080. Is that the wrong port maybe? I changed it meanwhile to 2333, for testing in rest.service.ts and ws.service.ts. Chrome is giving me this:

Bildschirmfoto

This all looks very promising execpt the last step and as soon as I'm able to broadcast the first sound in my server, I make the installation permanent with pm2 (I guess).

I'm also fine without a web-interface, if I could command yuri2 via discord.

Is the latter possible or can you imagine what the root cause of the failing rest and websocket servers is?

Thanks.

@yukinose
Copy link

yukinose commented Dec 4, 2019

try changing the ports in rest.service.ts and ws.service.ts to 4200 in your example

@Buccaneersdan
Copy link
Author

Buccaneersdan commented Dec 5, 2019

Ok thanks. Now I don't get a 404 for "api/localsounds" anymore. The ws-endpoint localsounds seems to be found, but the webapp can't authenticate to the websocket (see at the bottom).

This is what I did:

  • In yuri2/bin/application.yaml: server.port=4200
  • In yuri2/bin/config.yaml: lavalink.address=localhost:4200
  • In yuri2/web/src/app/api/rest/rest.service.ts in line 75:
    this.rootURL = environment.production ? '' : 'http://localhost:4200';
  • In yuri2/web/src/app/api/ws/ws.service.ts in line 31:
    : 'ws://localhost:4200/ws';

Without changing port to 4200 in application.yaml and config.yaml, I get the 404 for "api/localsounds" again.

Results:
Lavalink starts on port 4200
yuri2 connects to Lavalink and loads the sound files.
ng serve on webapp runs the app on 8080

When opening localhost:8080 I get the UI and some ws and rest exception toast-errors.

Network looks like this:
Bildschirmfoto-1

The 4 errors roughly correspond to the toast-errors:

So how do I let the webapp know the credentials of the rest and websocket services?
Got no experience in Angular so I don't get how the HttpClient is injected into the constructor of RestService and where it possibly could get the credentials. In rest.service.ts your using withCredentials: true though.

Thanks for reading and possibly thinking about it.

@yukinose
Copy link

yukinose commented Dec 6, 2019

i am not that familiar with the bot so i can't really give you much information about how it works (i'm not zekro). i have gotten the bot to work though and here are some steps i took:

  • In yuri2/bin/application.yaml: server.port= 2333
  • In yuri2/bin/config.yaml: lavalink.address=localhost:2333; api.address: 8080; api.public_address: http://localhost:8080
  • In yuri2/web/src/app/api/rest/rest.service.ts in line 75: set port to 8080
  • In yuri2/web/src/app/api/ws/ws.service.ts in line 31: set port to 8080

then, build the web server and move it into bin via the steps zekro provided in the previous issue:

$ cd web
$ npm install
$ npm run build --prod
$ cd ..
$ mkdir bin/web
$ cp -r web/dist/ bin/web/

after running the binary, you should be able to connect to the front end via localhost:8080. it should ask for "log in via discord", which is an oauth2 redirect... you will need to add http://localhost:8080/login/authorize to your "OAuth2 Redirects" via: discord developer portal > [your application] > OAuth2.

this should help you get it running. the bot also runs the web interface so you shouldn't have to run it via "ng serve". it seems that the cookie generated by authenticating via oAuth2 is used for restApi authentication (see the "docs" folder for more information).

@Buccaneersdan
Copy link
Author

Buccaneersdan commented Dec 6, 2019

Thanks so much @yukinose and @zekroTJA for the detailed instructions. The webinterface is running now and is presenting the defined songs in config.yaml->lavalink.sound_locations.

I still stumbled on one showstopper:
After starting a song in the webinterface I get a "WS Request Error - command failed: unexpected end of JSON input". My bot plays nothing.

You can close this issue atm. I'll try to figure that out myself and annoy you again, if I won't figure it out.

Edit: I'll try to debug it with chrome debugger now and see where the json is mangled. Will get back with my results here.

Edit 2: I guess I have to build lavalink and debug, as it's possible that it receives unexpected JSON by discord or something. Nothing suspicious in lavalink-logs. The chrome debugger wasn't any help, as the only debuggable codepoint was in ws.service.ts->onMessage() where I get just that brief error-message posted before.

@Buccaneersdan
Copy link
Author

Buccaneersdan commented Dec 7, 2019

Yay! Huge Success.

TL;DR Fix:

In the go application in

  • internal > player.go
    • Added "net/url" to imports
    • In line 344 exchanged
      track, err := p.loadTrack(node, ident)
      with
      track, err := p.loadTrack(node, url.PathEscape(ident))

It's working flawlessly now.

@zekroTJA The bot is the only discord soundbot working for me. The source code is really exceptionally clean and very well documented. A really huge effort to interconnect all the parts. I aspire to get to that level with rust or java, especially relating to oauth and websockets some day.
I think where this project could improve is an interactive setup application and if you think that is feasible and you haven't done anything for that, I might volunteer for that.

A rather boring blurb of how I found the culprit codeline:

  • Cloned Lavalink and started debugging with IntelliJ with my custom application.yaml
    • Found out lavalink.server > io >WebsocketHandlers.kt > fun play(...) isn't called which
      should be called by lavalink.server > io > SocketServer.kt > handleTextMessageSafe(...)
      where websocket messages are received by the yuri go application.
  • So I debugged the go app with vscode to see where the "play" websocket message is issued
    • It's issued in the gavalink-lib in
      vendor > github.com > zekroTJA > gavalink > player.go > func PlayAt(...) which in turn
      is initially called by the main go app in
      internal > player > player.go > func Play(...): line 360
    • When debugging the main Playfunc I found out that line 360 isn't reached
    • Instead the "unexpected JSON" error is catched in line 344 from p.loadTrack() which eventually
      is getting the error from gavalink > node.go in in
      func LoadTracks > line 184: resp, err := http.DefaultClient.Do(req)
      where the loadtracks Rest-endpoint "loadtracks" in AudioLoaderRestHandler.java
      is called. The problem is the Rest-endpoint isn't called at all in lavalink
    • I figured out that http.DefaultClient.Do(req) requests
      "http://localhost:2333/loadtracks?identifier=/path/to/track"
      and /path/to/track contained spaces which weren't encoded.
      That was it!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants