129 lines
2.8 KiB
TypeScript
129 lines
2.8 KiB
TypeScript
|
|
||
|
import { BaseConnection } from "tsrpc"
|
||
|
import User from "./User"
|
||
|
|
||
|
/**
|
||
|
* Client
|
||
|
*/
|
||
|
export default class Client {
|
||
|
|
||
|
//#region private
|
||
|
|
||
|
private conn: BaseConnection<any> = undefined
|
||
|
private ws: any = undefined
|
||
|
private sn: number = undefined
|
||
|
|
||
|
//#endregion
|
||
|
|
||
|
//#region get set
|
||
|
|
||
|
public get User(): User {
|
||
|
return this.user
|
||
|
}
|
||
|
|
||
|
private user: User = undefined
|
||
|
|
||
|
|
||
|
//#endregion
|
||
|
|
||
|
//#region Lifecycle
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
*/
|
||
|
constructor(conn: BaseConnection<any>, 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))
|
||
|
}
|
||
|
|
||
|
//#endregion
|
||
|
|
||
|
//#region Custom
|
||
|
|
||
|
/**
|
||
|
* setUser
|
||
|
*/
|
||
|
public setUser(user: User) {
|
||
|
this.user = user
|
||
|
}
|
||
|
|
||
|
//#endregion
|
||
|
|
||
|
//#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 = <string>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}()`)
|
||
|
// }
|
||
|
// }
|
||
|
|
||
|
// WebSocketServerClass.Instance.OnDataReceived.DispatchCallback(resp)
|
||
|
|
||
|
// // /// 發送消息給client
|
||
|
// // this.SendClient(data)
|
||
|
// // WebSocketServerClass.Instance.SendAllClient(data)
|
||
|
// }
|
||
|
|
||
|
// private onClose(): void {
|
||
|
// console.log(`Client_${this.clientCount} Close connected`)
|
||
|
// }
|
||
|
|
||
|
// /** 發送給client */
|
||
|
// public SendClient(req: INetResponse<any>): 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}()`)
|
||
|
// }
|
||
|
// }
|
||
|
|
||
|
// 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)
|
||
|
|
||
|
// this.ws.send(buffer)
|
||
|
// }
|
||
|
|
||
|
//#endregion
|
||
|
}
|