zero-remote
Advanced tools
+102
| import ZeroRemote, { GameVisitor, ICSMessage, ISCMessage, ZeroRemoteClient } from "./index" | ||
| import ws from "ws" | ||
| import { T2 } from "./T2" | ||
| class User extends GameVisitor { | ||
| clear(): void { | ||
| } | ||
| a = 0 | ||
| start() { | ||
| this.getReceiver<T2>("fuck").getPid(async () => { | ||
| this.a++ | ||
| return this.a | ||
| }) | ||
| // this.getReceiver<T1>("fuck").on("fuck", (data) => { | ||
| // setTimeout(() => { | ||
| // data.b() | ||
| // }, 2000); | ||
| // }) | ||
| // this.getReceiver<T1>("fuck").fuck((data) => { | ||
| // console.log("___________") | ||
| // setTimeout(() => { | ||
| // data.b() | ||
| // }, 2000); | ||
| // return 5 | ||
| // }) | ||
| // this.getSender<T1>("tt").fuck({ | ||
| // a: 1, b: () => { | ||
| // console.log("____nonono") | ||
| // } | ||
| // }) | ||
| } | ||
| } | ||
| let zn = new ZeroRemote(new ws.Server({ port: 8888 }), (carer, server) => { | ||
| return new User(carer, server) | ||
| }) | ||
| //------------------------------------------------- | ||
| // interface A { | ||
| // init(a: string, b: number): number | ||
| // init2(): void | ||
| // } | ||
| // interface B { | ||
| // a(): void | ||
| // } | ||
| // /** | ||
| // * 新建服务 | ||
| // */ | ||
| // new ZeroRemote(new ws.Server({ port: 2000 }), (carer) => { | ||
| // return new RPCVisitor(carer) | ||
| // }) | ||
| // RPCVisitor.addRemote<A>(async (visitor) => { | ||
| // let a = await visitor.rs.init("a", 4) | ||
| // console.log("打印远程回调结果") | ||
| // console.log(a) | ||
| // }, "tt") | ||
| // RPCVisitor.addRemote<B>(async (visitor) => { | ||
| // let xx = async () => { | ||
| // await visitor.rs.a() | ||
| // xx() | ||
| // } | ||
| // xx() | ||
| // }) | ||
| // class CS extends RPCClient implements A { | ||
| // constructor() { | ||
| // super("ws:127.0.0.1:2000", "tt") | ||
| // } | ||
| // init(a: string, b: number): number { | ||
| // console.log(a) | ||
| // return 5 + b | ||
| // } | ||
| // init2(): void { | ||
| // console.log("_init2____") | ||
| // } | ||
| // } | ||
| // class CS2 extends RPCClient implements B { | ||
| // constructor() { | ||
| // super("ws:127.0.0.1:2000") | ||
| // } | ||
| // a(): void { | ||
| // console.log("aaa") | ||
| // } | ||
| // } | ||
| // let client = new CS() | ||
| // let client2 = new CS2() | ||
| // client.link() | ||
| // client.on("open", () => { | ||
| // console.log("rpc linked") | ||
| // }) | ||
| // client2.link() |
+27
| import { ICSMessage, ISCMessage, ZeroRemoteClient } from "." | ||
| import { T2 } from "./T2" | ||
| setTimeout(() => { | ||
| let cc = new ZeroRemoteClient("ws://192.168.16.195:8888") | ||
| cc.link(() => { | ||
| console.log("linkEd") | ||
| }) | ||
| cc.on("linked", () => { | ||
| console.log("linked") | ||
| }) | ||
| let sender = cc.getSender<T2>("fuck") | ||
| cc.on("showMask",()=>{ | ||
| console.log("showMask") | ||
| }) | ||
| setInterval(() => { | ||
| sender.getPid().then((a) => { | ||
| console.log(a) | ||
| }).catch((e) => { | ||
| console.log("___", e) | ||
| }) | ||
| }, 500) | ||
| }, 2000) | ||
+122
| "use strict"; | ||
| var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
| if (k2 === undefined) k2 = k; | ||
| Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
| }) : (function(o, m, k, k2) { | ||
| if (k2 === undefined) k2 = k; | ||
| o[k2] = m[k]; | ||
| })); | ||
| var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
| Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
| }) : function(o, v) { | ||
| o["default"] = v; | ||
| }); | ||
| var __importStar = (this && this.__importStar) || function (mod) { | ||
| if (mod && mod.__esModule) return mod; | ||
| var result = {}; | ||
| if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
| __setModuleDefault(result, mod); | ||
| return result; | ||
| }; | ||
| var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
| function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
| return new (P || (P = Promise))(function (resolve, reject) { | ||
| function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
| function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
| function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
| step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
| }); | ||
| }; | ||
| var __importDefault = (this && this.__importDefault) || function (mod) { | ||
| return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| const index_1 = __importStar(require("./index")); | ||
| const ws_1 = __importDefault(require("ws")); | ||
| class User extends index_1.GameVisitor { | ||
| constructor() { | ||
| super(...arguments); | ||
| this.a = 0; | ||
| } | ||
| clear() { | ||
| } | ||
| start() { | ||
| this.getReceiver("fuck").getPid(() => __awaiter(this, void 0, void 0, function* () { | ||
| this.a++; | ||
| return this.a; | ||
| })); | ||
| // this.getReceiver<T1>("fuck").on("fuck", (data) => { | ||
| // setTimeout(() => { | ||
| // data.b() | ||
| // }, 2000); | ||
| // }) | ||
| // this.getReceiver<T1>("fuck").fuck((data) => { | ||
| // console.log("___________") | ||
| // setTimeout(() => { | ||
| // data.b() | ||
| // }, 2000); | ||
| // return 5 | ||
| // }) | ||
| // this.getSender<T1>("tt").fuck({ | ||
| // a: 1, b: () => { | ||
| // console.log("____nonono") | ||
| // } | ||
| // }) | ||
| } | ||
| } | ||
| let zn = new index_1.default(new ws_1.default.Server({ port: 8888 }), (carer, server) => { | ||
| return new User(carer, server); | ||
| }); | ||
| //------------------------------------------------- | ||
| // interface A { | ||
| // init(a: string, b: number): number | ||
| // init2(): void | ||
| // } | ||
| // interface B { | ||
| // a(): void | ||
| // } | ||
| // /** | ||
| // * 新建服务 | ||
| // */ | ||
| // new ZeroRemote(new ws.Server({ port: 2000 }), (carer) => { | ||
| // return new RPCVisitor(carer) | ||
| // }) | ||
| // RPCVisitor.addRemote<A>(async (visitor) => { | ||
| // let a = await visitor.rs.init("a", 4) | ||
| // console.log("打印远程回调结果") | ||
| // console.log(a) | ||
| // }, "tt") | ||
| // RPCVisitor.addRemote<B>(async (visitor) => { | ||
| // let xx = async () => { | ||
| // await visitor.rs.a() | ||
| // xx() | ||
| // } | ||
| // xx() | ||
| // }) | ||
| // class CS extends RPCClient implements A { | ||
| // constructor() { | ||
| // super("ws:127.0.0.1:2000", "tt") | ||
| // } | ||
| // init(a: string, b: number): number { | ||
| // console.log(a) | ||
| // return 5 + b | ||
| // } | ||
| // init2(): void { | ||
| // console.log("_init2____") | ||
| // } | ||
| // } | ||
| // class CS2 extends RPCClient implements B { | ||
| // constructor() { | ||
| // super("ws:127.0.0.1:2000") | ||
| // } | ||
| // a(): void { | ||
| // console.log("aaa") | ||
| // } | ||
| // } | ||
| // let client = new CS() | ||
| // let client2 = new CS2() | ||
| // client.link() | ||
| // client.on("open", () => { | ||
| // console.log("rpc linked") | ||
| // }) | ||
| // client2.link() |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| const _1 = require("."); | ||
| setTimeout(() => { | ||
| let cc = new _1.ZeroRemoteClient("ws://192.168.16.195:8888"); | ||
| cc.link(() => { | ||
| console.log("linkEd"); | ||
| }); | ||
| cc.on("linked", () => { | ||
| console.log("linked"); | ||
| }); | ||
| let sender = cc.getSender("fuck"); | ||
| cc.on("showMask", () => { | ||
| console.log("showMask"); | ||
| }); | ||
| setInterval(() => { | ||
| sender.getPid().then((a) => { | ||
| console.log(a); | ||
| }).catch((e) => { | ||
| console.log("___", e); | ||
| }); | ||
| }, 500); | ||
| }, 2000); |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); |
+6
| // interface T1 { | ||
| // fuck: (value: { a: number, b: Function }) => number | ||
| // } | ||
| export interface T2 { | ||
| getPid: () => number; | ||
| } |
+11
-2
@@ -7,9 +7,18 @@ { | ||
| "request": "launch", | ||
| "name": "启动", | ||
| "name": "服务", | ||
| "runtimeExecutable": "yarn", | ||
| "args": [ | ||
| "demo", | ||
| "demo1", | ||
| ] | ||
| }, | ||
| { | ||
| "type": "pwa-node", | ||
| "request": "launch", | ||
| "name": "客户", | ||
| "runtimeExecutable": "yarn", | ||
| "args": [ | ||
| "demo2", | ||
| ] | ||
| }, | ||
| ] | ||
| } |
+252
-227
@@ -0,11 +1,16 @@ | ||
| // import ws from "ws"; | ||
| // import { ZeroDispatcher } from "zero-mvc"; | ||
| // import { address as getLocalIp } from "ip" | ||
| // import ZeroRemote, { GameVisitor, IChannel, ICSMessage, ISCMessage, Receiver, ReportCode, Sender, Visitor, ZeroRemoteClient } from "zero-remote"; | ||
| // import ZeroRemote, { GameVisitor, IChannel, ICSMessage, ISCMessage, ReturnReceiver, ZeroRemoteClient } from "zero-remote"; | ||
| // type CMType = new (...ary: any[]) => Control | Model | ||
| // type MType = new (...ary: any[]) => Control | Model | ||
| // type CMType = new (...ary: any[]) => ZeroServerNode | ||
| // type MType = new (...ary: any[]) => ZeroServerNode | ||
| // export let routeMap: WeakMap<CMType, string> = new WeakMap() | ||
| // export let balancePool: { [key: string]: (player: any, totle: number) => number } = {} | ||
| // /** | ||
| // * 装饰器 | ||
| // * @param key | ||
| // * @returns | ||
| // */ | ||
| // export function route(key: string) { | ||
@@ -16,3 +21,8 @@ // return (type: CMType) => { | ||
| // } | ||
| // export function balance<T extends PlayerInfo>(callback: (player: T, totle: number) => number) { | ||
| // /** | ||
| // * 装饰器 用去落点均衡 | ||
| // * @param callback | ||
| // * @returns | ||
| // */ | ||
| // export function balance<T extends UserInfo>(callback: (player: T, totle: number) => number) { | ||
| // return (type: MType) => { | ||
@@ -25,8 +35,10 @@ // let route = getRoute(type) | ||
| // } | ||
| // // balance(model: ModelType, callback: (player: T) => number): void { | ||
| // // //TODO | ||
| // // } | ||
| // export function getRoute(controlType: new (...args: any[]) => any): string { | ||
| // let route = routeMap.get(controlType) | ||
| // /** | ||
| // * 获取装饰器路由名 | ||
| // * @param connectorType | ||
| // * @returns | ||
| // */ | ||
| // export function getRoute(connectorType: new (...args: any[]) => any): string { | ||
| // let route = routeMap.get(connectorType) | ||
| // if (route) { | ||
@@ -40,6 +52,8 @@ // return route | ||
| // /** | ||
| // * server类型 | ||
| // */ | ||
| // enum ServerType { | ||
| // PART, | ||
| // LINK, | ||
| // MODULE, | ||
| // CONNECT, | ||
| // SPECIAL, | ||
@@ -49,5 +63,6 @@ // } | ||
| // interface IZeroServerCS { | ||
| // init: { url: string, name: string, models: string[], type: ServerType, pid: number } | ||
| // getPid: { callback: (pid: number) => void } | ||
| // init: (value: { url: string, name: string, models: string[], type: ServerType, pid: number }) => void | ||
| // getPid: () => number | ||
| // addLine: (sid: number) => void | ||
| // removeLine: (sid: number) => void | ||
| // } | ||
@@ -62,13 +77,18 @@ | ||
| // interface IZeroServerSC { | ||
| // clustered: number | ||
| // add: IZeroServerInfo | ||
| // remove: number | ||
| // // givePid: number | ||
| // clustered: (value: number) => void | ||
| // add: (value: IZeroServerInfo) => void | ||
| // remove: (value: number) => void | ||
| // } | ||
| // type ModelType = new (...arg: any[]) => Model & { route: string } | ||
| // type ModuleType = new (...arg: any[]) => Module & { route: string } | ||
| // /** | ||
| // * server基类 | ||
| // */ | ||
| // export class ZeroServer extends ZeroRemoteClient { | ||
| // serverType: ServerType = ServerType.PART | ||
| // serverType: ServerType = ServerType.MODULE | ||
| // url: string; | ||
| // static mainUrl: string | ||
| // static modelPool: { [key: string]: Model } = {} | ||
| // static modelPool: { [key: string]: Module } = {} | ||
| // models: any = [] | ||
@@ -115,9 +135,7 @@ // clusterSender = this.getSender<IZeroServerCS>("") | ||
| // if (ZeroServer.pid == null) { | ||
| // this.clusterSender.getPid({ | ||
| // callback: (pid) => { | ||
| // if (ZeroServer.pid == null) { | ||
| // ZeroServer.pid = pid | ||
| // } | ||
| // this.sendInit() | ||
| // this.clusterSender.getPid().then((pid) => { | ||
| // if (ZeroServer.pid == null) { | ||
| // ZeroServer.pid = pid | ||
| // } | ||
| // this.sendInit() | ||
| // }) | ||
@@ -129,13 +147,13 @@ // } else { | ||
| // private sendInit() { | ||
| // this.clusterSender.init({ url: getLocalIp() + ":" + this.port, name: this.name, type: this.serverType, models: this.models, pid: ZeroServer.pid }) | ||
| // } | ||
| // /** | ||
| // * 生命不息 重连不止 | ||
| // */ | ||
| // popup(): void { | ||
| // console.log("集群失败") | ||
| // } | ||
| // linking(): void { | ||
| // } | ||
| // // main.addControl(ServerKey.LINK, () => { | ||
| // // }) | ||
| // addModel(modelType: ModelType) { | ||
| // addModule(modelType: ModuleType) { | ||
| // let route = getRoute(modelType) | ||
@@ -152,15 +170,10 @@ // if (route) { | ||
| // // export enum ServerKey { | ||
| // // ENTRY = "entry", | ||
| // // LINK = "link", | ||
| // // ADMIN = "admin" | ||
| // // } | ||
| // /** | ||
| // * 放在Link服务上 | ||
| // * 用来连接Model的线路 | ||
| // * 放在Connector服务上 | ||
| // * 用来连接Module的线路 | ||
| // */ | ||
| // class ClientFotPart extends ZeroRemoteClient { | ||
| // class ClientFotModule extends ZeroRemoteClient { | ||
| // relinkMaxCount = 100 | ||
| // popupReLinkTime: number = 1000 | ||
| // popupReLinkTimeIndex!: NodeJS.Timeout; | ||
| // constructor(public url: string) { | ||
@@ -182,5 +195,15 @@ // super() | ||
| // linked(): void { | ||
| // // throw new Error("Method not implemented."); | ||
| // this.popupReLinkTime = 1000 | ||
| // } | ||
| // popup(): void { | ||
| // console.log("pppppp") | ||
| // for (const key in this.callbackPool) { | ||
| // const element = this.callbackPool[key]; | ||
| // console.log(element) | ||
| // element(false, "服务器被断开") | ||
| // } | ||
| // this.popupReLinkTimeIndex = setTimeout(() => { | ||
| // this.link() | ||
| // this.popupReLinkTime += 1000 | ||
| // }, this.popupReLinkTime) | ||
| // // throw new Error("Method not implemented."); | ||
@@ -194,5 +217,6 @@ // } | ||
| // export class ZeroServerUser<T extends PlayerInfo = PlayerInfo> extends GameVisitor { | ||
| // // hook: ZeroDispatcher<any> = new ZeroDispatcher() | ||
| // /** | ||
| // * 用户 | ||
| // */ | ||
| // export class ZeroServerUser<T extends UserInfo = UserInfo> extends GameVisitor { | ||
| // static pool: { [key: string]: ZeroServerUser } = {} | ||
@@ -203,8 +227,7 @@ // static index: number = 0 | ||
| // } | ||
| // // protected modelPool: { [key: string]: Model } = {} | ||
| // clear(): void { | ||
| // } | ||
| // constructor(channel: IChannel<ISCMessage>, public info: T) { | ||
| // super(channel) | ||
| // constructor(channel: IChannel<ISCMessage>, server: ZeroRemote, public info: T) { | ||
| // super(channel, server) | ||
| // ZeroServerUser.pool[info.userId] = this | ||
@@ -214,18 +237,21 @@ // } | ||
| // export class LinkServer<T extends PlayerInfo> extends ZeroServer { | ||
| // controlTypePool: { [key: string]: new (...args: any[]) => Control } = {}; | ||
| // serverType = ServerType.LINK | ||
| // modelsChannels: { [key: string]: ClientFotPart[] } = {}; | ||
| // constructor(port: number, private playerInfoType: new (sid: number, uid: number) => PlayerInfo) { | ||
| // /** | ||
| // * 连接服务器 用于连接用户 | ||
| // */ | ||
| // export class ConnectorServer<T extends UserInfo> extends ZeroServer { | ||
| // connectorTypePool: { [key: string]: new (...args: any[]) => Connector } = {}; | ||
| // serverType = ServerType.CONNECT | ||
| // modelsChannels: { [key: string]: ClientFotModule[] } = {}; | ||
| // constructor(port: number, private playerInfoType: new (sid: number, uid: number) => T) { | ||
| // super(port) | ||
| // } | ||
| // clustered() { | ||
| // console.log("创建服务:", this.serverId) | ||
| // new ZeroRemote(new ws.Server({ port: this.port }), (channel) => { | ||
| // let user = new ZeroServerUser(channel, new this.playerInfoType(this.serverId, ZeroServerUser.getAtuoId())) | ||
| // for (const key in this.controlTypePool) { | ||
| // if (Object.prototype.hasOwnProperty.call(this.controlTypePool, key)) { | ||
| // const element = this.controlTypePool[key]; | ||
| // let control = new element(this, user, key); | ||
| // control.start() | ||
| // console.log("创建连接服务:", this.serverId) | ||
| // new ZeroRemote(new ws.Server({ port: this.port }), (channel, server) => { | ||
| // let user = new ZeroServerUser(channel, server, new this.playerInfoType(this.serverId, ZeroServerUser.getAtuoId())) | ||
| // for (const key in this.connectorTypePool) { | ||
| // if (Object.prototype.hasOwnProperty.call(this.connectorTypePool, key)) { | ||
| // const element = this.connectorTypePool[key]; | ||
| // let connector = new element(this, user, key); | ||
| // connector.start() | ||
| // } | ||
@@ -235,7 +261,10 @@ // } | ||
| // }) | ||
| // this.clusterReceiver.add((info) => { | ||
| // if (info.type == ServerType.PART) { | ||
| // let clientFotPart = new ClientFotPart("ws://" + info.url) | ||
| // clientFotPart.link() | ||
| // clientFotPart.receive("", "", this.partMessage.bind(this)) | ||
| // if (info.type == ServerType.MODULE) { | ||
| // let clientFotModule = new ClientFotModule("ws://" + info.url) | ||
| // clientFotModule.link(() => { | ||
| // this.clusterSender.addLine(info.sid) | ||
| // }) | ||
| // clientFotModule.receive("", "", this.partMessage.bind(this)) | ||
@@ -248,6 +277,9 @@ // info.models.forEach((modelsName: string) => { | ||
| // } | ||
| // clinets.push(clientFotPart) | ||
| // clinets.push(clientFotModule) | ||
| // }) | ||
| // } | ||
| // }) | ||
| // this.clusterReceiver.remove((info) => { | ||
| // }) | ||
| // } | ||
@@ -266,6 +298,6 @@ // /** | ||
| // } | ||
| // addControl(controlType: (new (...args: any[]) => Control)) { | ||
| // let route = getRoute(controlType) | ||
| // addConnector(connectorType: (new (...args: any[]) => Connector)) { | ||
| // let route = getRoute(connectorType) | ||
| // if (route) { | ||
| // this.controlTypePool[route] = controlType | ||
| // this.connectorTypePool[route] = connectorType | ||
| // } | ||
@@ -276,6 +308,4 @@ | ||
| // /** | ||
| // * 有个LINK服务器做为客户端连了上来 | ||
| // * 有个CONNECT服务器做为客户端连了上来 | ||
| // * 添加 ping | ||
@@ -285,38 +315,31 @@ // * 添加 callback | ||
| // */ | ||
| // export class LinkVisitor extends Visitor<ICSMessage, ISCMessage> { | ||
| // protected routerPool: { [key: string]: ZeroDispatcher } = {} | ||
| // constructor(channel: IChannel<ISCMessage>, public server: ModelServer) { | ||
| // super(channel) | ||
| // export class ConnectorVisitor extends GameVisitor { | ||
| // clear(): void { | ||
| // console.log("clear") | ||
| // } | ||
| // receive(body: ICSMessage): void { | ||
| // if (body.route == null) { | ||
| // this.channel.send({})//空对象当ping | ||
| // } else { | ||
| // if (body.cbs) { | ||
| // console.log("has cbs") | ||
| // // for (const key in body.cbs) { | ||
| // // if (body.cbs.hasOwnProperty(key)) { | ||
| // // const element = body.cbs[key]; | ||
| // // body.data[key] = (...cbArgs: any[]) => { | ||
| // // this.channel.send({ | ||
| // // code: ReportCode.CALLBACK, | ||
| // // callbackIndex: element, | ||
| // // args: cbArgs | ||
| // // } as any) | ||
| // // } | ||
| // // } | ||
| // // } | ||
| // constructor(channel: IChannel<ISCMessage>, public moduleServer: ModuleServer) { | ||
| // super(channel, moduleServer.connectorServer) | ||
| // } | ||
| // handle(route: string, key: string, data: any, back: (Function | undefined)) { | ||
| // let model: any = ZeroServer.modelPool[route] | ||
| // let method: Function = model[key] | ||
| // if (method) { | ||
| // model.playerInfo = data.p | ||
| // model.isRemote = true | ||
| // let visitor = this.moduleServer.connectorVisitorPool[model.playerInfo.serverId] | ||
| // if (visitor == null) { | ||
| // this.moduleServer.connectorVisitorPool[model.playerInfo.serverId] = this | ||
| // } else { | ||
| // let model: any = ZeroServer.modelPool[body.route] | ||
| // let method: Function = model[body.key] | ||
| // if (method) { | ||
| // model.playerInfo = body.data.p | ||
| // model.isRemote = true | ||
| // let visitor = this.server.linkVisitorPool[model.playerInfo.serverId] | ||
| // if (visitor == null) { | ||
| // this.server.linkVisitorPool[model.playerInfo.serverId] = this | ||
| // } else { | ||
| // // console.log("这里应该为true", visitor == this) | ||
| // } | ||
| // method.apply(model, body.data.a) | ||
| // // console.log("这里应该为true", visitor == this) | ||
| // } | ||
| // let p = method.apply(model, data.a) | ||
| // if (back != null) { | ||
| // if (p instanceof Promise) { | ||
| // p.then((valueThen) => { | ||
| // back!(true, valueThen) | ||
| // }).catch((error) => { | ||
| // back!(false, error) | ||
| // }) | ||
| // } else { | ||
| // back(true, p) | ||
| // } | ||
@@ -326,43 +349,16 @@ // } | ||
| // } | ||
| // getSender<T>(route: string): Sender<T> { | ||
| // return new Proxy<Sender<T>>({} as any, { | ||
| // get: (target: any, p: string, receiver: any) => { | ||
| // return (data: any) => { | ||
| // let sendInfo = { | ||
| // route: route, | ||
| // key: p, | ||
| // data: data | ||
| // } | ||
| // this.channel.send(sendInfo) | ||
| // } | ||
| // } | ||
| // }) | ||
| // } | ||
| // kick() { | ||
| // this.channel.send({ code: ReportCode.KICK }) | ||
| // super.kick() | ||
| // } | ||
| // error(message: string) { | ||
| // this.channel.send({ code: ReportCode.ERROR, error: message }) | ||
| // } | ||
| // clear(): void { | ||
| // } | ||
| // } | ||
| // export class ModelServer extends ZeroServer { | ||
| // serverType = ServerType.PART | ||
| // linkVisitorPool: { [key: string]: LinkVisitor } = {} | ||
| // export class ModuleServer extends ZeroServer { | ||
| // serverType = ServerType.MODULE | ||
| // connectorVisitorPool: { [key: string]: ConnectorVisitor } = {} | ||
| // connectorServer: ZeroRemote | ||
| // constructor(port: number) { | ||
| // super(port) | ||
| // new ZeroRemote(new ws.Server({ port: this.port }), (channel) => { | ||
| // // let user = new playerType(channel) | ||
| // // for (const key in this.controlTypePool) { | ||
| // // if (Object.prototype.hasOwnProperty.call(this.controlTypePool, key)) { | ||
| // // const element = this.controlTypePool[key]; | ||
| // // new element(user, key); | ||
| // // } | ||
| // // } | ||
| // return new LinkVisitor(channel, this) | ||
| // this.connectorServer = new ZeroRemote(new ws.Server({ port: this.port }), (channel, server) => { | ||
| // return new ConnectorVisitor(channel, this) | ||
| // }) | ||
| // } | ||
| // clustered() { | ||
| // console.log("创建功能服务:", this.serverId) | ||
| // } | ||
| // } | ||
@@ -372,4 +368,3 @@ // /** | ||
| // */ | ||
| // export class PlayerInfo { | ||
| // export class UserInfo { | ||
| // name: string | ||
@@ -385,13 +380,32 @@ // /** | ||
| // } | ||
| // /** | ||
| // * 一个服务节点 | ||
| // */ | ||
| // export class ZeroServerNode { | ||
| // constructor(public route: string) { | ||
| // } | ||
| // start() { | ||
| // } | ||
| // } | ||
| // export type ModelSender<T> = { [K in keyof T]: (value: T[K], playerInfo?: PlayerInfo) => void } | ||
| // export class Model<SC = any> { | ||
| // sender: ModelSender<SC> | ||
| // public playerInfo!: PlayerInfo | ||
| // type Parameters<T> = T extends (...args: infer P) => any ? P : never; | ||
| // type ReturnType<T> = T extends (...args: any) => infer R ? R : any; | ||
| // export type ModuleSender<T> = { [K in keyof T]: ((value: Parameters<T[K]>[0], playerInfo?: UserInfo) => Promise<ReturnType<T[K]>>) } | ||
| // /** | ||
| // * | ||
| // */ | ||
| // export class Module<SC = any> extends ZeroServerNode { | ||
| // sender: ModuleSender<SC> | ||
| // public playerInfo!: UserInfo | ||
| // public isRemote: boolean = false | ||
| // public server!: ZeroServer | ||
| // constructor(public route: string) { | ||
| // this.sender = new Proxy<ModelSender<SC>>({} as any, { | ||
| // constructor(route: string) { | ||
| // super(route) | ||
| // this.sender = new Proxy<ModuleSender<SC>>({} as any, { | ||
| // get: (target: any, p: string, receiver: any) => { | ||
| // return (data: any, playerInfo: PlayerInfo | null = null) => { | ||
| // return (data: any, playerInfo: UserInfo | null = null) => { | ||
| // // let playerInfo | ||
@@ -413,3 +427,3 @@ // if (playerInfo == null) { | ||
| // } | ||
| // let visitor = (this.server as ModelServer).linkVisitorPool[this.playerInfo.serverId] | ||
| // let visitor = (this.server as ModuleServer).connectorVisitorPool[this.playerInfo.serverId] | ||
| // visitor.channel.send(sendInfo) | ||
@@ -434,23 +448,24 @@ // } else { | ||
| // } | ||
| // start() { | ||
| // } | ||
| // } | ||
| // export class Control<CS = any> { | ||
| // route!: string | ||
| // export class Connector<CS = any> extends ZeroServerNode { | ||
| // player!: ZeroServerUser<any> | ||
| // receiver: Receiver<CS>; | ||
| // receiver: ReturnReceiver<CS>; | ||
| // private isFree: Boolean = true | ||
| // private lastCallback: (() => Promise<void>)[] = [] | ||
| // constructor(public server: LinkServer<any>, player: ZeroServerUser<any>, route: string) { | ||
| // constructor(public server: ConnectorServer<any>, player: ZeroServerUser<any>, route: string) { | ||
| // super(route) | ||
| // this.player = player; | ||
| // this.route = route; | ||
| // /** | ||
| // * 接收来自己玩家的请求所有做了 防攻击 | ||
| // * 接收来自己玩家的请求所有做了队例处理 防攻击 | ||
| // */ | ||
| // this.receiver = new Proxy<Receiver<CS>>({} as any, { | ||
| // this.receiver = new Proxy<ReturnReceiver<CS>>({} as any, { | ||
| // get: (target: any, p: string, receiver: any) => { | ||
| // return (callback: (value: any) => Promise<void>) => { | ||
| // this.player.getRouter<any>(this.route).on(p, (value) => { | ||
| // let router = (this.player as any).routerPool[route] | ||
| // if (router == null) { | ||
| // router = {}; | ||
| // (this.player as any).routerPool[route] = router | ||
| // } | ||
| // router[p] = (...args: any) => { | ||
| // if (this.lastCallback.length >= 100) { | ||
@@ -460,10 +475,3 @@ // this.player.kick() | ||
| // this.lastCallback.push(() => { | ||
| // if (process.env.ENV == "development") { | ||
| // console.time(route + "." + p) | ||
| // let re = callback(value) | ||
| // console.timeEnd(route + "." + p) | ||
| // return re | ||
| // } else { | ||
| // return callback(value) | ||
| // } | ||
| // return callback.apply(null, args) | ||
| // }) | ||
@@ -475,3 +483,3 @@ // } | ||
| // } | ||
| // }) | ||
| // } | ||
| // } | ||
@@ -485,7 +493,10 @@ // } | ||
| // if (callback) { | ||
| // callback().then((a) => { | ||
| // callback().then(() => { | ||
| // this.nextReceiver() | ||
| // }).catch((err: Error) => { | ||
| // console.log(err) | ||
| // this.player.error(err.message) | ||
| // }).catch((err: Error | string) => { | ||
| // if (typeof err == "string") { | ||
| // this.player.error(err) | ||
| // } else { | ||
| // this.player.error(err.message) | ||
| // } | ||
| // this.nextReceiver() | ||
@@ -498,11 +509,7 @@ // }) | ||
| // } | ||
| // start() { | ||
| // } | ||
| // protected getModel<T extends Model>(modelType: new (...args: any[]) => T): T { | ||
| // protected getModule<T extends Module>(modelType: new (...args: any[]) => T): T { | ||
| // let routeKey = getRoute(modelType) | ||
| // let model = ZeroServer.modelPool[routeKey] | ||
| // if (model == null) { | ||
| // let remotePart = this.getRemotePart(routeKey) | ||
| // let remoteModule = this.getRemoteModule(routeKey) | ||
| // return new Proxy<T>({} as any, { | ||
@@ -512,9 +519,30 @@ // get: (target: any, p: string, receiver: any) => { | ||
| // return (...args: any[]) => { | ||
| // remotePart.send(routeKey, p, { | ||
| // p: this.player.info, | ||
| // a: args | ||
| // console.log("__1__") | ||
| // return new Promise((resolve: (value: any) => void, reject: (reason?: any) => void) => { | ||
| // console.log("__2__") | ||
| // if (remoteModule == null) { | ||
| // remoteModule = this.getRemoteModule(routeKey) | ||
| // } | ||
| // if (remoteModule != null) { | ||
| // console.log("___3_") | ||
| // if (remoteModule.isOpen) { | ||
| // console.log("send") | ||
| // remoteModule.send(routeKey, p, { | ||
| // p: this.player.info, | ||
| // a: args | ||
| // }, (isError: boolean, value: any) => { | ||
| // if (isError) { | ||
| // resolve(value) | ||
| // } else { | ||
| // reject(value) | ||
| // } | ||
| // }) | ||
| // } | ||
| // } else { | ||
| // reject("没有这个服务") | ||
| // } | ||
| // }) | ||
| // } | ||
| // } else { | ||
| // //TODO | ||
| // } | ||
@@ -528,4 +556,2 @@ // }, | ||
| // } else { | ||
| // return new Proxy<T>({} as any, { | ||
@@ -537,3 +563,3 @@ // get: (target: any, p: string, receiver: any) => { | ||
| // let _model: any = model | ||
| // _model[p].apply(model, args) | ||
| // return _model[p].apply(model, args) | ||
| // } | ||
@@ -548,37 +574,21 @@ // } else { | ||
| // }) | ||
| // // console.log(this.player.info) | ||
| // // model.playerInfo = this.player.info | ||
| // // model.isRemote = false | ||
| // // return model as T | ||
| // } | ||
| // } | ||
| // getRemotePart(routeKey: string): ClientFotPart { | ||
| // let remoteModels = this.server.modelsChannels[routeKey] | ||
| // let pick = balancePool[routeKey](this.player, remoteModels.length) | ||
| // return remoteModels[pick] | ||
| // getRemoteModule(routeKey: string): ClientFotModule | null { | ||
| // let remoteModules = this.server.modelsChannels[routeKey] | ||
| // if (remoteModules) { | ||
| // let pick = balancePool[routeKey](this.player, remoteModules.length) | ||
| // return remoteModules[pick] | ||
| // } else { | ||
| // return null | ||
| // } | ||
| // } | ||
| // } | ||
| // // getModel<T extends Model>(router: string, modelType: new (...args: any[]) => T): T { | ||
| // // let single = this.modelPool[router] | ||
| // // if (single == null) { | ||
| // // single = new modelType(router, this.info) | ||
| // // this.modelPool[router] = single | ||
| // // single.start() | ||
| // // } | ||
| // // return <T>single | ||
| // // } | ||
| // export default class ZeroCluster extends ZeroRemote { | ||
| // export default class ZeroCluster extends ZeroRemote<{ change: [] }> { | ||
| // constructor(port: number) { | ||
| // super(new ws.Server({ port: port }), (channel) => { | ||
| // return new ClusterVisitor(channel) | ||
| // super(new ws.Server({ port: port }), (channel, server) => { | ||
| // return new ClusterVisitor(channel, server) | ||
| // }) | ||
| // } | ||
| // } | ||
| // export class ClusterVisitor extends GameVisitor { | ||
@@ -596,5 +606,7 @@ // static serverIndex: number = 0 | ||
| // type!: ServerType; | ||
| // constructor(channel: IChannel<ISCMessage>) { | ||
| // super(channel) | ||
| // this.receiver.init((info) => { | ||
| // lines: Set<number> = new Set() | ||
| // clusterServer!: ZeroCluster | ||
| // start() { | ||
| // this.clusterServer = this.server | ||
| // this.receiver.init(async (info) => { | ||
| // //TODO 这里要加密 | ||
@@ -610,2 +622,5 @@ // let server = ClusterVisitor.pool[info.url] | ||
| // ClusterVisitor.serverIndex++ | ||
| // /** | ||
| // * 给自己发一个完成的信息 | ||
| // */ | ||
| // this.sender.clustered(this.sid) | ||
@@ -640,7 +655,15 @@ // for (const key in ClusterVisitor.pool) { | ||
| // } | ||
| // this.clusterServer.emit("change") | ||
| // }) | ||
| // this.receiver.getPid((info) => { | ||
| // info.callback(ClusterVisitor.serverPid) | ||
| // ClusterVisitor.serverPid++ | ||
| // this.receiver.getPid(async () => { | ||
| // return ClusterVisitor.serverPid++ | ||
| // }) | ||
| // this.receiver.addLine(async (sid) => { | ||
| // this.lines.add(sid) | ||
| // this.clusterServer.emit("change") | ||
| // }) | ||
| // this.receiver.removeLine(async (sid) => { | ||
| // this.lines.delete(sid) | ||
| // this.clusterServer.emit("change") | ||
| // }) | ||
| // } | ||
@@ -656,2 +679,4 @@ // clear(): void { | ||
| // } | ||
| // } | ||
| // } | ||
+238
-200
| "use strict"; | ||
| // import ws from "ws"; | ||
| // import { ZeroDispatcher } from "zero-mvc"; | ||
| // import { address as getLocalIp } from "ip" | ||
| // import ZeroRemote, { GameVisitor, IChannel, ICSMessage, ISCMessage, Receiver, ReportCode, Sender, Visitor, ZeroRemoteClient } from "zero-remote"; | ||
| // type CMType = new (...ary: any[]) => Control | Model | ||
| // type MType = new (...ary: any[]) => Control | Model | ||
| // import ZeroRemote, { GameVisitor, IChannel, ICSMessage, ISCMessage, ReturnReceiver, ZeroRemoteClient } from "zero-remote"; | ||
| // type CMType = new (...ary: any[]) => ZeroServerNode | ||
| // type MType = new (...ary: any[]) => ZeroServerNode | ||
| // export let routeMap: WeakMap<CMType, string> = new WeakMap() | ||
| // export let balancePool: { [key: string]: (player: any, totle: number) => number } = {} | ||
| // /** | ||
| // * 装饰器 | ||
| // * @param key | ||
| // * @returns | ||
| // */ | ||
| // export function route(key: string) { | ||
@@ -15,3 +19,8 @@ // return (type: CMType) => { | ||
| // } | ||
| // export function balance<T extends PlayerInfo>(callback: (player: T, totle: number) => number) { | ||
| // /** | ||
| // * 装饰器 用去落点均衡 | ||
| // * @param callback | ||
| // * @returns | ||
| // */ | ||
| // export function balance<T extends UserInfo>(callback: (player: T, totle: number) => number) { | ||
| // return (type: MType) => { | ||
@@ -24,7 +33,9 @@ // let route = getRoute(type) | ||
| // } | ||
| // // balance(model: ModelType, callback: (player: T) => number): void { | ||
| // // //TODO | ||
| // // } | ||
| // export function getRoute(controlType: new (...args: any[]) => any): string { | ||
| // let route = routeMap.get(controlType) | ||
| // /** | ||
| // * 获取装饰器路由名 | ||
| // * @param connectorType | ||
| // * @returns | ||
| // */ | ||
| // export function getRoute(connectorType: new (...args: any[]) => any): string { | ||
| // let route = routeMap.get(connectorType) | ||
| // if (route) { | ||
@@ -36,10 +47,15 @@ // return route | ||
| // } | ||
| // /** | ||
| // * server类型 | ||
| // */ | ||
| // enum ServerType { | ||
| // PART, | ||
| // LINK, | ||
| // MODULE, | ||
| // CONNECT, | ||
| // SPECIAL, | ||
| // } | ||
| // interface IZeroServerCS { | ||
| // init: { url: string, name: string, models: string[], type: ServerType, pid: number } | ||
| // getPid: { callback: (pid: number) => void } | ||
| // init: (value: { url: string, name: string, models: string[], type: ServerType, pid: number }) => void | ||
| // getPid: () => number | ||
| // addLine: (sid: number) => void | ||
| // removeLine: (sid: number) => void | ||
| // } | ||
@@ -53,13 +69,15 @@ // interface IZeroServerInfo { | ||
| // interface IZeroServerSC { | ||
| // clustered: number | ||
| // add: IZeroServerInfo | ||
| // remove: number | ||
| // // givePid: number | ||
| // clustered: (value: number) => void | ||
| // add: (value: IZeroServerInfo) => void | ||
| // remove: (value: number) => void | ||
| // } | ||
| // type ModelType = new (...arg: any[]) => Model & { route: string } | ||
| // type ModuleType = new (...arg: any[]) => Module & { route: string } | ||
| // /** | ||
| // * server基类 | ||
| // */ | ||
| // export class ZeroServer extends ZeroRemoteClient { | ||
| // serverType: ServerType = ServerType.PART | ||
| // serverType: ServerType = ServerType.MODULE | ||
| // url: string; | ||
| // static mainUrl: string | ||
| // static modelPool: { [key: string]: Model } = {} | ||
| // static modelPool: { [key: string]: Module } = {} | ||
| // models: any = [] | ||
@@ -106,9 +124,7 @@ // clusterSender = this.getSender<IZeroServerCS>("") | ||
| // if (ZeroServer.pid == null) { | ||
| // this.clusterSender.getPid({ | ||
| // callback: (pid) => { | ||
| // if (ZeroServer.pid == null) { | ||
| // ZeroServer.pid = pid | ||
| // } | ||
| // this.sendInit() | ||
| // this.clusterSender.getPid().then((pid) => { | ||
| // if (ZeroServer.pid == null) { | ||
| // ZeroServer.pid = pid | ||
| // } | ||
| // this.sendInit() | ||
| // }) | ||
@@ -122,9 +138,11 @@ // } else { | ||
| // } | ||
| // /** | ||
| // * 生命不息 重连不止 | ||
| // */ | ||
| // popup(): void { | ||
| // console.log("集群失败") | ||
| // } | ||
| // linking(): void { | ||
| // } | ||
| // // main.addControl(ServerKey.LINK, () => { | ||
| // // }) | ||
| // addModel(modelType: ModelType) { | ||
| // addModule(modelType: ModuleType) { | ||
| // let route = getRoute(modelType) | ||
@@ -140,13 +158,10 @@ // if (route) { | ||
| // } | ||
| // // export enum ServerKey { | ||
| // // ENTRY = "entry", | ||
| // // LINK = "link", | ||
| // // ADMIN = "admin" | ||
| // // } | ||
| // /** | ||
| // * 放在Link服务上 | ||
| // * 用来连接Model的线路 | ||
| // * 放在Connector服务上 | ||
| // * 用来连接Module的线路 | ||
| // */ | ||
| // class ClientFotPart extends ZeroRemoteClient { | ||
| // class ClientFotModule extends ZeroRemoteClient { | ||
| // relinkMaxCount = 100 | ||
| // popupReLinkTime: number = 1000 | ||
| // popupReLinkTimeIndex!: NodeJS.Timeout; | ||
| // constructor(public url: string) { | ||
@@ -168,5 +183,15 @@ // super() | ||
| // linked(): void { | ||
| // // throw new Error("Method not implemented."); | ||
| // this.popupReLinkTime = 1000 | ||
| // } | ||
| // popup(): void { | ||
| // console.log("pppppp") | ||
| // for (const key in this.callbackPool) { | ||
| // const element = this.callbackPool[key]; | ||
| // console.log(element) | ||
| // element(false, "服务器被断开") | ||
| // } | ||
| // this.popupReLinkTimeIndex = setTimeout(() => { | ||
| // this.link() | ||
| // this.popupReLinkTime += 1000 | ||
| // }, this.popupReLinkTime) | ||
| // // throw new Error("Method not implemented."); | ||
@@ -178,4 +203,6 @@ // } | ||
| // } | ||
| // export class ZeroServerUser<T extends PlayerInfo = PlayerInfo> extends GameVisitor { | ||
| // // hook: ZeroDispatcher<any> = new ZeroDispatcher() | ||
| // /** | ||
| // * 用户 | ||
| // */ | ||
| // export class ZeroServerUser<T extends UserInfo = UserInfo> extends GameVisitor { | ||
| // static pool: { [key: string]: ZeroServerUser } = {} | ||
@@ -186,26 +213,28 @@ // static index: number = 0 | ||
| // } | ||
| // // protected modelPool: { [key: string]: Model } = {} | ||
| // clear(): void { | ||
| // } | ||
| // constructor(channel: IChannel<ISCMessage>, public info: T) { | ||
| // super(channel) | ||
| // constructor(channel: IChannel<ISCMessage>, server: ZeroRemote, public info: T) { | ||
| // super(channel, server) | ||
| // ZeroServerUser.pool[info.userId] = this | ||
| // } | ||
| // } | ||
| // export class LinkServer<T extends PlayerInfo> extends ZeroServer { | ||
| // controlTypePool: { [key: string]: new (...args: any[]) => Control } = {}; | ||
| // serverType = ServerType.LINK | ||
| // modelsChannels: { [key: string]: ClientFotPart[] } = {}; | ||
| // constructor(port: number, private playerInfoType: new (sid: number, uid: number) => PlayerInfo) { | ||
| // /** | ||
| // * 连接服务器 用于连接用户 | ||
| // */ | ||
| // export class ConnectorServer<T extends UserInfo> extends ZeroServer { | ||
| // connectorTypePool: { [key: string]: new (...args: any[]) => Connector } = {}; | ||
| // serverType = ServerType.CONNECT | ||
| // modelsChannels: { [key: string]: ClientFotModule[] } = {}; | ||
| // constructor(port: number, private playerInfoType: new (sid: number, uid: number) => T) { | ||
| // super(port) | ||
| // } | ||
| // clustered() { | ||
| // console.log("创建服务:", this.serverId) | ||
| // new ZeroRemote(new ws.Server({ port: this.port }), (channel) => { | ||
| // let user = new ZeroServerUser(channel, new this.playerInfoType(this.serverId, ZeroServerUser.getAtuoId())) | ||
| // for (const key in this.controlTypePool) { | ||
| // if (Object.prototype.hasOwnProperty.call(this.controlTypePool, key)) { | ||
| // const element = this.controlTypePool[key]; | ||
| // let control = new element(this, user, key); | ||
| // control.start() | ||
| // console.log("创建连接服务:", this.serverId) | ||
| // new ZeroRemote(new ws.Server({ port: this.port }), (channel, server) => { | ||
| // let user = new ZeroServerUser(channel, server, new this.playerInfoType(this.serverId, ZeroServerUser.getAtuoId())) | ||
| // for (const key in this.connectorTypePool) { | ||
| // if (Object.prototype.hasOwnProperty.call(this.connectorTypePool, key)) { | ||
| // const element = this.connectorTypePool[key]; | ||
| // let connector = new element(this, user, key); | ||
| // connector.start() | ||
| // } | ||
@@ -216,6 +245,8 @@ // } | ||
| // this.clusterReceiver.add((info) => { | ||
| // if (info.type == ServerType.PART) { | ||
| // let clientFotPart = new ClientFotPart("ws://" + info.url) | ||
| // clientFotPart.link() | ||
| // clientFotPart.receive("", "", this.partMessage.bind(this)) | ||
| // if (info.type == ServerType.MODULE) { | ||
| // let clientFotModule = new ClientFotModule("ws://" + info.url) | ||
| // clientFotModule.link(() => { | ||
| // this.clusterSender.addLine(info.sid) | ||
| // }) | ||
| // clientFotModule.receive("", "", this.partMessage.bind(this)) | ||
| // info.models.forEach((modelsName: string) => { | ||
@@ -227,6 +258,8 @@ // let clinets = this.modelsChannels[modelsName] | ||
| // } | ||
| // clinets.push(clientFotPart) | ||
| // clinets.push(clientFotModule) | ||
| // }) | ||
| // } | ||
| // }) | ||
| // this.clusterReceiver.remove((info) => { | ||
| // }) | ||
| // } | ||
@@ -245,6 +278,6 @@ // /** | ||
| // } | ||
| // addControl(controlType: (new (...args: any[]) => Control)) { | ||
| // let route = getRoute(controlType) | ||
| // addConnector(connectorType: (new (...args: any[]) => Connector)) { | ||
| // let route = getRoute(connectorType) | ||
| // if (route) { | ||
| // this.controlTypePool[route] = controlType | ||
| // this.connectorTypePool[route] = connectorType | ||
| // } | ||
@@ -254,3 +287,3 @@ // } | ||
| // /** | ||
| // * 有个LINK服务器做为客户端连了上来 | ||
| // * 有个CONNECT服务器做为客户端连了上来 | ||
| // * 添加 ping | ||
@@ -260,38 +293,31 @@ // * 添加 callback | ||
| // */ | ||
| // export class LinkVisitor extends Visitor<ICSMessage, ISCMessage> { | ||
| // protected routerPool: { [key: string]: ZeroDispatcher } = {} | ||
| // constructor(channel: IChannel<ISCMessage>, public server: ModelServer) { | ||
| // super(channel) | ||
| // export class ConnectorVisitor extends GameVisitor { | ||
| // clear(): void { | ||
| // console.log("clear") | ||
| // } | ||
| // receive(body: ICSMessage): void { | ||
| // if (body.route == null) { | ||
| // this.channel.send({})//空对象当ping | ||
| // } else { | ||
| // if (body.cbs) { | ||
| // console.log("has cbs") | ||
| // // for (const key in body.cbs) { | ||
| // // if (body.cbs.hasOwnProperty(key)) { | ||
| // // const element = body.cbs[key]; | ||
| // // body.data[key] = (...cbArgs: any[]) => { | ||
| // // this.channel.send({ | ||
| // // code: ReportCode.CALLBACK, | ||
| // // callbackIndex: element, | ||
| // // args: cbArgs | ||
| // // } as any) | ||
| // // } | ||
| // // } | ||
| // // } | ||
| // constructor(channel: IChannel<ISCMessage>, public moduleServer: ModuleServer) { | ||
| // super(channel, moduleServer.connectorServer) | ||
| // } | ||
| // handle(route: string, key: string, data: any, back: (Function | undefined)) { | ||
| // let model: any = ZeroServer.modelPool[route] | ||
| // let method: Function = model[key] | ||
| // if (method) { | ||
| // model.playerInfo = data.p | ||
| // model.isRemote = true | ||
| // let visitor = this.moduleServer.connectorVisitorPool[model.playerInfo.serverId] | ||
| // if (visitor == null) { | ||
| // this.moduleServer.connectorVisitorPool[model.playerInfo.serverId] = this | ||
| // } else { | ||
| // let model: any = ZeroServer.modelPool[body.route] | ||
| // let method: Function = model[body.key] | ||
| // if (method) { | ||
| // model.playerInfo = body.data.p | ||
| // model.isRemote = true | ||
| // let visitor = this.server.linkVisitorPool[model.playerInfo.serverId] | ||
| // if (visitor == null) { | ||
| // this.server.linkVisitorPool[model.playerInfo.serverId] = this | ||
| // } else { | ||
| // // console.log("这里应该为true", visitor == this) | ||
| // } | ||
| // method.apply(model, body.data.a) | ||
| // // console.log("这里应该为true", visitor == this) | ||
| // } | ||
| // let p = method.apply(model, data.a) | ||
| // if (back != null) { | ||
| // if (p instanceof Promise) { | ||
| // p.then((valueThen) => { | ||
| // back!(true, valueThen) | ||
| // }).catch((error) => { | ||
| // back!(false, error) | ||
| // }) | ||
| // } else { | ||
| // back(true, p) | ||
| // } | ||
@@ -301,42 +327,16 @@ // } | ||
| // } | ||
| // getSender<T>(route: string): Sender<T> { | ||
| // return new Proxy<Sender<T>>({} as any, { | ||
| // get: (target: any, p: string, receiver: any) => { | ||
| // return (data: any) => { | ||
| // let sendInfo = { | ||
| // route: route, | ||
| // key: p, | ||
| // data: data | ||
| // } | ||
| // this.channel.send(sendInfo) | ||
| // } | ||
| // } | ||
| // }) | ||
| // } | ||
| // kick() { | ||
| // this.channel.send({ code: ReportCode.KICK }) | ||
| // super.kick() | ||
| // } | ||
| // error(message: string) { | ||
| // this.channel.send({ code: ReportCode.ERROR, error: message }) | ||
| // } | ||
| // clear(): void { | ||
| // } | ||
| // } | ||
| // export class ModelServer extends ZeroServer { | ||
| // serverType = ServerType.PART | ||
| // linkVisitorPool: { [key: string]: LinkVisitor } = {} | ||
| // export class ModuleServer extends ZeroServer { | ||
| // serverType = ServerType.MODULE | ||
| // connectorVisitorPool: { [key: string]: ConnectorVisitor } = {} | ||
| // connectorServer: ZeroRemote | ||
| // constructor(port: number) { | ||
| // super(port) | ||
| // new ZeroRemote(new ws.Server({ port: this.port }), (channel) => { | ||
| // // let user = new playerType(channel) | ||
| // // for (const key in this.controlTypePool) { | ||
| // // if (Object.prototype.hasOwnProperty.call(this.controlTypePool, key)) { | ||
| // // const element = this.controlTypePool[key]; | ||
| // // new element(user, key); | ||
| // // } | ||
| // // } | ||
| // return new LinkVisitor(channel, this) | ||
| // this.connectorServer = new ZeroRemote(new ws.Server({ port: this.port }), (channel, server) => { | ||
| // return new ConnectorVisitor(channel, this) | ||
| // }) | ||
| // } | ||
| // clustered() { | ||
| // console.log("创建功能服务:", this.serverId) | ||
| // } | ||
| // } | ||
@@ -346,3 +346,3 @@ // /** | ||
| // */ | ||
| // export class PlayerInfo { | ||
| // export class UserInfo { | ||
| // name: string | ||
@@ -358,12 +358,27 @@ // /** | ||
| // } | ||
| // export type ModelSender<T> = { [K in keyof T]: (value: T[K], playerInfo?: PlayerInfo) => void } | ||
| // export class Model<SC = any> { | ||
| // sender: ModelSender<SC> | ||
| // public playerInfo!: PlayerInfo | ||
| // /** | ||
| // * 一个服务节点 | ||
| // */ | ||
| // export class ZeroServerNode { | ||
| // constructor(public route: string) { | ||
| // } | ||
| // start() { | ||
| // } | ||
| // } | ||
| // type Parameters<T> = T extends (...args: infer P) => any ? P : never; | ||
| // type ReturnType<T> = T extends (...args: any) => infer R ? R : any; | ||
| // export type ModuleSender<T> = { [K in keyof T]: ((value: Parameters<T[K]>[0], playerInfo?: UserInfo) => Promise<ReturnType<T[K]>>) } | ||
| // /** | ||
| // * | ||
| // */ | ||
| // export class Module<SC = any> extends ZeroServerNode { | ||
| // sender: ModuleSender<SC> | ||
| // public playerInfo!: UserInfo | ||
| // public isRemote: boolean = false | ||
| // public server!: ZeroServer | ||
| // constructor(public route: string) { | ||
| // this.sender = new Proxy<ModelSender<SC>>({} as any, { | ||
| // constructor(route: string) { | ||
| // super(route) | ||
| // this.sender = new Proxy<ModuleSender<SC>>({} as any, { | ||
| // get: (target: any, p: string, receiver: any) => { | ||
| // return (data: any, playerInfo: PlayerInfo | null = null) => { | ||
| // return (data: any, playerInfo: UserInfo | null = null) => { | ||
| // // let playerInfo | ||
@@ -385,3 +400,3 @@ // if (playerInfo == null) { | ||
| // } | ||
| // let visitor = (this.server as ModelServer).linkVisitorPool[this.playerInfo.serverId] | ||
| // let visitor = (this.server as ModuleServer).connectorVisitorPool[this.playerInfo.serverId] | ||
| // visitor.channel.send(sendInfo) | ||
@@ -406,21 +421,24 @@ // } else { | ||
| // } | ||
| // start() { | ||
| // } | ||
| // } | ||
| // export class Control<CS = any> { | ||
| // route!: string | ||
| // export class Connector<CS = any> extends ZeroServerNode { | ||
| // player!: ZeroServerUser<any> | ||
| // receiver: Receiver<CS>; | ||
| // receiver: ReturnReceiver<CS>; | ||
| // private isFree: Boolean = true | ||
| // private lastCallback: (() => Promise<void>)[] = [] | ||
| // constructor(public server: LinkServer<any>, player: ZeroServerUser<any>, route: string) { | ||
| // constructor(public server: ConnectorServer<any>, player: ZeroServerUser<any>, route: string) { | ||
| // super(route) | ||
| // this.player = player; | ||
| // this.route = route; | ||
| // /** | ||
| // * 接收来自己玩家的请求所有做了 防攻击 | ||
| // * 接收来自己玩家的请求所有做了队例处理 防攻击 | ||
| // */ | ||
| // this.receiver = new Proxy<Receiver<CS>>({} as any, { | ||
| // this.receiver = new Proxy<ReturnReceiver<CS>>({} as any, { | ||
| // get: (target: any, p: string, receiver: any) => { | ||
| // return (callback: (value: any) => Promise<void>) => { | ||
| // this.player.getRouter<any>(this.route).on(p, (value) => { | ||
| // let router = (this.player as any).routerPool[route] | ||
| // if (router == null) { | ||
| // router = {}; | ||
| // (this.player as any).routerPool[route] = router | ||
| // } | ||
| // router[p] = (...args: any) => { | ||
| // if (this.lastCallback.length >= 100) { | ||
@@ -430,10 +448,3 @@ // this.player.kick() | ||
| // this.lastCallback.push(() => { | ||
| // if (process.env.ENV == "development") { | ||
| // console.time(route + "." + p) | ||
| // let re = callback(value) | ||
| // console.timeEnd(route + "." + p) | ||
| // return re | ||
| // } else { | ||
| // return callback(value) | ||
| // } | ||
| // return callback.apply(null, args) | ||
| // }) | ||
@@ -445,3 +456,3 @@ // } | ||
| // } | ||
| // }) | ||
| // } | ||
| // } | ||
@@ -455,7 +466,10 @@ // } | ||
| // if (callback) { | ||
| // callback().then((a) => { | ||
| // callback().then(() => { | ||
| // this.nextReceiver() | ||
| // }).catch((err: Error) => { | ||
| // console.log(err) | ||
| // this.player.error(err.message) | ||
| // }).catch((err: Error | string) => { | ||
| // if (typeof err == "string") { | ||
| // this.player.error(err) | ||
| // } else { | ||
| // this.player.error(err.message) | ||
| // } | ||
| // this.nextReceiver() | ||
@@ -468,9 +482,7 @@ // }) | ||
| // } | ||
| // start() { | ||
| // } | ||
| // protected getModel<T extends Model>(modelType: new (...args: any[]) => T): T { | ||
| // protected getModule<T extends Module>(modelType: new (...args: any[]) => T): T { | ||
| // let routeKey = getRoute(modelType) | ||
| // let model = ZeroServer.modelPool[routeKey] | ||
| // if (model == null) { | ||
| // let remotePart = this.getRemotePart(routeKey) | ||
| // let remoteModule = this.getRemoteModule(routeKey) | ||
| // return new Proxy<T>({} as any, { | ||
@@ -480,8 +492,30 @@ // get: (target: any, p: string, receiver: any) => { | ||
| // return (...args: any[]) => { | ||
| // remotePart.send(routeKey, p, { | ||
| // p: this.player.info, | ||
| // a: args | ||
| // console.log("__1__") | ||
| // return new Promise((resolve: (value: any) => void, reject: (reason?: any) => void) => { | ||
| // console.log("__2__") | ||
| // if (remoteModule == null) { | ||
| // remoteModule = this.getRemoteModule(routeKey) | ||
| // } | ||
| // if (remoteModule != null) { | ||
| // console.log("___3_") | ||
| // if (remoteModule.isOpen) { | ||
| // console.log("send") | ||
| // remoteModule.send(routeKey, p, { | ||
| // p: this.player.info, | ||
| // a: args | ||
| // }, (isError: boolean, value: any) => { | ||
| // if (isError) { | ||
| // resolve(value) | ||
| // } else { | ||
| // reject(value) | ||
| // } | ||
| // }) | ||
| // } | ||
| // } else { | ||
| // reject("没有这个服务") | ||
| // } | ||
| // }) | ||
| // } | ||
| // } else { | ||
| // //TODO | ||
| // } | ||
@@ -501,3 +535,3 @@ // }, | ||
| // let _model: any = model | ||
| // _model[p].apply(model, args) | ||
| // return _model[p].apply(model, args) | ||
| // } | ||
@@ -512,27 +546,18 @@ // } else { | ||
| // }) | ||
| // // console.log(this.player.info) | ||
| // // model.playerInfo = this.player.info | ||
| // // model.isRemote = false | ||
| // // return model as T | ||
| // } | ||
| // } | ||
| // getRemotePart(routeKey: string): ClientFotPart { | ||
| // let remoteModels = this.server.modelsChannels[routeKey] | ||
| // let pick = balancePool[routeKey](this.player, remoteModels.length) | ||
| // return remoteModels[pick] | ||
| // getRemoteModule(routeKey: string): ClientFotModule | null { | ||
| // let remoteModules = this.server.modelsChannels[routeKey] | ||
| // if (remoteModules) { | ||
| // let pick = balancePool[routeKey](this.player, remoteModules.length) | ||
| // return remoteModules[pick] | ||
| // } else { | ||
| // return null | ||
| // } | ||
| // } | ||
| // } | ||
| // // getModel<T extends Model>(router: string, modelType: new (...args: any[]) => T): T { | ||
| // // let single = this.modelPool[router] | ||
| // // if (single == null) { | ||
| // // single = new modelType(router, this.info) | ||
| // // this.modelPool[router] = single | ||
| // // single.start() | ||
| // // } | ||
| // // return <T>single | ||
| // // } | ||
| // export default class ZeroCluster extends ZeroRemote { | ||
| // export default class ZeroCluster extends ZeroRemote<{ change: [] }> { | ||
| // constructor(port: number) { | ||
| // super(new ws.Server({ port: port }), (channel) => { | ||
| // return new ClusterVisitor(channel) | ||
| // super(new ws.Server({ port: port }), (channel, server) => { | ||
| // return new ClusterVisitor(channel, server) | ||
| // }) | ||
@@ -553,5 +578,7 @@ // } | ||
| // type!: ServerType; | ||
| // constructor(channel: IChannel<ISCMessage>) { | ||
| // super(channel) | ||
| // this.receiver.init((info) => { | ||
| // lines: Set<number> = new Set() | ||
| // clusterServer!: ZeroCluster | ||
| // start() { | ||
| // this.clusterServer = this.server | ||
| // this.receiver.init(async (info) => { | ||
| // //TODO 这里要加密 | ||
@@ -567,2 +594,5 @@ // let server = ClusterVisitor.pool[info.url] | ||
| // ClusterVisitor.serverIndex++ | ||
| // /** | ||
| // * 给自己发一个完成的信息 | ||
| // */ | ||
| // this.sender.clustered(this.sid) | ||
@@ -597,7 +627,15 @@ // for (const key in ClusterVisitor.pool) { | ||
| // } | ||
| // this.clusterServer.emit("change") | ||
| // }) | ||
| // this.receiver.getPid((info) => { | ||
| // info.callback(ClusterVisitor.serverPid) | ||
| // ClusterVisitor.serverPid++ | ||
| // this.receiver.getPid(async () => { | ||
| // return ClusterVisitor.serverPid++ | ||
| // }) | ||
| // this.receiver.addLine(async (sid) => { | ||
| // this.lines.add(sid) | ||
| // this.clusterServer.emit("change") | ||
| // }) | ||
| // this.receiver.removeLine(async (sid) => { | ||
| // this.lines.delete(sid) | ||
| // this.clusterServer.emit("change") | ||
| // }) | ||
| // } | ||
@@ -604,0 +642,0 @@ // clear(): void { |
+41
-45
@@ -225,10 +225,2 @@ "use strict"; | ||
| } | ||
| // getRouter<T>(route: string): ZeroDispatcher<{ [P in keyof T]: [T[P]] }> { | ||
| // let router = this.routerPool[route] | ||
| // if (router == null) { | ||
| // router = new ZeroDispatcher() | ||
| // this.routerPool[route] = router | ||
| // } | ||
| // return router | ||
| // } | ||
| getReceiver(route) { | ||
@@ -280,6 +272,8 @@ return new Proxy({}, { | ||
| */ | ||
| class ZeroRemoteClient { | ||
| constructor() { | ||
| class ZeroRemoteClient extends zero_mvc_1.ZeroDispatcher { | ||
| constructor(url) { | ||
| super(); | ||
| this.url = url; | ||
| this.relinkMaxCount = 3; | ||
| this.relinkTime = 5000; | ||
| this.relinkTime = 2000; | ||
| /** | ||
@@ -291,3 +285,3 @@ * 回调函数缓存个数 | ||
| this.heartbeatTime = 5; //整秒=5 | ||
| this._mask = false; | ||
| this.pack = exports.jsonPack; | ||
| this.isOpen = false; | ||
@@ -301,10 +295,10 @@ this.isBusy = false; | ||
| this.pingIndex = 0; | ||
| this._mask = false; | ||
| setInterval(() => { | ||
| if (this.isOpen) { | ||
| if (this.pingIndex == this.heartbeatTime + this.outTime) { | ||
| console.log(this.mask); | ||
| this.mask = true; | ||
| } | ||
| else if (this.pingIndex == this.heartbeatTime) { | ||
| this.ws.send(this.stringify({})); | ||
| this.ws.send(this.pack.stringify({})); | ||
| } | ||
@@ -322,6 +316,8 @@ this.pingIndex++; | ||
| if (this._mask) { | ||
| this.showMask(); | ||
| // this.showMask() | ||
| this.emit("showMask"); | ||
| } | ||
| else { | ||
| this.hideMask(); | ||
| // this.hideMask() | ||
| this.emit("hideMask"); | ||
| } | ||
@@ -331,8 +327,2 @@ } | ||
| /** | ||
| * 所有通信当作心跳,没有通信时补发心跳 | ||
| */ | ||
| heartbeat() { | ||
| } | ||
| ; | ||
| /** | ||
| * 本条结构协议 | ||
@@ -347,12 +337,10 @@ * @param route 要发送的路由名 | ||
| return new Promise((resolve, reject) => { | ||
| if (this.isOpen) { | ||
| this.send(route, p, data, (isError, value) => { | ||
| if (isError) { | ||
| resolve(value); | ||
| } | ||
| else { | ||
| reject(); | ||
| } | ||
| }); | ||
| } | ||
| this.send(route, p, data, (isError, value) => { | ||
| if (isError) { | ||
| resolve(value); | ||
| } | ||
| else { | ||
| reject(value); | ||
| } | ||
| }); | ||
| }); | ||
@@ -397,10 +385,12 @@ }; | ||
| } | ||
| this.ws.send(this.stringify({ route: route, key: key, data: data, cbs: cbs, index: index })); | ||
| this.ws.send(this.pack.stringify({ route: route, key: key, data: data, cbs: cbs, index: index })); | ||
| } | ||
| else { | ||
| console.log("弱网环境,send信息被忽略" + route); | ||
| // console.log("弱网环境,send信息被忽略" + route) | ||
| callback(false, "弱网环境,send信息被忽略"); | ||
| } | ||
| } | ||
| else { | ||
| console.log("游戏服务器未连接,send信息被忽略" + route); | ||
| // console.log("游戏服务器未连接,send信息被忽略" + route) | ||
| callback(false, "服务器未连接,send信息被忽略"); | ||
| } | ||
@@ -412,3 +402,3 @@ } | ||
| reLink() { | ||
| if (this.relinkCount > (this.relinkMaxCount)) { | ||
| if (this.relinkCount < this.relinkMaxCount) { | ||
| setTimeout(() => { | ||
@@ -421,3 +411,3 @@ this.relinkCount++; | ||
| this.relinkCount = 0; | ||
| this.popup(); | ||
| this.emit("popup"); | ||
| } | ||
@@ -428,3 +418,3 @@ } | ||
| try { | ||
| obj = JSON.parse(value); | ||
| obj = this.pack.parse(value); | ||
| } | ||
@@ -458,11 +448,15 @@ catch (_a) { | ||
| else { | ||
| this.error(obj.error); | ||
| console.log(obj); | ||
| this.emit("error", obj.error); | ||
| } | ||
| } | ||
| pong() { | ||
| this.heartbeat(); | ||
| this.emit("heartbeat"); | ||
| this.pingIndex = 0; | ||
| this.mask = false; | ||
| } | ||
| /** | ||
| * 这个callback 在重连里不会触发 | ||
| * 如需要触发 请使用 on("linked") | ||
| * @param callback | ||
| */ | ||
| link(callback) { | ||
@@ -480,11 +474,11 @@ this.isRelink = true; | ||
| this.isBusy = true; | ||
| this.linking(); | ||
| this.emit("linking"); | ||
| this.ws.onerror = () => { | ||
| this.isRelink = false; | ||
| this.popup(); | ||
| this.emit("error", "网络出错"); | ||
| }; | ||
| this.ws.onopen = () => { | ||
| this.mask = false; | ||
| this.relinkCount = 0; | ||
| this.isOpen = true; | ||
| this.linked(); | ||
| this.emit("linked"); | ||
| if (callback) { | ||
@@ -495,4 +489,6 @@ callback(); | ||
| this.ws.onclose = () => { | ||
| this.mask = true; | ||
| this.isOpen = false; | ||
| this.isBusy = false; | ||
| this.emit("suspend"); | ||
| if (this.isRelink) { | ||
@@ -499,0 +495,0 @@ this.reLink(); |
+61
-66
@@ -248,12 +248,2 @@ | ||
| } | ||
| // getRouter<T>(route: string): ZeroDispatcher<{ [P in keyof T]: [T[P]] }> { | ||
| // let router = this.routerPool[route] | ||
| // if (router == null) { | ||
| // router = new ZeroDispatcher() | ||
| // this.routerPool[route] = router | ||
| // } | ||
| // return router | ||
| // } | ||
| getReceiver<T>(route: string): ReturnReceiver<T> { | ||
@@ -273,3 +263,2 @@ return new Proxy<ReturnReceiver<T>>({} as any, { | ||
| } | ||
| getSender<T>(route: string): Sender<T> { | ||
@@ -298,3 +287,2 @@ return new Proxy<Sender<T>>({} as any, { | ||
| type Parameters<T> = T extends (...args: infer P) => any ? P : never; | ||
@@ -318,5 +306,14 @@ type ReturnType<T> = T extends (...args: any) => infer R ? R : any; | ||
| export abstract class ZeroRemoteClient { | ||
| export class ZeroRemoteClient extends ZeroDispatcher<{ | ||
| showMask: [] | ||
| hideMask: [] | ||
| error: [string?] | ||
| popup: [] | ||
| heartbeat: [] | ||
| linking: [] | ||
| linked: [] | ||
| suspend: [] | ||
| }> { | ||
| relinkMaxCount: number = 3 | ||
| relinkTime: number = 5000 | ||
| relinkTime: number = 2000 | ||
| /** | ||
@@ -328,2 +325,14 @@ * 回调函数缓存个数 | ||
| heartbeatTime: number = 5;//整秒=5 | ||
| public pack: IPack = jsonPack | ||
| isOpen = false | ||
| isBusy = false | ||
| private isRelink: boolean = true | ||
| private relinkCount: number = 0 | ||
| protected ws!: WebSocket; | ||
| protected router: ZeroDispatcher<any> = new ZeroDispatcher(); | ||
| protected callbackPool: { [key: number]: Function } = {} | ||
| private callbackIndex: number = 0 | ||
| private pingIndex: number = 0 | ||
| private _mask: boolean = false | ||
@@ -337,5 +346,7 @@ public get mask(): boolean { | ||
| if (this._mask) { | ||
| this.showMask() | ||
| // this.showMask() | ||
| this.emit("showMask") | ||
| } else { | ||
| this.hideMask() | ||
| // this.hideMask() | ||
| this.emit("hideMask") | ||
| } | ||
@@ -345,33 +356,10 @@ | ||
| } | ||
| abstract showMask(): void | ||
| abstract hideMask(): void | ||
| abstract url: string | ||
| abstract stringify(value: ISCMessage | ICSMessage): string | ||
| abstract error(error: any): void; | ||
| abstract linked(): void; | ||
| abstract popup(): void; | ||
| abstract linking(): void; | ||
| /** | ||
| * 所有通信当作心跳,没有通信时补发心跳 | ||
| */ | ||
| protected heartbeat(): void { | ||
| }; | ||
| isOpen = false | ||
| isBusy = false | ||
| private isRelink: boolean = true | ||
| private relinkCount: number = 0 | ||
| protected ws!: WebSocket; | ||
| protected router: ZeroDispatcher<any> = new ZeroDispatcher(); | ||
| private callbackPool: { [key: number]: Function } = {} | ||
| private callbackIndex: number = 0 | ||
| private pingIndex: number = 0 | ||
| constructor() { | ||
| constructor(public url: string) { | ||
| super() | ||
| setInterval(() => { | ||
| if (this.isOpen) { | ||
| if (this.pingIndex == this.heartbeatTime + this.outTime) { | ||
| console.log(this.mask) | ||
| this.mask = true | ||
| } else if (this.pingIndex == this.heartbeatTime) { | ||
| this.ws.send(this.stringify({})) | ||
| this.ws.send(this.pack.stringify({})) | ||
| } | ||
@@ -391,12 +379,10 @@ this.pingIndex++ | ||
| return (data: any) => { | ||
| return new Promise((resolve: (value: any) => void, reject: () => void) => { | ||
| if (this.isOpen) { | ||
| this.send(route, p, data, (isError: boolean, value: any) => { | ||
| if (isError) { | ||
| resolve(value) | ||
| } else { | ||
| reject() | ||
| } | ||
| }) | ||
| } | ||
| return new Promise((resolve: (value: any) => void, reject: (reason?: any) => void) => { | ||
| this.send(route, p, data, (isError: boolean, value: any) => { | ||
| if (isError) { | ||
| resolve(value) | ||
| } else { | ||
| reject(value) | ||
| } | ||
| }) | ||
| }) | ||
@@ -416,3 +402,3 @@ } | ||
| } | ||
| send(route: string, key: string, data: any, callback: (isError: boolean, value: any) => void) { | ||
| send(route: string, key: string, data: any, callback: (isOK: boolean, value: any) => void) { | ||
| if (this.isOpen) { | ||
@@ -442,10 +428,12 @@ if (!this.mask) { | ||
| } | ||
| this.ws.send(this.stringify( | ||
| this.ws.send(this.pack.stringify( | ||
| { route: route, key: key, data: data, cbs: cbs, index: index } | ||
| )) | ||
| } else { | ||
| console.log("弱网环境,send信息被忽略" + route) | ||
| // console.log("弱网环境,send信息被忽略" + route) | ||
| callback(false, "弱网环境,send信息被忽略") | ||
| } | ||
| } else { | ||
| console.log("游戏服务器未连接,send信息被忽略" + route) | ||
| // console.log("游戏服务器未连接,send信息被忽略" + route) | ||
| callback(false, "服务器未连接,send信息被忽略") | ||
| } | ||
@@ -457,3 +445,3 @@ } | ||
| protected reLink() { | ||
| if (this.relinkCount > (this.relinkMaxCount)) { | ||
| if (this.relinkCount < this.relinkMaxCount) { | ||
| setTimeout(() => { | ||
@@ -465,3 +453,3 @@ this.relinkCount++ | ||
| this.relinkCount = 0 | ||
| this.popup() | ||
| this.emit("popup") | ||
| } | ||
@@ -472,3 +460,3 @@ } | ||
| try { | ||
| obj = JSON.parse(value) | ||
| obj = this.pack.parse(value) | ||
| } catch { | ||
@@ -497,11 +485,16 @@ throw new Error("服务器数据无法序列化") | ||
| } else { | ||
| this.error(obj.error) | ||
| console.log(obj) | ||
| this.emit("error", obj.error) | ||
| } | ||
| } | ||
| protected pong() { | ||
| this.heartbeat() | ||
| this.emit("heartbeat") | ||
| this.pingIndex = 0 | ||
| this.mask = false | ||
| } | ||
| /** | ||
| * 这个callback 在重连里不会触发 | ||
| * 如需要触发 请使用 on("linked") | ||
| * @param callback | ||
| */ | ||
| link(callback?: () => void) { | ||
@@ -517,11 +510,11 @@ this.isRelink = true | ||
| this.isBusy = true | ||
| this.linking() | ||
| this.emit("linking") | ||
| this.ws.onerror = () => { | ||
| this.isRelink = false | ||
| this.popup() | ||
| this.emit("error", "网络出错") | ||
| } | ||
| this.ws.onopen = () => { | ||
| this.mask = false | ||
| this.relinkCount = 0 | ||
| this.isOpen = true | ||
| this.linked() | ||
| this.emit("linked") | ||
| if (callback) { | ||
@@ -532,4 +525,6 @@ callback() | ||
| this.ws.onclose = () => { | ||
| this.mask = true | ||
| this.isOpen = false | ||
| this.isBusy = false | ||
| this.emit("suspend") | ||
| if (this.isRelink) { | ||
@@ -536,0 +531,0 @@ this.reLink() |
+3
-2
| { | ||
| "name": "zero-remote", | ||
| "version": "1.1.1", | ||
| "version": "1.1.2", | ||
| "description": "常连接网络", | ||
| "main": "dist/index.js", | ||
| "scripts": { | ||
| "demo": "ts-node-dev demo.ts" | ||
| "demo1": "ts-node-dev demo1.ts", | ||
| "demo2": "ts-node-dev demo2.ts" | ||
| }, | ||
@@ -9,0 +10,0 @@ "keywords": [ |
-170
| import ZeroRemote, { GameVisitor, ICSMessage, ISCMessage, ZeroRemoteClient } from "./index" | ||
| import ws from "ws" | ||
| // interface T1 { | ||
| // fuck: (value: { a: number, b: Function }) => number | ||
| // } | ||
| interface T2 { | ||
| getPid: () => number | ||
| } | ||
| class User extends GameVisitor { | ||
| clear(): void { | ||
| } | ||
| start() { | ||
| this.getReceiver<T2>("fuck").getPid(async () => { | ||
| console.log("_____________-") | ||
| return 0 | ||
| }) | ||
| // this.getReceiver<T1>("fuck").on("fuck", (data) => { | ||
| // setTimeout(() => { | ||
| // data.b() | ||
| // }, 2000); | ||
| // }) | ||
| // this.getReceiver<T1>("fuck").fuck((data) => { | ||
| // console.log("___________") | ||
| // setTimeout(() => { | ||
| // data.b() | ||
| // }, 2000); | ||
| // return 5 | ||
| // }) | ||
| // this.getSender<T1>("tt").fuck({ | ||
| // a: 1, b: () => { | ||
| // console.log("____nonono") | ||
| // } | ||
| // }) | ||
| } | ||
| } | ||
| let zn = new ZeroRemote(new ws.Server({ port: 8888 }), (carer, server) => { | ||
| return new User(carer, server) | ||
| }) | ||
| class Client extends ZeroRemoteClient { | ||
| url: string = "ws://localhost:8888" | ||
| stringify(value: ISCMessage | ICSMessage): string { | ||
| return JSON.stringify(value) | ||
| } | ||
| showMask(): void { | ||
| console.log("showMask") | ||
| } | ||
| hideMask(): void { | ||
| console.log("hideMask") | ||
| } | ||
| error(error: any): void { | ||
| console.log(error) | ||
| console.log("error") | ||
| } | ||
| linked(): void { | ||
| console.log("linked") | ||
| } | ||
| popup(): void { | ||
| console.log("popup") | ||
| } | ||
| linking(): void { | ||
| console.log("linking") | ||
| } | ||
| } | ||
| setTimeout(() => { | ||
| let cc = new Client() | ||
| // cc.getReceiver<T1>("tt").fuck((a) => { | ||
| // console.log("+++") | ||
| // console.log(a) | ||
| // }) | ||
| cc.link(async () => { | ||
| // console.log("link+++1") | ||
| let sender = cc.getSender<T2>("fuck") | ||
| sender.getPid().then((a) => { | ||
| console.log(a) | ||
| }) | ||
| // console.log("link+++2") | ||
| // sender.fuck({ | ||
| // a: 5, b: () => { | ||
| // console.log("1") | ||
| // } | ||
| // }) | ||
| // console.log("link+++3") | ||
| // sender.fuck({ | ||
| // a: 5, b: () => { | ||
| // console.log("2") | ||
| // sender.fuck({ | ||
| // a: 5, b: () => { | ||
| // console.log("3") | ||
| // } | ||
| // }) | ||
| // } | ||
| // }) | ||
| }) | ||
| }, 2000) | ||
| //------------------------------------------------- | ||
| // interface A { | ||
| // init(a: string, b: number): number | ||
| // init2(): void | ||
| // } | ||
| // interface B { | ||
| // a(): void | ||
| // } | ||
| // /** | ||
| // * 新建服务 | ||
| // */ | ||
| // new ZeroRemote(new ws.Server({ port: 2000 }), (carer) => { | ||
| // return new RPCVisitor(carer) | ||
| // }) | ||
| // RPCVisitor.addRemote<A>(async (visitor) => { | ||
| // let a = await visitor.rs.init("a", 4) | ||
| // console.log("打印远程回调结果") | ||
| // console.log(a) | ||
| // }, "tt") | ||
| // RPCVisitor.addRemote<B>(async (visitor) => { | ||
| // let xx = async () => { | ||
| // await visitor.rs.a() | ||
| // xx() | ||
| // } | ||
| // xx() | ||
| // }) | ||
| // class CS extends RPCClient implements A { | ||
| // constructor() { | ||
| // super("ws:127.0.0.1:2000", "tt") | ||
| // } | ||
| // init(a: string, b: number): number { | ||
| // console.log(a) | ||
| // return 5 + b | ||
| // } | ||
| // init2(): void { | ||
| // console.log("_init2____") | ||
| // } | ||
| // } | ||
| // class CS2 extends RPCClient implements B { | ||
| // constructor() { | ||
| // super("ws:127.0.0.1:2000") | ||
| // } | ||
| // a(): void { | ||
| // console.log("aaa") | ||
| // } | ||
| // } | ||
| // let client = new CS() | ||
| // let client2 = new CS2() | ||
| // client.link() | ||
| // client.on("open", () => { | ||
| // console.log("rpc linked") | ||
| // }) | ||
| // client2.link() |
-177
| "use strict"; | ||
| var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
| if (k2 === undefined) k2 = k; | ||
| Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
| }) : (function(o, m, k, k2) { | ||
| if (k2 === undefined) k2 = k; | ||
| o[k2] = m[k]; | ||
| })); | ||
| var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
| Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
| }) : function(o, v) { | ||
| o["default"] = v; | ||
| }); | ||
| var __importStar = (this && this.__importStar) || function (mod) { | ||
| if (mod && mod.__esModule) return mod; | ||
| var result = {}; | ||
| if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
| __setModuleDefault(result, mod); | ||
| return result; | ||
| }; | ||
| var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
| function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
| return new (P || (P = Promise))(function (resolve, reject) { | ||
| function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
| function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
| function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
| step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
| }); | ||
| }; | ||
| var __importDefault = (this && this.__importDefault) || function (mod) { | ||
| return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| const index_1 = __importStar(require("./index")); | ||
| const ws_1 = __importDefault(require("ws")); | ||
| class User extends index_1.GameVisitor { | ||
| clear() { | ||
| } | ||
| start() { | ||
| this.getReceiver("fuck").getPid(() => __awaiter(this, void 0, void 0, function* () { | ||
| console.log("_____________-"); | ||
| return 0; | ||
| })); | ||
| // this.getReceiver<T1>("fuck").on("fuck", (data) => { | ||
| // setTimeout(() => { | ||
| // data.b() | ||
| // }, 2000); | ||
| // }) | ||
| // this.getReceiver<T1>("fuck").fuck((data) => { | ||
| // console.log("___________") | ||
| // setTimeout(() => { | ||
| // data.b() | ||
| // }, 2000); | ||
| // return 5 | ||
| // }) | ||
| // this.getSender<T1>("tt").fuck({ | ||
| // a: 1, b: () => { | ||
| // console.log("____nonono") | ||
| // } | ||
| // }) | ||
| } | ||
| } | ||
| let zn = new index_1.default(new ws_1.default.Server({ port: 8888 }), (carer, server) => { | ||
| return new User(carer, server); | ||
| }); | ||
| class Client extends index_1.ZeroRemoteClient { | ||
| constructor() { | ||
| super(...arguments); | ||
| this.url = "ws://localhost:8888"; | ||
| } | ||
| stringify(value) { | ||
| return JSON.stringify(value); | ||
| } | ||
| showMask() { | ||
| console.log("showMask"); | ||
| } | ||
| hideMask() { | ||
| console.log("hideMask"); | ||
| } | ||
| error(error) { | ||
| console.log(error); | ||
| console.log("error"); | ||
| } | ||
| linked() { | ||
| console.log("linked"); | ||
| } | ||
| popup() { | ||
| console.log("popup"); | ||
| } | ||
| linking() { | ||
| console.log("linking"); | ||
| } | ||
| } | ||
| setTimeout(() => { | ||
| let cc = new Client(); | ||
| // cc.getReceiver<T1>("tt").fuck((a) => { | ||
| // console.log("+++") | ||
| // console.log(a) | ||
| // }) | ||
| cc.link(() => __awaiter(void 0, void 0, void 0, function* () { | ||
| // console.log("link+++1") | ||
| let sender = cc.getSender("fuck"); | ||
| sender.getPid().then((a) => { | ||
| console.log(a); | ||
| }); | ||
| // console.log("link+++2") | ||
| // sender.fuck({ | ||
| // a: 5, b: () => { | ||
| // console.log("1") | ||
| // } | ||
| // }) | ||
| // console.log("link+++3") | ||
| // sender.fuck({ | ||
| // a: 5, b: () => { | ||
| // console.log("2") | ||
| // sender.fuck({ | ||
| // a: 5, b: () => { | ||
| // console.log("3") | ||
| // } | ||
| // }) | ||
| // } | ||
| // }) | ||
| })); | ||
| }, 2000); | ||
| //------------------------------------------------- | ||
| // interface A { | ||
| // init(a: string, b: number): number | ||
| // init2(): void | ||
| // } | ||
| // interface B { | ||
| // a(): void | ||
| // } | ||
| // /** | ||
| // * 新建服务 | ||
| // */ | ||
| // new ZeroRemote(new ws.Server({ port: 2000 }), (carer) => { | ||
| // return new RPCVisitor(carer) | ||
| // }) | ||
| // RPCVisitor.addRemote<A>(async (visitor) => { | ||
| // let a = await visitor.rs.init("a", 4) | ||
| // console.log("打印远程回调结果") | ||
| // console.log(a) | ||
| // }, "tt") | ||
| // RPCVisitor.addRemote<B>(async (visitor) => { | ||
| // let xx = async () => { | ||
| // await visitor.rs.a() | ||
| // xx() | ||
| // } | ||
| // xx() | ||
| // }) | ||
| // class CS extends RPCClient implements A { | ||
| // constructor() { | ||
| // super("ws:127.0.0.1:2000", "tt") | ||
| // } | ||
| // init(a: string, b: number): number { | ||
| // console.log(a) | ||
| // return 5 + b | ||
| // } | ||
| // init2(): void { | ||
| // console.log("_init2____") | ||
| // } | ||
| // } | ||
| // class CS2 extends RPCClient implements B { | ||
| // constructor() { | ||
| // super("ws:127.0.0.1:2000") | ||
| // } | ||
| // a(): void { | ||
| // console.log("aaa") | ||
| // } | ||
| // } | ||
| // let client = new CS() | ||
| // let client2 = new CS2() | ||
| // client.link() | ||
| // client.on("open", () => { | ||
| // console.log("rpc linked") | ||
| // }) | ||
| // client2.link() |
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
116529
0.87%17
30.77%3227
0.53%2
100%