mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-06-26 03:14:47 +00:00
update
This commit is contained in:
parent
f9b5038f1c
commit
0b9cf0ea9b
@ -21,8 +21,9 @@ import { SpriteFrame } from "cc";
|
|||||||
import Loading from "../../extensions/ngame/assets/ngame/util/Loading";
|
import Loading from "../../extensions/ngame/assets/ngame/util/Loading";
|
||||||
import { TB, Tables } from "../resources/config/data/schema";
|
import { TB, Tables } from "../resources/config/data/schema";
|
||||||
import { JsonAsset } from "cc";
|
import { JsonAsset } from "cc";
|
||||||
|
import { GAction } from "./GActionEnum";
|
||||||
|
|
||||||
let IP = "kyu.jisol.cn";
|
let IP = "localhost";
|
||||||
|
|
||||||
//重写UI
|
//重写UI
|
||||||
class JNGLayer extends JNLayer{
|
class JNGLayer extends JNLayer{
|
||||||
@ -32,8 +33,16 @@ class JNGLayer extends JNLayer{
|
|||||||
|
|
||||||
//重写Socket
|
//重写Socket
|
||||||
class JNGSocket extends JNSocket{
|
class JNGSocket extends JNSocket{
|
||||||
|
async onInit() {
|
||||||
|
this.on(GAction.TOKEN_EXPIRED,this.onTokenExpired.bind(this));
|
||||||
|
await super.onInit();
|
||||||
|
}
|
||||||
public url() {
|
public url() {
|
||||||
return `ws://${IP}:8080/websocket`;
|
return `ws://${IP}:8080/websocket/b113a866e5664f46b386c12c985d394a`;
|
||||||
|
}
|
||||||
|
//Token失效
|
||||||
|
onTokenExpired(){
|
||||||
|
console.log("onTokenExpired");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
5
JisolGameCocos/assets/script/GActionEnum.ts
Normal file
5
JisolGameCocos/assets/script/GActionEnum.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
export enum GAction {
|
||||||
|
|
||||||
|
TOKEN_EXPIRED = 1001, //Token过期
|
||||||
|
|
||||||
|
}
|
9
JisolGameCocos/assets/script/GActionEnum.ts.meta
Normal file
9
JisolGameCocos/assets/script/GActionEnum.ts.meta
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"ver": "4.0.23",
|
||||||
|
"importer": "typescript",
|
||||||
|
"imported": true,
|
||||||
|
"uuid": "b0d5a426-cf84-4bbb-b06f-533234076520",
|
||||||
|
"files": [],
|
||||||
|
"subMetas": {},
|
||||||
|
"userData": {}
|
||||||
|
}
|
@ -23,11 +23,11 @@ export class Main extends Component {
|
|||||||
{path:"proto/GDemo"}
|
{path:"proto/GDemo"}
|
||||||
]);
|
]);
|
||||||
|
|
||||||
//发生帧同步开始
|
// //发生帧同步开始
|
||||||
app.socket.Send(JNSyncAction.NSyncFrameStart);
|
// app.socket.Send(JNSyncAction.NSyncFrameStart);
|
||||||
|
|
||||||
// 创建世界
|
// // 创建世界
|
||||||
director.getScene().addChild(instantiate(this.WorldPrefab));
|
// director.getScene().addChild(instantiate(this.WorldPrefab));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1 +1 @@
|
|||||||
Subproject commit 6349055e1f9362860a81a92c971d343ef9ab483a
|
Subproject commit 661098aefe9d7e1c6f0d3a4c587b3b15cc27abd8
|
@ -1 +1 @@
|
|||||||
Subproject commit 8951e60a10df0ede0db4e2415f3658daa4790b33
|
Subproject commit a23b0bc24282b113594d777e7db34d03379f07b6
|
@ -0,0 +1,7 @@
|
|||||||
|
package cn.jisol.game.actions;
|
||||||
|
|
||||||
|
public interface GActionEnum {
|
||||||
|
|
||||||
|
int TOKEN_EXPIRED = 1001; //Token过期
|
||||||
|
|
||||||
|
}
|
@ -1,6 +1,9 @@
|
|||||||
package cn.jisol.game.controller;
|
package cn.jisol.game.controller;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.IdUtil;
|
||||||
import cn.hutool.core.util.RandomUtil;
|
import cn.hutool.core.util.RandomUtil;
|
||||||
|
import cn.jisol.game.data.Cache;
|
||||||
|
import cn.jisol.game.dto.LoginDTO;
|
||||||
import cn.jisol.game.entity.User;
|
import cn.jisol.game.entity.User;
|
||||||
import cn.jisol.game.service.UserService;
|
import cn.jisol.game.service.UserService;
|
||||||
import cn.jisol.game.service.impl.UserServiceImpl;
|
import cn.jisol.game.service.impl.UserServiceImpl;
|
||||||
@ -41,7 +44,7 @@ public class UserController {
|
|||||||
@ApiImplicitParams({})
|
@ApiImplicitParams({})
|
||||||
@ApiOperation(value = "登录账号")
|
@ApiOperation(value = "登录账号")
|
||||||
@PostMapping("/login")
|
@PostMapping("/login")
|
||||||
public NewsContext<User> login(@RequestBody User user){
|
public NewsContext<LoginDTO> login(@RequestBody User user){
|
||||||
if(Objects.isNull(user.getUserId()) || Objects.isNull(user.getUserPass())){
|
if(Objects.isNull(user.getUserId()) || Objects.isNull(user.getUserPass())){
|
||||||
return NewsContext.onFail("登录失败");
|
return NewsContext.onFail("登录失败");
|
||||||
}
|
}
|
||||||
@ -49,7 +52,11 @@ public class UserController {
|
|||||||
query.eq(User::getUserId,user.getUserId());
|
query.eq(User::getUserId,user.getUserId());
|
||||||
query.eq(User::getUserPass,user.getUserPass());
|
query.eq(User::getUserPass,user.getUserPass());
|
||||||
if(Objects.nonNull(user = userService.getOne(query))){
|
if(Objects.nonNull(user = userService.getOne(query))){
|
||||||
return NewsContext.onSuccess("登录成功",user);
|
user.setUserPass("");
|
||||||
|
//生成Token
|
||||||
|
String token = IdUtil.simpleUUID();
|
||||||
|
Cache.TOKEN.put(token,user);
|
||||||
|
return NewsContext.onSuccess("登录成功", LoginDTO.builder().user(user).token(token).build());
|
||||||
}else{
|
}else{
|
||||||
return NewsContext.onFail("登录失败");
|
return NewsContext.onFail("登录失败");
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package cn.jisol.game.data;
|
package cn.jisol.game.data;
|
||||||
|
|
||||||
|
import cn.jisol.game.entity.User;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ -8,21 +10,6 @@ import java.util.Map;
|
|||||||
*/
|
*/
|
||||||
public class Cache {
|
public class Cache {
|
||||||
|
|
||||||
public static Map<String,Object> DATA = new HashMap<>();
|
public static Map<String, User> TOKEN = new HashMap<>();
|
||||||
|
|
||||||
//存储数据
|
|
||||||
public void addData(String key,Object data){
|
|
||||||
DATA.put(key,data);
|
|
||||||
}
|
|
||||||
|
|
||||||
//读取数据
|
|
||||||
public Object getData(String key){
|
|
||||||
return DATA.get(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
//删除数据
|
|
||||||
public void delData(String key){
|
|
||||||
DATA.remove(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,14 @@
|
|||||||
|
package cn.jisol.game.dto;
|
||||||
|
|
||||||
|
import cn.jisol.game.entity.User;
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@ApiModel(value = "登录返回")
|
||||||
|
@Builder
|
||||||
|
@Data
|
||||||
|
public class LoginDTO {
|
||||||
|
User user; //玩家信息
|
||||||
|
String token; //登录token
|
||||||
|
}
|
@ -1,5 +1,9 @@
|
|||||||
package cn.jisol.game.network;
|
package cn.jisol.game.network;
|
||||||
|
|
||||||
|
import cn.jisol.game.actions.GActionEnum;
|
||||||
|
import cn.jisol.game.data.Cache;
|
||||||
|
import cn.jisol.game.network.client.GClient;
|
||||||
|
import cn.jisol.ngame.actions.core.NActionEnum;
|
||||||
import cn.jisol.ngame.client.NClient;
|
import cn.jisol.ngame.client.NClient;
|
||||||
import cn.jisol.ngame.network.JNetwork;
|
import cn.jisol.ngame.network.JNetwork;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
@ -11,21 +15,32 @@ import javax.websocket.Session;
|
|||||||
import javax.websocket.server.ServerEndpoint;
|
import javax.websocket.server.ServerEndpoint;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
@ServerEndpoint(
|
@ServerEndpoint(
|
||||||
value = "/websocket"
|
value = "/websocket/{token}"
|
||||||
)
|
)
|
||||||
@Controller
|
@Controller
|
||||||
public class WebSocket {
|
public class WebSocket {
|
||||||
|
|
||||||
public static final Map<String, NClient> CLIENTS = new ConcurrentHashMap<>();
|
public static final Map<String, NClient> CLIENTS = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
|
||||||
@OnOpen
|
@OnOpen
|
||||||
public void onOpen(Session session){
|
public void onOpen(Session session){
|
||||||
CLIENTS.put(session.getId(),new NClient(session));
|
|
||||||
|
String token = session.getPathParameters().get("token");
|
||||||
|
NClient client = new GClient(session);
|
||||||
|
if(Objects.isNull(Cache.TOKEN.get(token))){
|
||||||
|
//发送Token过期请求
|
||||||
|
client.invoke(GActionEnum.TOKEN_EXPIRED);
|
||||||
|
//关闭连接
|
||||||
|
client.Close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
CLIENTS.put(session.getId(),client);
|
||||||
System.out.printf("[WebSocket] %s 连接成功.\n",session.getId());
|
System.out.printf("[WebSocket] %s 连接成功.\n",session.getId());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@OnMessage
|
@OnMessage
|
||||||
|
@ -0,0 +1,35 @@
|
|||||||
|
package cn.jisol.game.network.client;
|
||||||
|
|
||||||
|
import cn.jisol.ngame.client.QueueNClient;
|
||||||
|
|
||||||
|
import javax.websocket.EncodeException;
|
||||||
|
import javax.websocket.Session;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class GClient extends QueueNClient {
|
||||||
|
|
||||||
|
public Session session;
|
||||||
|
|
||||||
|
public GClient(Session session){
|
||||||
|
super(session.getId());
|
||||||
|
this.session = session;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onMessage(byte[] bytes) {
|
||||||
|
if (session.isOpen()){
|
||||||
|
try {
|
||||||
|
session.getBasicRemote().sendObject(bytes);
|
||||||
|
} catch (Exception ignored) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onClose() {
|
||||||
|
if (session.isOpen()){
|
||||||
|
try {
|
||||||
|
session.close();
|
||||||
|
} catch (IOException ignored) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user