SEE for groups messages
This commit is contained in:
parent
7b40c6edea
commit
5c43347710
5 changed files with 133 additions and 2 deletions
Binary file not shown.
|
@ -46,6 +46,8 @@ public class Configuration {
|
|||
private String jwt_key;
|
||||
|
||||
private String sql_folder;
|
||||
|
||||
private int event_chapter;
|
||||
|
||||
private File _file;
|
||||
|
||||
|
@ -228,4 +230,8 @@ public class Configuration {
|
|||
public String getSqlFolder(){
|
||||
return this.sql_folder;
|
||||
}
|
||||
|
||||
public int getEventChapter(){
|
||||
return this.event_chapter;
|
||||
}
|
||||
}
|
|
@ -93,7 +93,6 @@ public class Main{
|
|||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
context.response(200);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
initRoutes(router, repo, config);
|
||||
|
@ -141,6 +140,25 @@ public class Main{
|
|||
}
|
||||
}).start();
|
||||
|
||||
new Thread(new Runnable(){
|
||||
public void run(){
|
||||
Key key = new Key();
|
||||
|
||||
Locker<Throwable> locker = router.getExceptionLogger();
|
||||
|
||||
locker.init(key);
|
||||
try {
|
||||
while(true){
|
||||
locker.lock(key);
|
||||
locker.getValue(key).printStackTrace();
|
||||
}
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
locker.remove(key);
|
||||
}
|
||||
}).start();
|
||||
|
||||
if(config.useSsl()) router.configureSSL(config.getSslKeystore(), config.getSslKeystorePasswd());
|
||||
router.listen(config.getTcpPort(), config.useSsl());
|
||||
}
|
||||
|
@ -150,7 +168,8 @@ public class Main{
|
|||
.of(repo, router, config, new RouteExtracter(router), config.getMail())
|
||||
.of("waitting", new HashMap<>())
|
||||
.of("leaderboard", new Locker<>())
|
||||
.of("groups", new Locker<>()));
|
||||
.of("groups", new Locker<>())
|
||||
.of("groupMessages", new Locker<>()));
|
||||
|
||||
// Bot bot = new Bot(config, repo, groupLock);
|
||||
// bot.start();
|
||||
|
|
75
src/dev/peerat/backend/routes/EventSSE.java
Normal file
75
src/dev/peerat/backend/routes/EventSSE.java
Normal file
|
@ -0,0 +1,75 @@
|
|||
package dev.peerat.backend.routes;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.model.Group;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.repository.DatabaseGroupRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.Injection;
|
||||
import dev.peerat.framework.Locker;
|
||||
import dev.peerat.framework.Locker.Key;
|
||||
import dev.peerat.framework.Route;
|
||||
|
||||
public class EventSSE{
|
||||
|
||||
private Locker<GroupMessage> locker;
|
||||
private DatabaseGroupRepository repo;
|
||||
private int eventChapter;
|
||||
|
||||
public EventSSE(@Injection("groupMessages") Locker<GroupMessage> locker, DatabaseRepository repo, Configuration config){
|
||||
this.locker = locker;
|
||||
this.repo = repo.getGroupRepository();
|
||||
this.eventChapter = config.getEventChapter();
|
||||
}
|
||||
|
||||
@Route(path = "^/group/event/$", needLogin = true)
|
||||
public void connect(Context context, HttpWriter writer) throws Exception {
|
||||
Group group = repo.getPlayerGroup(context.<PeerAtUser>getUser().getId(), this.eventChapter);
|
||||
if(group == null){
|
||||
context.response(401);
|
||||
return;
|
||||
}
|
||||
context.response(200, "Content-Type: text/event-stream");
|
||||
|
||||
Key key = new Key();
|
||||
|
||||
locker.init(key);
|
||||
try {
|
||||
while(true){
|
||||
GroupMessage message = locker.getValue(key);
|
||||
if(message.getGroup() == null || message.getGroup().equals(group.getName())){
|
||||
writer.write("data: "+message.getMessage()+"\n\n");
|
||||
writer.flush();
|
||||
}
|
||||
locker.lock(key);
|
||||
}
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
locker.remove(key);
|
||||
}
|
||||
|
||||
public static class GroupMessage{
|
||||
|
||||
private String group;
|
||||
private String message;
|
||||
|
||||
public GroupMessage(String group, String message){
|
||||
this.group = group;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getGroup(){
|
||||
return this.group;
|
||||
}
|
||||
|
||||
public String getMessage(){
|
||||
return this.message;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
31
src/dev/peerat/backend/routes/admins/GroupController.java
Normal file
31
src/dev/peerat/backend/routes/admins/GroupController.java
Normal file
|
@ -0,0 +1,31 @@
|
|||
package dev.peerat.backend.routes.admins;
|
||||
|
||||
import dev.peerat.backend.routes.EventSSE.GroupMessage;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.Injection;
|
||||
import dev.peerat.framework.Locker;
|
||||
import dev.peerat.framework.Route;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
|
||||
public class GroupController {
|
||||
|
||||
private Locker<GroupMessage> locker;
|
||||
|
||||
public GroupController(@Injection("groupMessages") Locker<GroupMessage> locker){
|
||||
this.locker = locker;
|
||||
}
|
||||
|
||||
@Route(path = "^/admin/event/$", needLogin = true)
|
||||
public void send(Context context, HttpReader reader) throws Exception{
|
||||
JsonMap json = reader.readJson();
|
||||
if(!json.has("message")){
|
||||
context.response(400);
|
||||
return;
|
||||
}
|
||||
|
||||
locker.setValue(new GroupMessage(json.get("group"), json.get("message")));
|
||||
context.response(200);
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue