Initial
This commit is contained in:
17
server/src/main/java/gltronic/tronio/TronIoApplication.java
Normal file
17
server/src/main/java/gltronic/tronio/TronIoApplication.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package gltronic.tronio;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackages = "gltronic.voozik")
|
||||
@PropertySource("classpath:application.properties")
|
||||
public class TronIoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TronIoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package gltronic.tronio.business;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.springframework.web.socket.TextMessage;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
|
||||
public interface IRoomManager {
|
||||
public void login(WebSocketSession session, String name) throws InterruptedException, IOException;
|
||||
public void leave(WebSocketSession session);
|
||||
public void createRoom(WebSocketSession session) throws InterruptedException, IOException;
|
||||
public void connectRoom(WebSocketSession session, String roomName) throws InterruptedException, IOException;
|
||||
public void followRTC(WebSocketSession session, TextMessage message) throws InterruptedException, IOException;
|
||||
public void sendMessage(WebSocketSession session, String type, String message) throws InterruptedException, IOException;
|
||||
public void sendServerInfos(WebSocketSession session) throws InterruptedException, IOException;
|
||||
}
|
||||
117
server/src/main/java/gltronic/tronio/business/RoomManager.java
Normal file
117
server/src/main/java/gltronic/tronio/business/RoomManager.java
Normal file
@@ -0,0 +1,117 @@
|
||||
package gltronic.tronio.business;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Random;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.socket.TextMessage;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
|
||||
import gltronic.tronio.model.BiMap;
|
||||
|
||||
@Service
|
||||
public class RoomManager implements IRoomManager{
|
||||
private volatile BiMap<String, WebSocketSession> users = new BiMap<String, WebSocketSession>();
|
||||
private volatile BiMap<String, String> rooms = new BiMap<String, String>();
|
||||
|
||||
public void login(WebSocketSession session, String name) throws InterruptedException, IOException {
|
||||
if (name == null) {
|
||||
sendMessage(session, "error", "bad command command");
|
||||
return;
|
||||
}
|
||||
if (users.containsKey(name)) {
|
||||
sendMessage(session, "login", "false");
|
||||
return;
|
||||
}
|
||||
|
||||
users.put(name, session);
|
||||
sendMessage(session, "login", "true");
|
||||
sendServerInfos(session);
|
||||
|
||||
System.err.println("[ROOM] Logged "+name);
|
||||
}
|
||||
|
||||
public void leave(WebSocketSession session) {
|
||||
String name = users.getKey(session);
|
||||
users.removeValue(session);
|
||||
if(rooms.containsValue(name)) rooms.removeValue(name);
|
||||
}
|
||||
|
||||
public void createRoom(WebSocketSession session) throws InterruptedException, IOException {
|
||||
if (!users.containsValue(session)) {
|
||||
sendMessage(session, "error", "need login");
|
||||
return;
|
||||
}
|
||||
String userName = users.getKey(session);
|
||||
if (rooms.containsKey(userName)) {
|
||||
sendMessage(session, "error", "no multiple room");
|
||||
return;
|
||||
}
|
||||
|
||||
Random random = new Random();
|
||||
String roomName = Integer.toString(random.nextInt(9999));
|
||||
|
||||
while (roomName.length() < 4) roomName += 0 + roomName;
|
||||
|
||||
rooms.put(roomName, userName);
|
||||
sendMessage(session, "createRoom", roomName);
|
||||
|
||||
System.err.println("[ROOM] Created room "+roomName+" by "+userName);
|
||||
}
|
||||
|
||||
public void connectRoom(WebSocketSession session, String roomName) throws InterruptedException, IOException {
|
||||
if (roomName == null) {
|
||||
sendMessage(session, "error", "bad command command");
|
||||
return;
|
||||
}
|
||||
if (!users.containsValue(session)) {
|
||||
sendMessage(session, "error", "need login");
|
||||
return;
|
||||
}
|
||||
if (!rooms.containsKey(roomName)) {
|
||||
sendMessage(session, "error", "no room");
|
||||
return;
|
||||
}
|
||||
|
||||
String roomAdmin = rooms.get(roomName);
|
||||
sendMessage(session, "connectRoom", roomAdmin);
|
||||
|
||||
String userName = users.getKey(session);
|
||||
System.err.println("[ROOM] Connection to room "+roomName+" ("+roomAdmin+") by "+userName);
|
||||
}
|
||||
|
||||
public void followRTC(WebSocketSession session, TextMessage message) throws InterruptedException, IOException {
|
||||
String payload = message.getPayload();
|
||||
JSONObject jsonObject = new JSONObject(payload);
|
||||
String target = (String) jsonObject.get("target");
|
||||
|
||||
if (target == null) {
|
||||
sendMessage(session, "error", "no target");
|
||||
return;
|
||||
}
|
||||
|
||||
WebSocketSession targetSession = users.get(target);
|
||||
|
||||
if (targetSession == null) {
|
||||
sendMessage(session, "error", "unknow target");
|
||||
return;
|
||||
}
|
||||
|
||||
System.err.println("[ROOM] Foward RTC message");
|
||||
followMessage(targetSession, message);
|
||||
}
|
||||
|
||||
public void followMessage (WebSocketSession session, TextMessage message) throws IOException {
|
||||
session.sendMessage(message);
|
||||
}
|
||||
|
||||
public void sendMessage(WebSocketSession session, String type, String message) throws InterruptedException, IOException {
|
||||
session.sendMessage(new TextMessage("{\"type\":\""+type+"\",\"message\":\""+message+"\"}"));
|
||||
}
|
||||
|
||||
public void sendServerInfos(WebSocketSession session) throws InterruptedException, IOException {
|
||||
session.sendMessage(new TextMessage("{\"type\":\"serverInfos\",\"userCount\":\""+users.size()+"\",\"roomCount\":\""+rooms.size()+"\"}"));
|
||||
}
|
||||
|
||||
}
|
||||
46
server/src/main/java/gltronic/tronio/model/BiMap.java
Normal file
46
server/src/main/java/gltronic/tronio/model/BiMap.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package gltronic.tronio.model;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class BiMap<K, V> {
|
||||
HashMap<K,V> map = new HashMap<K, V>();
|
||||
HashMap<V,K> inversedMap = new HashMap<V, K>();
|
||||
|
||||
public void put(K k, V v) {
|
||||
map.put(k, v);
|
||||
inversedMap.put(v, k);
|
||||
}
|
||||
|
||||
public V get(K k) {
|
||||
return map.get(k);
|
||||
}
|
||||
|
||||
public K getKey(V v) {
|
||||
return inversedMap.get(v);
|
||||
}
|
||||
|
||||
public boolean containsKey(K k) {
|
||||
return map.containsKey(k);
|
||||
}
|
||||
|
||||
public boolean containsValue(V v) {
|
||||
return map.containsValue(v);
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return map.size();
|
||||
}
|
||||
|
||||
public void removeKey(K k) {
|
||||
V v = map.get(k);
|
||||
map.remove(k);
|
||||
inversedMap.remove(v);
|
||||
}
|
||||
|
||||
public void removeValue(V v) {
|
||||
K k = inversedMap.get(v);
|
||||
inversedMap.remove(v);
|
||||
map.remove(k);
|
||||
}
|
||||
|
||||
}
|
||||
74
server/src/main/java/gltronic/tronio/web/SocketHandler.java
Normal file
74
server/src/main/java/gltronic/tronio/web/SocketHandler.java
Normal file
@@ -0,0 +1,74 @@
|
||||
package gltronic.tronio.web;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.socket.CloseStatus;
|
||||
import org.springframework.web.socket.TextMessage;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
import org.springframework.web.socket.handler.TextWebSocketHandler;
|
||||
|
||||
import gltronic.tronio.business.IRoomManager;
|
||||
|
||||
@Component
|
||||
public class SocketHandler extends TextWebSocketHandler {
|
||||
private volatile List<WebSocketSession> sessions = new CopyOnWriteArrayList<>();
|
||||
|
||||
@Autowired
|
||||
IRoomManager roomManager;
|
||||
|
||||
@Override
|
||||
public void handleTextMessage(WebSocketSession session, TextMessage message) throws InterruptedException, IOException {
|
||||
System.err.println("[WS] message :" + message.getPayload());
|
||||
|
||||
String payload = message.getPayload();
|
||||
JSONObject jsonObject = new JSONObject(payload);
|
||||
|
||||
String type = (String) jsonObject.get("type");
|
||||
switch (type) {
|
||||
case "serverInfos":
|
||||
roomManager.sendServerInfos(session);
|
||||
break;
|
||||
case "login":
|
||||
roomManager.login(session, (String) jsonObject.get("name"));
|
||||
break;
|
||||
case "createRoom":
|
||||
roomManager.createRoom(session);
|
||||
break;
|
||||
case "connectRoom":
|
||||
roomManager.connectRoom(session, (String) jsonObject.get("name"));
|
||||
break;
|
||||
case "leave":
|
||||
roomManager.leave(session);
|
||||
break;
|
||||
case "offer":
|
||||
case "answer":
|
||||
case "candidate":
|
||||
roomManager.followRTC(session, message);
|
||||
break;
|
||||
case "search":
|
||||
break;
|
||||
case "alive":
|
||||
break;
|
||||
default:
|
||||
roomManager.sendMessage(session, "error", "unknow command");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
|
||||
System.err.println("[WS] new connection " + this);
|
||||
sessions.add(session);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus){
|
||||
System.err.println("[WS] connection closed");
|
||||
sessions.remove(session);
|
||||
roomManager.leave(session);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package gltronic.tronio.web;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.socket.config.annotation.EnableWebSocket;
|
||||
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
|
||||
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSocket
|
||||
public class WebSocketConfiguration implements WebSocketConfigurer {
|
||||
|
||||
@Autowired
|
||||
private SocketHandler socketHandler;
|
||||
|
||||
@Override
|
||||
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
|
||||
registry.addHandler(socketHandler, "/socket").setAllowedOrigins("*");
|
||||
}
|
||||
}
|
||||
1
server/src/main/resources/application.properties
Normal file
1
server/src/main/resources/application.properties
Normal file
@@ -0,0 +1 @@
|
||||
server.port=8181
|
||||
@@ -0,0 +1,13 @@
|
||||
package gltronic.voozik;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class DemoApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user