Added socket
This commit is contained in:
parent
56de051229
commit
08d14e1122
6 changed files with 163 additions and 6 deletions
|
@ -1,5 +1,6 @@
|
|||
package me.mctp;
|
||||
|
||||
import me.mctp.socket.Client;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.event.Listener;
|
||||
|
@ -8,8 +9,8 @@ import org.bukkit.plugin.java.JavaPlugin;
|
|||
|
||||
public class Main extends JavaPlugin implements Listener {
|
||||
|
||||
public static Plugin pl;
|
||||
private static Main plugin;
|
||||
private static Plugin pl;
|
||||
private static Client client;
|
||||
|
||||
public static String prefix = (ChatColor.GOLD + "[" + ChatColor.YELLOW + "MCTP" + ChatColor.GOLD + "] " + ChatColor.GRAY);
|
||||
|
||||
|
@ -18,8 +19,10 @@ public class Main extends JavaPlugin implements Listener {
|
|||
|
||||
Bukkit.getLogger().info(prefix + "loading...");
|
||||
|
||||
pl = (Plugin) this;
|
||||
plugin = this;
|
||||
pl = this;
|
||||
|
||||
client = new Client("SOCKET URL");
|
||||
client.connect();
|
||||
|
||||
getCommand("mctpaudio").setExecutor(new MCTPAudioCmd());
|
||||
|
||||
|
@ -35,8 +38,11 @@ public class Main extends JavaPlugin implements Listener {
|
|||
Bukkit.getLogger().info(prefix + "successfully disabled!");
|
||||
}
|
||||
|
||||
public static Main getPlugin() {
|
||||
return plugin;
|
||||
public static Plugin getPlugin() {
|
||||
return pl;
|
||||
}
|
||||
|
||||
public static Client getClient() {
|
||||
return client;
|
||||
}
|
||||
}
|
||||
|
|
115
src/me/mctp/socket/Client.java
Normal file
115
src/me/mctp/socket/Client.java
Normal file
|
@ -0,0 +1,115 @@
|
|||
package me.mctp.socket;
|
||||
|
||||
import me.mctp.Main;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.java_websocket.client.WebSocketClient;
|
||||
import org.java_websocket.handshake.ServerHandshake;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
public class Client {
|
||||
private String url;
|
||||
private int taskID = 0;
|
||||
private int controlID = 0;
|
||||
private WebSocketClient wsc;
|
||||
private boolean connected = false;
|
||||
|
||||
public Client(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public void connect() {
|
||||
if (!this.connected) {
|
||||
this.connected = true;
|
||||
this.controlID = Bukkit.getScheduler().runTaskTimer(Main.getPlugin(), () -> {
|
||||
if (!this.connected) {
|
||||
Bukkit.getScheduler().cancelTask(this.controlID);
|
||||
this.controlID = 0;
|
||||
} else if (this.wsc == null || !this.wsc.isOpen()) {
|
||||
if (this.wsc != null) {
|
||||
this.wsc.closeConnection(404, "Disconnected from socket");
|
||||
this.wsc = null;
|
||||
}
|
||||
|
||||
this.connect();
|
||||
}
|
||||
}, 600L, 600L).getTaskId();
|
||||
}
|
||||
|
||||
if (this.url != null && this.wsc == null) {
|
||||
try {
|
||||
URI uri = new URI(this.url + "?type=SERVER&");
|
||||
|
||||
this.wsc = new WebSocketClient(uri) {
|
||||
@Override
|
||||
public void onOpen(ServerHandshake serverHandshake) {}
|
||||
|
||||
@Override
|
||||
public void onMessage(String s) {
|
||||
//TODO Add message handler
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClose(int i, String s, boolean b) {
|
||||
Client.this.wsc = null;
|
||||
Bukkit.getScheduler().cancelTask(Client.this.taskID);
|
||||
Client.this.taskID = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
};
|
||||
|
||||
this.wsc.connect();
|
||||
|
||||
if (this.taskID == 0) {
|
||||
this.taskID = Bukkit.getScheduler().runTaskTimerAsynchronously(Main.getPlugin(), () -> {
|
||||
if (Client.this.wsc != null && Client.this.wsc.isOpen()) {
|
||||
Client.this.wsc.send("__PING__");
|
||||
} else {
|
||||
if (Client.this.wsc != null) {
|
||||
Client.this.wsc.closeConnection(404, "Disconnected from socket");
|
||||
Client.this.wsc = null;
|
||||
}
|
||||
|
||||
Bukkit.getScheduler().cancelTask(Client.this.taskID);
|
||||
Client.this.taskID = 0;
|
||||
}
|
||||
}, 200L, 200L).getTaskId();
|
||||
}
|
||||
} catch (URISyntaxException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void disconnect() {
|
||||
if (this.wsc != null) {
|
||||
this.wsc.closeConnection(404, "Disconnected from socket");
|
||||
}
|
||||
|
||||
this.wsc = null;
|
||||
|
||||
if (this.taskID != 0) {
|
||||
Bukkit.getScheduler().cancelTask(this.taskID);
|
||||
this.taskID = 0;
|
||||
}
|
||||
|
||||
this.connected = false;
|
||||
|
||||
if (this.controlID != 0) {
|
||||
Bukkit.getScheduler().cancelTask(this.controlID);
|
||||
this.controlID = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void sendData(JSONObject object) {
|
||||
if (this.wsc != null && this.wsc.isOpen() && object != null && object.toJSONString() != null) {
|
||||
this.wsc.send(object.toJSONString());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue