diff --git a/src/api/ApiAccountLogin.ts b/src/api/ApiAccountLogin.ts index 2286497..a6ac94b 100644 --- a/src/api/ApiAccountLogin.ts +++ b/src/api/ApiAccountLogin.ts @@ -1,6 +1,4 @@ import { ApiCall, BaseConnection } from "tsrpc"; -import Client from "../component/Client/Client"; -import User from "../component/Client/User"; import Lobby from "../component/Lobby/Lobby"; import { ReqAccountLogin, ResAccountLogin } from "../shared/protocols/PtlAccountLogin"; @@ -16,9 +14,8 @@ export default async function (call: ApiCall) const { name } = req const conn: BaseConnection = call.conn console.log(`name: ${name} is Login`) - const user = new User(name) - const client = new Client(conn, sn) - client.setUser(user) - Lobby.AddClient(client) + conn.UserId = sn + conn.NickName = name + Lobby.AddConns(conn) call.succ(0) } \ No newline at end of file diff --git a/src/component/Client/Client.ts b/src/component/Client/Client.ts index c5911fd..f592a53 100644 --- a/src/component/Client/Client.ts +++ b/src/component/Client/Client.ts @@ -1,128 +1,127 @@ -import { BaseConnection } from "tsrpc" -import User from "./User" +// import { BaseConnection } from "tsrpc" -/** - * Client - */ -export default class Client { +// /** +// * Client +// */ +// export default class Client { - //#region private +// //#region private - private conn: BaseConnection = undefined - private ws: any = undefined - private sn: number = undefined +// private conn: BaseConnection = undefined +// private ws: any = undefined +// private sn: number = undefined - //#endregion +// //#endregion - //#region get set +// //#region get set - public get User(): User { - return this.user - } +// public get User(): User { +// return this.user +// } - private user: User = undefined +// private user: User = undefined - //#endregion +// //#endregion - //#region Lifecycle +// //#region Lifecycle - /** - * - */ - constructor(conn: BaseConnection, sn: number) { - this.conn = conn - this.ws = conn["ws"] - this.sn = sn +// /** +// * +// */ +// constructor(conn: BaseConnection, sn: number) { +// this.conn = conn +// this.ws = conn["ws"] +// this.sn = sn - // // 當收到client消息時 - // ws.on('message', this.onMessage.bind(this)) - // // 當連線關閉 - // ws.on('close', this.onClose.bind(this)) - } +// // // 當收到client消息時 +// // ws.on('message', this.onMessage.bind(this)) +// // // 當連線關閉 +// // ws.on('close', this.onClose.bind(this)) +// } - //#endregion +// //#endregion - //#region Custom +// //#region Custom - /** - * setUser - */ - public setUser(user: User) { - this.user = user - } +// /** +// * setUser +// */ +// public setUser(user: User) { +// this.user = user +// } - //#endregion +// //#endregion - //#region Server +// //#region Server - // private onMessage(buffer: _ws.RawData): void { - // // 收回來是 Buffer 格式、需轉成字串 - // const dataStr: string = "[" + buffer.toString().split("[").slice(1).join("[") - // const json = JSON.parse(dataStr) - // const method = json[0] - // let status = 0 - // const data = json[1] - // const resp = { - // Method: method, - // Status: status, - // Data: data, - // IsValid: method && status === 0, - // WS: this - // } +// // private onMessage(buffer: _ws.RawData): void { +// // // 收回來是 Buffer 格式、需轉成字串 +// // const dataStr: string = "[" + buffer.toString().split("[").slice(1).join("[") +// // const json = JSON.parse(dataStr) +// // const method = json[0] +// // let status = 0 +// // const data = json[1] +// // const resp = { +// // Method: method, +// // Status: status, +// // Data: data, +// // IsValid: method && status === 0, +// // WS: this +// // } - // if (true) { - // if (data) { - // console.debug(`[RPC] 收到server呼叫:(${resp.WS.clientCount}): ${resp.Method}(${JSON.stringify(resp.Data)})`) - // } else { - // console.debug(`[RPC] 收到server呼叫:(${resp.WS.clientCount}): ${resp.Method}()`) - // } - // } +// // if (true) { +// // if (data) { +// // console.debug(`[RPC] 收到server呼叫:(${resp.WS.clientCount}): ${resp.Method}(${JSON.stringify(resp.Data)})`) +// // } else { +// // console.debug(`[RPC] 收到server呼叫:(${resp.WS.clientCount}): ${resp.Method}()`) +// // } +// // } - // WebSocketServerClass.Instance.OnDataReceived.DispatchCallback(resp) +// // WebSocketServerClass.Instance.OnDataReceived.DispatchCallback(resp) - // // /// 發送消息給client - // // this.SendClient(data) - // // WebSocketServerClass.Instance.SendAllClient(data) - // } +// // // /// 發送消息給client +// // // this.SendClient(data) +// // // WebSocketServerClass.Instance.SendAllClient(data) +// // } - // private onClose(): void { - // console.log(`Client_${this.clientCount} Close connected`) - // } +// // private onClose(): void { +// // console.log(`Client_${this.clientCount} Close connected`) +// // } - // /** 發送給client */ - // public SendClient(req: INetResponse): void { - // const status = 0 - // const json: any[] = [req.Method] - // //@ts-ignore - // if (req.Data != null && req.Data != undefined && req.Data != NaN) { - // json[1] = [status, req.Data] - // } +// // /** 發送給client */ +// // public SendClient(req: INetResponse): void { +// // const status = 0 +// // const json: any[] = [req.Method] +// // //@ts-ignore +// // if (req.Data != null && req.Data != undefined && req.Data != NaN) { +// // json[1] = [status, req.Data] +// // } - // if (true) { - // //@ts-ignore - // if (req.Data != null && req.Data != undefined && req.Data != NaN) { - // console.log(`[RPC] 傳送client資料:(${this.clientCount}): ${req.Method}(${JSON.stringify(req.Data)})`) - // } else { - // console.log(`[RPC] 傳送client資料:(${this.clientCount}): ${req.Method}()`) - // } - // } +// // if (true) { +// // //@ts-ignore +// // if (req.Data != null && req.Data != undefined && req.Data != NaN) { +// // console.log(`[RPC] 傳送client資料:(${this.clientCount}): ${req.Method}(${JSON.stringify(req.Data)})`) +// // } else { +// // console.log(`[RPC] 傳送client資料:(${this.clientCount}): ${req.Method}()`) +// // } +// // } - // const str = JSON.stringify(json) - // if (str.length > 65535) { - // throw new Error('要傳的資料太大囉') - // } +// // const str = JSON.stringify(json) +// // if (str.length > 65535) { +// // throw new Error('要傳的資料太大囉') +// // } - // const strary = Encoding.UTF8.GetBytes(str) - // const buffer = new Uint8Array(4 + strary.byteLength) - // const u16ary = new Uint16Array(buffer.buffer, 0, 3) - // u16ary[0] = strary.byteLength - // buffer[3] = 0x01 - // buffer.set(strary, 4) +// // const strary = Encoding.UTF8.GetBytes(str) +// // const buffer = new Uint8Array(4 + strary.byteLength) +// // const u16ary = new Uint16Array(buffer.buffer, 0, 3) +// // u16ary[0] = strary.byteLength +// // buffer[3] = 0x01 +// // buffer.set(strary, 4) - // this.ws.send(buffer) - // } +// // this.ws.send(buffer) +// // } - //#endregion -} +// //#endregion +// } diff --git a/src/component/Client/User.ts b/src/component/Client/User.ts deleted file mode 100644 index 58314e8..0000000 --- a/src/component/Client/User.ts +++ /dev/null @@ -1,29 +0,0 @@ - - -/** - * User - */ -export default class User { - - //#region get set - - public get Name(): string { - return this.name - } - - private name: string = undefined - - //#endregion - - //#region Lifecycle - - constructor(name: string) { - this.name = name - } - - //#endregion - - //#region Custom - - //#endregion -} diff --git a/src/component/Lobby/Lobby.ts b/src/component/Lobby/Lobby.ts index 4a294f6..b5c610b 100644 --- a/src/component/Lobby/Lobby.ts +++ b/src/component/Lobby/Lobby.ts @@ -1,4 +1,4 @@ -import Client from "../Client/Client"; +import { BaseConnection } from "tsrpc"; import Room from "../Room/Room"; /** @@ -8,7 +8,7 @@ export default class Lobby { //#region private - private static clients: Client[] = [] + private static conns: BaseConnection[] = []; private static serialNumber: number = 0 //#endregion @@ -22,9 +22,9 @@ export default class Lobby { //#region Custom - /** AddClient */ - public static AddClient(client: Client): void { - this.clients.push(client) + /** AddConns */ + public static AddConns(conn: BaseConnection): void { + this.conns.push(conn) } // /** List */ diff --git a/src/component/User/User.ts b/src/component/User/User.ts new file mode 100644 index 0000000..0a12f1b --- /dev/null +++ b/src/component/User/User.ts @@ -0,0 +1,31 @@ + + +/** + * User + */ +export default class User { + + //#region get set + + // public get NickName(): string { + // return this.nickName + // } + + // private nickName: string = undefined + public UserId: number = undefined + public NickName: string = undefined + + //#endregion + + //#region Lifecycle + + // constructor(nickName: string) { + // this.nickName = nickName + // } + + //#endregion + + //#region Custom + + //#endregion +} diff --git a/src/index.ts b/src/index.ts index 676fae9..f69d184 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,7 @@ import "dayjs/locale/zh-tw"; import dotenv from "dotenv"; import * as path from "path"; import { WsServer } from "tsrpc"; +import User from "./component/User/User"; import { BaseEnumerator } from "./Engine/CatanEngine/CoroutineV2/Core/BaseEnumerator"; import "./Engine/CatanEngine/CSharp/String"; import "./Engine/Utils/CCExtensions/ArrayExtension"; @@ -34,4 +35,8 @@ async function main() { await init(); await server.start(); } -main(); \ No newline at end of file +main(); + +declare module 'tsrpc' { + export interface BaseConnection extends User { } +} \ No newline at end of file