多人对战ws框架

This commit is contained in:
石皮幼鸟 2024-06-13 19:58:37 +08:00
parent 8fc08dad6a
commit b9126ef3ab
7 changed files with 118 additions and 0 deletions

View File

@ -47,6 +47,12 @@
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--springboot-websocket-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<!--springboot-test-->
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@ -0,0 +1,21 @@
package com.example.catchTheLetters.config;
import com.example.catchTheLetters.handler.WebSocketHandler;
import jakarta.annotation.Resource;
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 WebSocketConfig implements WebSocketConfigurer {
@Resource
private WebSocketHandler webSocketHandler;
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(webSocketHandler, "/ws");
}
}

View File

@ -0,0 +1,38 @@
package com.example.catchTheLetters.entity;
import com.example.catchTheLetters.enums.Status;
import com.example.catchTheLetters.model.vo.Letter;
import org.springframework.web.socket.WebSocketSession;
import java.util.ArrayList;
import java.util.List;
public class GameRoom {
private List<WebSocketSession> players = new ArrayList<>();
private Status status = Status.WAITING;
public void addPlayer(WebSocketSession session) {
players.add(session);
}
public void removePlayer(WebSocketSession session) {
players.remove(session);
}
public void startGame() {
status = Status.PLAYING;
}
public void endGame() {
status = Status.END;
}
public void handleInput(WebSocketSession session, String input) {
// 处理玩家输入
}
public Letter generateLetter() {
// 生成字母
return null;
}
}

View File

@ -0,0 +1,5 @@
package com.example.catchTheLetters.enums;
public enum Status {
WAITING, PLAYING, END
}

View File

@ -0,0 +1,15 @@
package com.example.catchTheLetters.handler;
import org.springframework.stereotype.Controller;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
@Controller
public class WebSocketHandler extends TextWebSocketHandler {
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) {
// 处理接收到的消息
}
}

View File

@ -0,0 +1,15 @@
package com.example.catchTheLetters.model.vo;
public class Letter {
// 字母值如果是10则是加血
private String letterVal;
// 下落速度
private float speed = 3f;
// 字母的x坐标
private float x;
// 字母的y坐标
private float y;
}

View File

@ -0,0 +1,18 @@
package com.example.catchTheLetters.service;
import com.example.catchTheLetters.model.vo.Letter;
import org.springframework.web.socket.WebSocketSession;
public interface RoomService {
void addPlayer(WebSocketSession session);
void removePlayer(WebSocketSession session);
void startGame();
void endGame();
void handleInput(WebSocketSession session, String input);
Letter generateLetter();
}