Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
freyacodes committed Jan 29, 2018
2 parents 6c2afbf + b8dd3c8 commit 610dab3
Show file tree
Hide file tree
Showing 25 changed files with 559 additions and 709 deletions.
91 changes: 27 additions & 64 deletions IMPLEMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,31 @@ How to write your own client. The Java client will serve as an example implement
* Hybi 10
* Hixie 76
* Hixie 75

## Changes v1.3 -> v2.0
With the release of v2.0 many unnecessary ops were removed:

* `connect`
* `disconnect`
* `validationRes`
* `isConnectedRes`
* `validationReq`
* `isConnectedReq`
* `sendWS`

With Lavalink 1.x the server had the responsibility of handling Discord VOICE_SERVER_UPDATEs as well as its own internal ratelimiting.
This remote handling makes things unnecessarily complicated and adds a lot og points where things could go wrong.
One problem we noticed is that since JDAA is unaware of ratelimits on the bot's gateway connection, it would keep adding
to the ratelimit queue to the gateway. With this update this is now the responsibility of the Lavalink client or the
Discord client.

A voice connection is now initiated by forwarding a `voiceUpdate` (VOICE_SERVER_UPDATE) to the server. When you want to
disconnect or move to a different voice channel you must send Discord a new VOICE_STATE_UPDATE. If you want to move your
connection to a new Lavalink server you can simply send the VOICE_SERVER_UPDATE to the new node, and the other node
will be disconnected by Discord.

Depending on your Discord library, it may be possible to take advantage of the library's OP 4 handling. For instance,
the JDA client takes advantage of JDA's websocket write thread to send OP 4s for connects, disconnects and reconnects.

## Protocol
### Opening a connection
Expand All @@ -21,16 +46,7 @@ User-Id: The user id of the bot you are playing music with
```

### Outgoing messages
Make the server queue a voice connection
```json
{
"op": "connect",
"guildId": "...",
"channelId": "..."
}
```

Provide an intercepted voice server update
Provide an intercepted voice server update. This causes the server to connect to the voice channel
```json
{
"op": "voiceUpdate",
Expand All @@ -40,33 +56,6 @@ Provide an intercepted voice server update
}
```

Close a voice connection
```json
{
"op": "disconnect",
"guildId": "123"
}
```

Response to `validationReq`. `channelId` is omitted if the request does not display the channel id.
```json
{
"op": "validationRes",
"guildId": "...",
"channelId": "...",
"valid": true
}
```

Response to `isConnectedRes`.
```json
{
"op": "isConnectedRes",
"shardId": 1337,
"connected": true
}
```

Cause the player to play a track.
`startTime` is an optional setting that determines the number of milliseconds to offset the track by. Defaults to 0.
`endTime` is an optional setting that determines at the number of milliseconds at which point the track should stop playing. Helpful if you only want to play a snippet of a bigger track. By default the track plays until it's end as per the encoded data.
Expand Down Expand Up @@ -117,35 +106,9 @@ Set player volume. Volume may range from 0 to 150. 100 is default.

### Incoming messages
See
[LavalinkSocket.java](https://github.com/Frederikam/Lavalink/blob/91bc0ef4dab6ca5d5efcba12203ee4054bb55ae9/LavalinkClient/src/main/java/lavalink/client/io/LavalinkSocket.java)
[LavalinkSocket.java](https://github.com/Frederikam/Lavalink/blob/dev/LavalinkClient/src/main/java/lavalink/client/io/LavalinkSocket.java)
for client implementation

Incoming message to forward to mainWS
```json
{
"op": "sendWS",
"shardId": 1337,
"message": "..."
}
```

Request to check if the VC or Guild exists, and that we have access to the VC. Note that the channelId may be omitted, in which case you should only check if we are in the guild.
```json
{
"op": "validationReq",
"guildId": "...",
"channelId": "..."
}
```

Request to check if a shard's mainWS is connected
```json
{
"op": "isConnectedReq",
"shardId": 1337
}
```

Position information about a player. Includes unix timestamp.
```json
{
Expand Down
3 changes: 2 additions & 1 deletion LavalinkClient/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
/logs/
/target/
/dependency-reduced-pom.xml
build/*
build/*
/out/
9 changes: 5 additions & 4 deletions LavalinkClient/build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
description = 'JDA based client for the Lavalink-Server'
version System.getenv('dev') == 'true' ? '-SNAPSHOT' : '1.3'
version System.getenv('dev') == 'true' ? '-SNAPSHOT' : '2.0'
ext {
moduleName = 'Lavalink-Client'
}
dependencies {
compile group: 'com.sedmelluq', name: 'lavaplayer', version: '1.2.45'
compile group: 'org.java-websocket', name: 'Java-WebSocket', version: '1.3.4'
compile group: 'org.java-websocket', name: 'Java-WebSocket', version: '1.3.7'
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
compile group: 'org.json', name: 'json', version: '20170516'
compile group: 'net.dv8tion', name: 'JDA', version: '3.3.1_307'
compile group: 'org.json', name: 'json', version: '20171018'
compile group: 'net.dv8tion', name: 'JDA', version: '3.5.0_328'
compileOnly group: 'io.prometheus', name: 'simpleclient', version: '0.1.0'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.0-M4'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.0.0-M4'
testCompile group: 'org.junit.platform', name: 'junit-platform-launcher', version: '1.0.0-M4'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public class LavalinkUtil {

static {
PLAYER_MANAGER = new DefaultAudioPlayerManager();
PLAYER_MANAGER.enableGcMonitoring();

/* These are only to encode/decode messages */
PLAYER_MANAGER.registerSourceManager(new YoutubeAudioSourceManager());
Expand All @@ -65,7 +64,7 @@ public static AudioTrack toAudioTrack(String message) throws IOException {
public static String toMessage(AudioTrack track) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PLAYER_MANAGER.encodeTrack(new MessageOutput(baos), track);
return new String(Base64.encodeBytesToBytes(baos.toByteArray()));
return Base64.encodeBytes(baos.toByteArray());
}

public static int getShardFromSnowflake(String snowflake, int numShards) {
Expand Down
Loading

0 comments on commit 610dab3

Please sign in to comment.