diff --git a/assets/Scene/main.fire b/assets/Scene/main.fire index 121ee9a..549fa4f 100755 --- a/assets/Scene/main.fire +++ b/assets/Scene/main.fire @@ -80,26 +80,29 @@ "__id__": 21 }, { - "__id__": 28 + "__id__": 29 }, { - "__id__": 35 + "__id__": 37 }, { - "__id__": 42 + "__id__": 45 }, { - "__id__": 49 + "__id__": 53 }, { - "__id__": 60 + "__id__": 64 } ], "_active": true, "_level": 0, "_components": [ { - "__id__": 67 + "__id__": 72 + }, + { + "__id__": 73 } ], "_prefab": null, @@ -1207,7 +1210,11 @@ "_enabled": true, "duration": 0.1, "zoomScale": 1.2, - "clickEvents": [], + "clickEvents": [ + { + "__id__": 28 + } + ], "_N$interactable": true, "_N$enableAutoGrayEffect": false, "_N$transition": 2, @@ -1277,6 +1284,16 @@ }, "_id": "b2KpTiPMBKnqob5HJqb0Xn" }, + { + "__type__": "cc.ClickEvent", + "target": { + "__id__": 2 + }, + "component": "", + "_componentId": "55d23dNa9JDRafcCFqeXFVH", + "handler": "onConnectClick", + "customEventData": "" + }, { "__type__": "cc.Node", "_name": "button", @@ -1286,14 +1303,14 @@ }, "_children": [ { - "__id__": 29 + "__id__": 30 } ], "_active": true, "_level": 1, "_components": [ { - "__id__": 34 + "__id__": 35 } ], "_prefab": null, @@ -1346,21 +1363,21 @@ "_name": "Background", "_objFlags": 0, "_parent": { - "__id__": 28 + "__id__": 29 }, "_children": [ { - "__id__": 30 + "__id__": 31 } ], "_active": true, "_level": 2, "_components": [ { - "__id__": 32 + "__id__": 33 }, { - "__id__": 33 + "__id__": 34 } ], "_prefab": null, @@ -1413,14 +1430,14 @@ "_name": "Label", "_objFlags": 0, "_parent": { - "__id__": 29 + "__id__": 30 }, "_children": [], "_active": true, "_level": 3, "_components": [ { - "__id__": 31 + "__id__": 32 } ], "_prefab": null, @@ -1473,7 +1490,7 @@ "_name": "", "_objFlags": 0, "node": { - "__id__": 30 + "__id__": 31 }, "_enabled": true, "_materials": [ @@ -1503,7 +1520,7 @@ "_name": "", "_objFlags": 0, "node": { - "__id__": 29 + "__id__": 30 }, "_enabled": true, "_materials": [ @@ -1535,7 +1552,7 @@ "_name": "", "_objFlags": 0, "node": { - "__id__": 29 + "__id__": 30 }, "_enabled": true, "alignMode": 0, @@ -1562,12 +1579,16 @@ "_name": "", "_objFlags": 0, "node": { - "__id__": 28 + "__id__": 29 }, "_enabled": true, "duration": 0.1, "zoomScale": 1.2, - "clickEvents": [], + "clickEvents": [ + { + "__id__": 36 + } + ], "_N$interactable": true, "_N$enableAutoGrayEffect": false, "_N$transition": 2, @@ -1633,10 +1654,20 @@ "__uuid__": "29158224-f8dd-4661-a796-1ffab537140e" }, "_N$target": { - "__id__": 29 + "__id__": 30 }, "_id": "76p2Qpob9DM5TeAHzz3dN7" }, + { + "__type__": "cc.ClickEvent", + "target": { + "__id__": 2 + }, + "component": "", + "_componentId": "55d23dNa9JDRafcCFqeXFVH", + "handler": "onSendClick", + "customEventData": "" + }, { "__type__": "cc.Node", "_name": "button", @@ -1646,14 +1677,14 @@ }, "_children": [ { - "__id__": 36 + "__id__": 38 } ], "_active": true, "_level": 1, "_components": [ { - "__id__": 41 + "__id__": 43 } ], "_prefab": null, @@ -1706,21 +1737,21 @@ "_name": "Background", "_objFlags": 0, "_parent": { - "__id__": 35 + "__id__": 37 }, "_children": [ { - "__id__": 37 + "__id__": 39 } ], "_active": true, "_level": 2, "_components": [ { - "__id__": 39 + "__id__": 41 }, { - "__id__": 40 + "__id__": 42 } ], "_prefab": null, @@ -1773,14 +1804,14 @@ "_name": "Label", "_objFlags": 0, "_parent": { - "__id__": 36 + "__id__": 38 }, "_children": [], "_active": true, "_level": 3, "_components": [ { - "__id__": 38 + "__id__": 40 } ], "_prefab": null, @@ -1833,7 +1864,7 @@ "_name": "", "_objFlags": 0, "node": { - "__id__": 37 + "__id__": 39 }, "_enabled": true, "_materials": [ @@ -1863,7 +1894,7 @@ "_name": "", "_objFlags": 0, "node": { - "__id__": 36 + "__id__": 38 }, "_enabled": true, "_materials": [ @@ -1895,7 +1926,7 @@ "_name": "", "_objFlags": 0, "node": { - "__id__": 36 + "__id__": 38 }, "_enabled": true, "alignMode": 0, @@ -1922,12 +1953,16 @@ "_name": "", "_objFlags": 0, "node": { - "__id__": 35 + "__id__": 37 }, "_enabled": true, "duration": 0.1, "zoomScale": 1.2, - "clickEvents": [], + "clickEvents": [ + { + "__id__": 44 + } + ], "_N$interactable": true, "_N$enableAutoGrayEffect": false, "_N$transition": 2, @@ -1993,10 +2028,20 @@ "__uuid__": "29158224-f8dd-4661-a796-1ffab537140e" }, "_N$target": { - "__id__": 36 + "__id__": 38 }, "_id": "5dawawCB9ILK9dEXz10fmX" }, + { + "__type__": "cc.ClickEvent", + "target": { + "__id__": 2 + }, + "component": "", + "_componentId": "", + "handler": "", + "customEventData": "" + }, { "__type__": "cc.Node", "_name": "button", @@ -2006,14 +2051,14 @@ }, "_children": [ { - "__id__": 43 + "__id__": 46 } ], "_active": true, "_level": 1, "_components": [ { - "__id__": 48 + "__id__": 51 } ], "_prefab": null, @@ -2066,21 +2111,21 @@ "_name": "Background", "_objFlags": 0, "_parent": { - "__id__": 42 + "__id__": 45 }, "_children": [ { - "__id__": 44 + "__id__": 47 } ], "_active": true, "_level": 2, "_components": [ { - "__id__": 46 + "__id__": 49 }, { - "__id__": 47 + "__id__": 50 } ], "_prefab": null, @@ -2133,14 +2178,14 @@ "_name": "Label", "_objFlags": 0, "_parent": { - "__id__": 43 + "__id__": 46 }, "_children": [], "_active": true, "_level": 3, "_components": [ { - "__id__": 45 + "__id__": 48 } ], "_prefab": null, @@ -2193,7 +2238,7 @@ "_name": "", "_objFlags": 0, "node": { - "__id__": 44 + "__id__": 47 }, "_enabled": true, "_materials": [ @@ -2223,7 +2268,7 @@ "_name": "", "_objFlags": 0, "node": { - "__id__": 43 + "__id__": 46 }, "_enabled": true, "_materials": [ @@ -2255,7 +2300,7 @@ "_name": "", "_objFlags": 0, "node": { - "__id__": 43 + "__id__": 46 }, "_enabled": true, "alignMode": 0, @@ -2282,12 +2327,16 @@ "_name": "", "_objFlags": 0, "node": { - "__id__": 42 + "__id__": 45 }, "_enabled": true, "duration": 0.1, "zoomScale": 1.2, - "clickEvents": [], + "clickEvents": [ + { + "__id__": 52 + } + ], "_N$interactable": true, "_N$enableAutoGrayEffect": false, "_N$transition": 2, @@ -2353,10 +2402,20 @@ "__uuid__": "29158224-f8dd-4661-a796-1ffab537140e" }, "_N$target": { - "__id__": 43 + "__id__": 46 }, "_id": "13btpffZxIJaE2kcLK2JKp" }, + { + "__type__": "cc.ClickEvent", + "target": { + "__id__": 2 + }, + "component": "", + "_componentId": "", + "handler": "", + "customEventData": "" + }, { "__type__": "cc.Node", "_name": "editbox", @@ -2366,20 +2425,20 @@ }, "_children": [ { - "__id__": 50 + "__id__": 54 }, { - "__id__": 53 + "__id__": 57 }, { - "__id__": 56 + "__id__": 60 } ], "_active": true, "_level": 1, "_components": [ { - "__id__": 59 + "__id__": 63 } ], "_prefab": null, @@ -2432,17 +2491,17 @@ "_name": "BACKGROUND_SPRITE", "_objFlags": 0, "_parent": { - "__id__": 49 + "__id__": 53 }, "_children": [], "_active": true, "_level": 2, "_components": [ { - "__id__": 51 + "__id__": 55 }, { - "__id__": 52 + "__id__": 56 } ], "_prefab": null, @@ -2495,7 +2554,7 @@ "_name": "", "_objFlags": 0, "node": { - "__id__": 50 + "__id__": 54 }, "_enabled": true, "_materials": [ @@ -2527,7 +2586,7 @@ "_name": "", "_objFlags": 0, "node": { - "__id__": 50 + "__id__": 54 }, "_enabled": true, "alignMode": 0, @@ -2554,17 +2613,17 @@ "_name": "TEXT_LABEL", "_objFlags": 0, "_parent": { - "__id__": 49 + "__id__": 53 }, "_children": [], "_active": false, "_level": 2, "_components": [ { - "__id__": 54 + "__id__": 58 }, { - "__id__": 55 + "__id__": 59 } ], "_prefab": null, @@ -2617,7 +2676,7 @@ "_name": "", "_objFlags": 0, "node": { - "__id__": 53 + "__id__": 57 }, "_enabled": true, "_materials": [], @@ -2643,7 +2702,7 @@ "_name": "", "_objFlags": 0, "node": { - "__id__": 53 + "__id__": 57 }, "_enabled": true, "alignMode": 0, @@ -2670,17 +2729,17 @@ "_name": "PLACEHOLDER_LABEL", "_objFlags": 0, "_parent": { - "__id__": 49 + "__id__": 53 }, "_children": [], "_active": true, "_level": 2, "_components": [ { - "__id__": 57 + "__id__": 61 }, { - "__id__": 58 + "__id__": 62 } ], "_prefab": null, @@ -2733,7 +2792,7 @@ "_name": "", "_objFlags": 0, "node": { - "__id__": 56 + "__id__": 60 }, "_enabled": true, "_materials": [ @@ -2763,7 +2822,7 @@ "_name": "", "_objFlags": 0, "node": { - "__id__": 56 + "__id__": 60 }, "_enabled": true, "alignMode": 0, @@ -2790,7 +2849,7 @@ "_name": "", "_objFlags": 0, "node": { - "__id__": 49 + "__id__": 53 }, "_enabled": true, "_useOriginalSize": false, @@ -2803,13 +2862,13 @@ "editingDidEnded": [], "editingReturn": [], "_N$textLabel": { - "__id__": 54 + "__id__": 58 }, "_N$placeholderLabel": { - "__id__": 57 + "__id__": 61 }, "_N$background": { - "__id__": 51 + "__id__": 55 }, "_N$inputFlag": 5, "_N$inputMode": 6, @@ -2825,14 +2884,14 @@ }, "_children": [ { - "__id__": 61 + "__id__": 65 } ], "_active": true, "_level": 1, "_components": [ { - "__id__": 66 + "__id__": 70 } ], "_prefab": null, @@ -2885,21 +2944,21 @@ "_name": "Background", "_objFlags": 0, "_parent": { - "__id__": 60 + "__id__": 64 }, "_children": [ { - "__id__": 62 + "__id__": 66 } ], "_active": true, "_level": 2, "_components": [ { - "__id__": 64 + "__id__": 68 }, { - "__id__": 65 + "__id__": 69 } ], "_prefab": null, @@ -2952,14 +3011,14 @@ "_name": "Label", "_objFlags": 0, "_parent": { - "__id__": 61 + "__id__": 65 }, "_children": [], "_active": true, "_level": 3, "_components": [ { - "__id__": 63 + "__id__": 67 } ], "_prefab": null, @@ -3012,7 +3071,7 @@ "_name": "", "_objFlags": 0, "node": { - "__id__": 62 + "__id__": 66 }, "_enabled": true, "_materials": [ @@ -3042,7 +3101,7 @@ "_name": "", "_objFlags": 0, "node": { - "__id__": 61 + "__id__": 65 }, "_enabled": true, "_materials": [ @@ -3074,7 +3133,7 @@ "_name": "", "_objFlags": 0, "node": { - "__id__": 61 + "__id__": 65 }, "_enabled": true, "alignMode": 0, @@ -3101,12 +3160,16 @@ "_name": "", "_objFlags": 0, "node": { - "__id__": 60 + "__id__": 64 }, "_enabled": true, "duration": 0.1, "zoomScale": 1.2, - "clickEvents": [], + "clickEvents": [ + { + "__id__": 71 + } + ], "_N$interactable": true, "_N$enableAutoGrayEffect": false, "_N$transition": 2, @@ -3172,10 +3235,20 @@ "__uuid__": "29158224-f8dd-4661-a796-1ffab537140e" }, "_N$target": { - "__id__": 61 + "__id__": 65 }, "_id": "96LXG7k+9DhJ9uqQe+PwPL" }, + { + "__type__": "cc.ClickEvent", + "target": { + "__id__": 2 + }, + "component": "", + "_componentId": "55d23dNa9JDRafcCFqeXFVH", + "handler": "onDisconnectClick", + "customEventData": "" + }, { "__type__": "cc.Canvas", "_name": "", @@ -3192,5 +3265,22 @@ "_fitWidth": false, "_fitHeight": true, "_id": "2cVuH++1RJi4gzFGjg5Kbt" + }, + { + "__type__": "55d23dNa9JDRafcCFqeXFVH", + "_name": "", + "_objFlags": 0, + "node": { + "__id__": 2 + }, + "_enabled": true, + "text": "hello", + "textLabel": { + "__id__": 58 + }, + "urlLabel": { + "__id__": 11 + }, + "_id": "0bpgf5DdFHd43ZDy0vkFUX" } ] \ No newline at end of file diff --git a/assets/Script/example.meta b/assets/Script/example.meta new file mode 100644 index 0000000..dbd0cbe --- /dev/null +++ b/assets/Script/example.meta @@ -0,0 +1,7 @@ +{ + "ver": "1.0.1", + "uuid": "dbb441af-a09d-46e3-8941-73a234e9ccb4", + "isSubpackage": false, + "subpackageName": "", + "subMetas": {} +} \ No newline at end of file diff --git a/assets/Script/example/NetExample.ts b/assets/Script/example/NetExample.ts new file mode 100644 index 0000000..14b2a0f --- /dev/null +++ b/assets/Script/example/NetExample.ts @@ -0,0 +1,42 @@ +import { WebSock } from "../network/WebSock"; +import { NetManager } from "../network/NetManager"; +import { NetNode } from "../network/NetNode"; +import { DefStringProtocol, NetData } from "../network/NetInterface"; + + + +const { ccclass, property } = cc._decorator; + +@ccclass +export default class NewClass extends cc.Component { + @property + text: string = 'hello'; + @property(cc.Label) + textLabel: cc.Label = null; + @property(cc.Label) + urlLabel: cc.Label = null; + + onLoad() { + let Node = new NetNode(); + Node.init(new WebSock(), new DefStringProtocol()); + Node.setResponeHandler(0, (cmd: number, data: NetData) => { + console.log(`${data}`); + }); + NetManager.getInstance().setNetNode(Node); + + } + + onConnectClick() { + NetManager.getInstance().connect({ url: this.urlLabel.string }); + } + + onSendClick() { + NetManager.getInstance().send(this.textLabel.string); + } + + onDisconnectClick() { + NetManager.getInstance().close(); + } + + // update (dt) {} +} diff --git a/assets/Script/example/NetExample.ts.meta b/assets/Script/example/NetExample.ts.meta new file mode 100644 index 0000000..01dc950 --- /dev/null +++ b/assets/Script/example/NetExample.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "1.0.5", + "uuid": "55d2374d-6bd2-4345-a7dc-085a9e5c5547", + "isPlugin": false, + "loadPluginInWeb": true, + "loadPluginInNative": true, + "loadPluginInEditor": false, + "subMetas": {} +} \ No newline at end of file diff --git a/assets/Script/network/NetInterface.ts b/assets/Script/network/NetInterface.ts index fe07ec8..93c0bb0 100644 --- a/assets/Script/network/NetInterface.ts +++ b/assets/Script/network/NetInterface.ts @@ -6,7 +6,7 @@ */ export type NetData = (string | ArrayBufferLike | Blob | ArrayBufferView); -export type NetCallFunc = (mainCmd: number, subCmd: number, data: any) => void; +export type NetCallFunc = (cmd: number, data: any) => void; // 回调对象 export interface CallbackObject { @@ -29,6 +29,22 @@ export interface IProtocolHelper { checkCmd(msg: NetData): number; // 获取协议类型或id } +// 默认字符串协议对象 +export class DefStringProtocol implements IProtocolHelper { + getHeadlen(): number { + return 0; + } + getHearbeat(): NetData { + return ""; + } + checkHead(msg: NetData): boolean { + return true; + } + checkCmd(msg: NetData): number { + return 0; + } +} + // Socket对象 export interface ISocket { onConnected: (event) => void; diff --git a/assets/Script/network/NetManager.ts b/assets/Script/network/NetManager.ts index da4f9c4..5418681 100644 --- a/assets/Script/network/NetManager.ts +++ b/assets/Script/network/NetManager.ts @@ -19,7 +19,7 @@ export class NetManager { } // 添加Node,返回ChannelID - public setNetNode(channelId: number, newNode: NetNode) { + public setNetNode(newNode: NetNode, channelId: number = 0) { this._channels[channelId] = newNode; } diff --git a/assets/Script/network/NetNode.ts b/assets/Script/network/NetNode.ts index e7fd1af..fb6e0da 100644 --- a/assets/Script/network/NetNode.ts +++ b/assets/Script/network/NetNode.ts @@ -55,14 +55,17 @@ export class NetNode { protected _receiveTime: number = 6000000; // 多久没收到数据断开 protected _reconnetTimeOut: number = 8000000; // 重连间隔 protected _requests: RequestObject[] = Array(); // 请求列表 - protected _ protected _listener: { [key: number]: CallbackObject[] } = {} // 监听者列表 /********************** 网络相关处理 *********************/ - public init(socket: ISocket, networkTips: any = null) { + public init(socket: ISocket, protocol: IProtocolHelper, networkTips: any = null, execFunc : ExecuterFunc = null) { console.log(`NetNode init socket`); this._socket = socket; + this._protocolHelper = protocol; this._networkTips = networkTips; + this._callbackExecuter = execFunc ? execFunc : (callback: CallbackObject, buffer: NetData) => { + callback.callback.call(callback.target, 0, buffer); + } } public connect(options: NetConnectOptions): boolean { @@ -275,7 +278,7 @@ export class NetNode { } // 唯一request,确保没有同一响应的请求(避免一个请求重复发送,netTips界面的屏蔽也是一个好的方法) - public requestUnique(buf: NetData, rspCmd: number, rspObject: CallbackObject, showTips: boolean = true, force: boolean = false):boolean { + public requestUnique(buf: NetData, rspCmd: number, rspObject: CallbackObject, showTips: boolean = true, force: boolean = false): boolean { for (let i = 0; i < this._requests.length; ++i) { if (this._requests[i].rspCmd == rspCmd) { console.log(`NetNode requestUnique faile for ${rspCmd}`);