[add] Lobby List
This commit is contained in:
parent
65a0d2ed41
commit
81ca68d81f
@ -2,12 +2,12 @@
|
||||
import _ws from "ws"
|
||||
import { Encoding } from "../Engine/CatanEngine/CSharp/System/Text/Encoding"
|
||||
import { INetResponse } from "../Engine/CatanEngine/NetManagerV2/Core/INetResponse"
|
||||
import WebSocketServerClass from "./WebSocketServerClass"
|
||||
import WebSocketServerClass from "../NetManager/WebSocketServerClass"
|
||||
|
||||
/**
|
||||
* WebSocket
|
||||
* Client
|
||||
*/
|
||||
export default class WebSocketClass {
|
||||
export default class Client {
|
||||
|
||||
//#region private
|
||||
|
||||
@ -69,7 +69,7 @@ export default class WebSocketClass {
|
||||
console.log(`Client_${this.clientCount} Close connected`)
|
||||
}
|
||||
|
||||
/** 發送給所有client */
|
||||
/** 發送給client */
|
||||
public SendClient(req: INetResponse<any>): void {
|
||||
const status = 0
|
||||
const json: any[] = [req.Method]
|
@ -1,5 +1,6 @@
|
||||
import { INetResponse } from "../Engine/CatanEngine/NetManagerV2/Core/INetResponse"
|
||||
import WebSocketServerClass from "../NetManager/WebSocketServerClass"
|
||||
import { INetResponse } from "../Engine/CatanEngine/NetManagerV2/Core/INetResponse";
|
||||
import Lobby from "../Lobby/Lobby";
|
||||
import WebSocketServerClass from "../NetManager/WebSocketServerClass";
|
||||
|
||||
export default class MainControlData {
|
||||
constructor() {
|
||||
@ -7,11 +8,11 @@ export default class MainControlData {
|
||||
}
|
||||
|
||||
/** SERVER主動通知 */
|
||||
private _serverData(resp: INetResponse<any>): void {
|
||||
if (resp.IsValid) {
|
||||
switch (resp.Method) {
|
||||
case "chat.send":
|
||||
resp.WS.SendClient(resp)
|
||||
private _serverData(req: INetResponse<any>): void {
|
||||
if (req.IsValid) {
|
||||
switch (req.Method) {
|
||||
case "lobby.list":
|
||||
Lobby.GetList(req);
|
||||
// GiftData.Instance?.AddCommonList(resp.Data)
|
||||
break
|
||||
default:
|
||||
|
@ -7,7 +7,7 @@ export interface INetRequest<TRequest, TResponse> {
|
||||
Data: TRequest;
|
||||
Result: INetResponse<TResponse>;
|
||||
|
||||
SendAsync(): Iterator<any>;
|
||||
// SendAsync(): Iterator<any>;
|
||||
|
||||
Send();
|
||||
// Send();
|
||||
}
|
@ -1,9 +1,9 @@
|
||||
import WebSocketClass from "../../../../NetManager/WebSocketClass"
|
||||
import Client from "../../../../Client/Client"
|
||||
|
||||
export interface INetResponse<TResponse> {
|
||||
readonly Method: string
|
||||
readonly WS: WebSocketClass
|
||||
readonly WS: Client
|
||||
readonly Status: number
|
||||
readonly Data: TResponse
|
||||
readonly IsValid: boolean
|
||||
readonly IsValid?: boolean
|
||||
}
|
@ -1,5 +1,3 @@
|
||||
import Event from "@/modules/event";
|
||||
import * as Define from "@/modules/player/define";
|
||||
import { BaseEnumerator } from "../CoroutineV2/Core/BaseEnumerator";
|
||||
import { Action } from "../CSharp/System/Action";
|
||||
import { Encoding } from "../CSharp/System/Text/Encoding";
|
||||
@ -8,8 +6,6 @@ import { INetResponse } from "./Core/INetResponse";
|
||||
import NetConfig from "./NetConfig";
|
||||
|
||||
export class NetConnector {
|
||||
/** Event */
|
||||
public readonly event: Event<Define.Event.Func> = new Event<Define.Event.Func>();
|
||||
readonly OnDataReceived: Action<INetResponse<any>> = new Action<INetResponse<any>>();
|
||||
readonly OnDisconnected: Action<void> = new Action<void>();
|
||||
readonly OnLoadUIMask: Action<boolean> = new Action<boolean>();
|
||||
@ -137,7 +133,7 @@ export class NetConnector {
|
||||
// if (CC_DEBUG) {
|
||||
console.debug(`[RPC] ${this._host} Connected.`);
|
||||
// }
|
||||
this.event.emit(Define.Event.Socket.Connect);
|
||||
// this.event.emit(Define.Event.Socket.Connect);
|
||||
}
|
||||
|
||||
private OnWebSocketMessage(e: MessageEvent) {
|
||||
@ -208,11 +204,11 @@ export class NetConnector {
|
||||
|
||||
private OnWebSocketClose(e: CloseEvent) {
|
||||
this.WebSocketEnded();
|
||||
this.event.emit(Define.Event.Socket.Disconnect);
|
||||
// this.event.emit(Define.Event.Socket.Disconnect);
|
||||
}
|
||||
|
||||
private OnWebSocketError(e: CloseEvent) {
|
||||
this.event.emit(Define.Event.Socket.Error);
|
||||
// this.event.emit(Define.Event.Socket.Error);
|
||||
}
|
||||
}
|
||||
|
||||
@ -221,6 +217,7 @@ const ErrorResponse: INetResponse<any> = {
|
||||
Method: "",
|
||||
Data: {},
|
||||
IsValid: false,
|
||||
WS: undefined,
|
||||
};
|
||||
|
||||
class WsConnectEnumerator extends BaseEnumerator {
|
||||
|
@ -1,4 +1,3 @@
|
||||
import { VueNetConnector } from "@/assets/VueScript/Net/VueNetConnector";
|
||||
import { INetRequest } from "./Core/INetRequest";
|
||||
|
||||
export abstract class NetRequest<TResquest, TResponse> implements INetRequest<TResquest, TResponse> {
|
||||
@ -11,18 +10,18 @@ export abstract class NetRequest<TResquest, TResponse> implements INetRequest<TR
|
||||
Data: TResquest;
|
||||
Result: import("./Core/INetResponse").INetResponse<TResponse>;
|
||||
|
||||
/**
|
||||
* 在大廳呼叫Cocos會收到SERVER主動通知
|
||||
* 在大廳呼叫Cocos會收到SERVER主動通知
|
||||
* 在大廳呼叫Cocos會收到SERVER主動通知
|
||||
*/
|
||||
SendAsync(mask: boolean = false): Iterator<any> {
|
||||
// return NetManager.SendAsync(this, mask);
|
||||
return VueNetConnector.SendAsync(this, mask);
|
||||
}
|
||||
// /**
|
||||
// * 在大廳呼叫Cocos會收到SERVER主動通知
|
||||
// * 在大廳呼叫Cocos會收到SERVER主動通知
|
||||
// * 在大廳呼叫Cocos會收到SERVER主動通知
|
||||
// */
|
||||
// SendAsync(mask: boolean = false): Iterator<any> {
|
||||
// // return NetManager.SendAsync(this, mask);
|
||||
// return VueNetConnector.SendAsync(this, mask);
|
||||
// }
|
||||
|
||||
Send() {
|
||||
// NetManager.Send(this);
|
||||
VueNetConnector.Send(this);
|
||||
}
|
||||
// Send() {
|
||||
// // NetManager.Send(this);
|
||||
// VueNetConnector.Send(this);
|
||||
// }
|
||||
}
|
||||
|
28
src/Engine/CatanEngine/NetManagerV2/NetResponse.ts
Normal file
28
src/Engine/CatanEngine/NetManagerV2/NetResponse.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import Client from "../../../Client/Client"
|
||||
import { INetResponse } from "./Core/INetResponse"
|
||||
|
||||
export abstract class NetResponse implements INetResponse<any> {
|
||||
protected data: any = undefined
|
||||
protected method: string = ""
|
||||
protected ws: Client = undefined
|
||||
protected status: number = undefined
|
||||
|
||||
get Method(): string {
|
||||
return this.method
|
||||
}
|
||||
get WS(): Client {
|
||||
return this.ws
|
||||
}
|
||||
get Status(): number {
|
||||
return this.status
|
||||
}
|
||||
get Data(): any {
|
||||
return this.data
|
||||
}
|
||||
|
||||
constructor(ws: Client, data: any, status: number = 0) {
|
||||
this.ws = ws
|
||||
this.data = data
|
||||
this.status = status
|
||||
}
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
import MainControlData from "./DataReceived/MainControlData"
|
||||
import WebSocketServerClass from "./NetManager/WebSocketServerClass"
|
||||
|
||||
/**
|
||||
* GuessWhoIAm
|
||||
*/
|
||||
export default class GuessWhoIAmClass {
|
||||
|
||||
//#region private
|
||||
|
||||
private clientCount: number = 0
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region Lifecycle
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
constructor() {
|
||||
new WebSocketServerClass()
|
||||
new MainControlData()
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region Custom
|
||||
|
||||
//#endregion
|
||||
}
|
42
src/Lobby/Lobby.ts
Normal file
42
src/Lobby/Lobby.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import { LobbyListRequest } from "../define/Request/LobbyRequest";
|
||||
import { INetResponse } from "../Engine/CatanEngine/NetManagerV2/Core/INetResponse";
|
||||
import Room from "../Room/Room";
|
||||
|
||||
/**
|
||||
* Lobby
|
||||
*/
|
||||
export default class Lobby {
|
||||
|
||||
//#region private
|
||||
|
||||
private static list: Room[] = []
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region Custom
|
||||
|
||||
/** GetList */
|
||||
public static GetList(req: INetResponse<any>): void {
|
||||
let resp: LobbyListRequest = new LobbyListRequest(req.WS, this.list, 0)
|
||||
req.WS.SendClient(resp)
|
||||
}
|
||||
|
||||
/** Create */
|
||||
public static Create(req: INetResponse<any>): void {
|
||||
let room = new Room()
|
||||
this.list.push(room)
|
||||
//
|
||||
}
|
||||
|
||||
/** Join */
|
||||
public static Join(req: INetResponse<any>): void {
|
||||
//
|
||||
}
|
||||
|
||||
/** Exit */
|
||||
public static Exit(req: INetResponse<any>): void {
|
||||
//
|
||||
}
|
||||
|
||||
//#endregion
|
||||
}
|
@ -1,10 +1,10 @@
|
||||
import express from "express"
|
||||
import fs from "fs"
|
||||
import _ws from "ws"
|
||||
import Client from "../Client/Client"
|
||||
import { Action } from "../Engine/CatanEngine/CSharp/System/Action"
|
||||
import { INetResponse } from "../Engine/CatanEngine/NetManagerV2/Core/INetResponse"
|
||||
import BaseSingleton from "../Engine/Utils/Singleton/BaseSingleton"
|
||||
import WebSocketClass from "./WebSocketClass"
|
||||
const SocketServer: typeof _ws.Server = _ws.Server
|
||||
|
||||
/**
|
||||
@ -63,7 +63,7 @@ export default class WebSocketServerClass extends BaseSingleton<WebSocketServerC
|
||||
private onConnection(ws: _ws): void {
|
||||
const clientNum: number = this.clientCount
|
||||
console.log(`Client_${clientNum} connected`)
|
||||
new WebSocketClass(ws, clientNum)
|
||||
new Client(ws, clientNum)
|
||||
this.clientCount++
|
||||
}
|
||||
|
||||
|
28
src/Room/Room.ts
Normal file
28
src/Room/Room.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import BaseSingleton from "../Engine/Utils/Singleton/BaseSingleton"
|
||||
|
||||
/**
|
||||
* Room
|
||||
*/
|
||||
export default class Room extends BaseSingleton<Room>() {
|
||||
|
||||
//#region private
|
||||
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region Lifecycle
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
constructor() {
|
||||
super()
|
||||
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region Custom
|
||||
|
||||
//#endregion
|
||||
}
|
@ -6,11 +6,13 @@
|
||||
import dayjs from "dayjs"
|
||||
import "dayjs/locale/zh-tw"
|
||||
import dotenv from "dotenv"
|
||||
import MainControlData from "./DataReceived/MainControlData"
|
||||
import "./Engine/CatanEngine/CSharp/String"
|
||||
import "./Engine/Utils/CCExtensions/ArrayExtension"
|
||||
import "./Engine/Utils/CCExtensions/NumberExtension"
|
||||
import GuessWhoIAmClass from "./GuessWhoIAmClass"
|
||||
import WebSocketServerClass from "./NetManager/WebSocketServerClass"
|
||||
|
||||
dayjs.locale("zh-tw")
|
||||
dotenv.config()
|
||||
new GuessWhoIAmClass()
|
||||
new WebSocketServerClass()
|
||||
new MainControlData()
|
28
src/define/Request/.ExampleCodeRequest.ts
Normal file
28
src/define/Request/.ExampleCodeRequest.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { NetRequest } from "../../Engine/CatanEngine/NetManagerV2/NetRequest";
|
||||
|
||||
// #region Request
|
||||
|
||||
export type RpcExampleCodeRequest = null
|
||||
export type RpcExampleCodeResponse = ExampleCodeData[]
|
||||
|
||||
export class ExampleCodeRequest extends NetRequest<RpcExampleCodeRequest, RpcExampleCodeResponse> {
|
||||
get Method(): string {
|
||||
return "example.code";
|
||||
}
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region Type
|
||||
|
||||
export type ExampleCodeData = [
|
||||
id: number,
|
||||
title: number,
|
||||
content: number,
|
||||
time: number,
|
||||
];
|
||||
|
||||
// #endregion
|
17
src/define/Request/LobbyRequest.ts
Normal file
17
src/define/Request/LobbyRequest.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { NetResponse } from "../../Engine/CatanEngine/NetManagerV2/NetResponse"
|
||||
import Room from "../../Room/Room"
|
||||
|
||||
// #region Request
|
||||
|
||||
export type RpcLobbyListRequest = Room[]
|
||||
|
||||
export class LobbyListRequest extends NetResponse {
|
||||
protected data: RpcLobbyListRequest = undefined
|
||||
protected method: string = "lobby.list"
|
||||
}
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region Type
|
||||
|
||||
// #endregion
|
Loading…
x
Reference in New Issue
Block a user