-
Notifications
You must be signed in to change notification settings - Fork 4
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 Server-Sent Events #16
Open
MrToirol
wants to merge
8
commits into
EarthMC:master
Choose a base branch
from
MrToirol:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5c98183
Add SSE
MrToirol 081b2d6
Add more SSE events
MrToirol 5b6fc54
Final tweaks, remove debug broadcast and add docs
MrToirol b85e9ee
Merge branch 'EarthMC:master' into master
MrToirol e1adb3c
Merge branch 'EarthMC:master' into master
MrToirol cb7c6a7
Improve SSEs (now async) and docs
MrToirol 95f9c1a
Move SSE endpoint to /v3/events
MrToirol a76fa22
Update docs
MrToirol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,290 @@ | ||
# Server-Sent Events (SSE) Endpoint | ||
Accessed at https://api.earthmc.net/v3/events | ||
|
||
> **Server-Sent Events** (SSEs) are a simple, one-way communication method where a server can push real-time updates to clients over HTTP. Unlike WebSockets, SSEs use a persistent HTTP connection, making them ideal for continuous data streams, such as live notifications. | ||
> [[MDN Reference]](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events) | ||
|
||
You can easily connect to the event stream from your terminal using | ||
|
||
````bash | ||
curl -H "Accept:text/event-steam" "https://api.earthmc.net/events" | ||
```` | ||
|
||
If the connection was successful, you will receive a `open` event from the server. | ||
|
||
--- | ||
|
||
### Example usage (in JavaScript) | ||
Use the endpoint as an [EventSource](https://developer.mozilla.org/en-US/docs/Web/API/EventSource) to receive live events. | ||
```javascript | ||
const sse = new EventSource('https://api.earthmc.net/events'); | ||
|
||
/* | ||
* This will listen only for events | ||
* similar to the following: | ||
* | ||
* event: NewNation | ||
* data: Event data (see below) | ||
*/ | ||
|
||
sse.addEventListener('NewNation', (e) => { | ||
console.log(e.data); | ||
}); | ||
``` | ||
<br>Example `NewNation` event | ||
```json5 | ||
{ | ||
"event": "NewNation", | ||
|
||
"data": { | ||
"nation": { | ||
"name": "Egypt", | ||
"uuid": "e82b84fb-d3fd-4065-a43b-013d53416162" | ||
}, | ||
|
||
"king": { | ||
"name": "Lumpeeh", | ||
"uuid": "a03f71f9-625e-419f-9d16-0e5ab50414e4" | ||
}, | ||
|
||
"timestamp": "1651592417137" | ||
} | ||
} | ||
``` | ||
|
||
--- | ||
|
||
### Event data | ||
Below is a list of all the events and the JSON structure of their `data` field. | ||
<br>*(Each `data` object additionally carries a UNIX timestamp)* | ||
|
||
**Player Connections** | ||
- PlayerJoin (aurora) | ||
```yaml | ||
{ | ||
player: { | ||
name: str | ||
uuid: str | ||
} | ||
} | ||
``` | ||
- PlayerQuit (aurora) | ||
```yaml | ||
{ | ||
player: { | ||
name: str | ||
uuid: str | ||
} | ||
} | ||
``` | ||
|
||
<br>**Newday** | ||
|
||
- NewDay | ||
```yaml | ||
{ | ||
fallenTowns: str[] // Names | ||
fallenNations: str[] // Names | ||
} | ||
``` | ||
|
||
<br>**Nation** | ||
|
||
- NewNation | ||
```yaml | ||
{ | ||
nation: { | ||
name: str | ||
uuid: str | ||
} | ||
king: { | ||
name: str | ||
uuid: str | ||
} | ||
} | ||
``` | ||
- DeleteNation | ||
```yaml | ||
{ | ||
nation: { | ||
name: str | ||
uuid: str | ||
} | ||
king: { | ||
name: str | ||
uuid: str | ||
} | ||
} | ||
``` | ||
- RenameNation | ||
```yaml | ||
{ | ||
nation: { | ||
name: str | ||
uuid: str | ||
} | ||
oldName: str | ||
} | ||
``` | ||
- NationKingChange | ||
```yaml | ||
{ | ||
nation: { | ||
name: str | ||
uuid: str | ||
} | ||
newKing: { | ||
name: str | ||
uuid: str | ||
} | ||
oldKing: { | ||
name: str | ||
uuid: str | ||
} | ||
isCapitalChange: bool | ||
|
||
# if isCapitalChange is true: | ||
newCapital: { | ||
name: str | ||
uuid: str | ||
} | ||
oldCapital: { | ||
name: str | ||
uuid: str | ||
} | ||
} | ||
``` | ||
- NationAddTown | ||
```yaml | ||
{ | ||
nation: { | ||
name: str | ||
uuid: str | ||
} | ||
town: { | ||
name: str | ||
uuid: str | ||
} | ||
} | ||
``` | ||
- NationRemoveTown | ||
```yaml | ||
{ | ||
nation: { | ||
name: str | ||
uuid: str | ||
} | ||
town: { | ||
name: str | ||
uuid: str | ||
} | ||
} | ||
``` | ||
|
||
<br>**Town** | ||
|
||
- NewTown | ||
```yaml | ||
{ | ||
town: { | ||
name: str | ||
uuid: str | ||
} | ||
mayor: { | ||
name: str | ||
uuid: str | ||
} | ||
} | ||
``` | ||
- DeleteTown | ||
```yaml | ||
{ | ||
town: { | ||
name: str | ||
uuid: str | ||
} | ||
mayor: { | ||
name: str | ||
uuid: str | ||
} | ||
} | ||
``` | ||
- RenameTown | ||
```yaml | ||
{ | ||
town: { | ||
name: str | ||
uuid: str | ||
} | ||
oldName: str | ||
} | ||
``` | ||
- TownMayorChanged | ||
```yaml | ||
{ | ||
town: { | ||
name: str | ||
uuid: str | ||
} | ||
newMayor: { | ||
name: str | ||
uuid: str | ||
} | ||
oldMayor: { | ||
name: str | ||
uuid: str | ||
} | ||
} | ||
``` | ||
- TownRuined | ||
```yaml | ||
{ | ||
town: { | ||
name: str | ||
uuid: str | ||
} | ||
oldMayor: { | ||
name: str | ||
uuid: str | ||
} | ||
} | ||
``` | ||
- TownReclaimed | ||
```yaml | ||
{ | ||
town: { | ||
name: str | ||
uuid: str | ||
} | ||
newMayor: { | ||
name: str | ||
uuid: str | ||
} | ||
} | ||
``` | ||
- TownAddResident | ||
```yaml | ||
{ | ||
town: { | ||
name: str | ||
uuid: str | ||
} | ||
resident: { | ||
name: str | ||
uuid: str | ||
} | ||
} | ||
``` | ||
- TownRemoveResident | ||
```yaml | ||
{ | ||
town: { | ||
name: str | ||
uuid: str | ||
} | ||
resident: { | ||
name: str | ||
uuid: str | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/net/earthmc/emcapi/listeners/PlayerConnectionListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package net.earthmc.emcapi.listeners; | ||
|
||
import com.google.gson.JsonObject; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.player.PlayerJoinEvent; | ||
import org.bukkit.event.player.PlayerQuitEvent; | ||
|
||
import net.earthmc.emcapi.util.EndpointUtils; | ||
import net.earthmc.emcapi.manager.SSEManager; | ||
|
||
|
||
public class PlayerConnectionListener implements Listener { | ||
|
||
@EventHandler | ||
public void onPlayerJoin(PlayerJoinEvent event) { | ||
Player player = event.getPlayer(); | ||
JsonObject message = new JsonObject(); | ||
message.add("player", EndpointUtils.generateNameUUIDJsonObject(player.getName(), player.getUniqueId())); | ||
SSEManager.broadcastMessage("PlayerJoin", message); | ||
} | ||
|
||
@EventHandler | ||
public void onPlayerQuit(PlayerQuitEvent event) { | ||
Player player = event.getPlayer(); | ||
JsonObject message = new JsonObject(); | ||
message.add("player", EndpointUtils.generateNameUUIDJsonObject(player.getName(), player.getUniqueId())); | ||
SSEManager.broadcastMessage("PlayerQuit", message); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that player join/quit events are too intrusive, this class should be removed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I shadow Warriorrrr's opinion, those events are intrusive though, it can prove helpful to have it...