mirror of
https://github.com/Leo501/CocosCreatorTutorial.git
synced 2026-05-09 07:07:31 +08:00
更新ws代码
This commit is contained in:
@@ -10,8 +10,18 @@
|
||||
},
|
||||
{
|
||||
"__type__": "cc.Scene",
|
||||
"_name": "",
|
||||
"_objFlags": 0,
|
||||
"_parent": null,
|
||||
"_children": [
|
||||
{
|
||||
"__id__": 2
|
||||
}
|
||||
],
|
||||
"_tag": -1,
|
||||
"_active": true,
|
||||
"_components": [],
|
||||
"_prefab": null,
|
||||
"_id": "2d2f792f-a40c-49bb-a189-ed176a246e49",
|
||||
"_opacity": 255,
|
||||
"_color": {
|
||||
"__type__": "cc.Color",
|
||||
@@ -21,7 +31,6 @@
|
||||
"a": 255
|
||||
},
|
||||
"_cascadeOpacityEnabled": true,
|
||||
"_parent": null,
|
||||
"_anchorPoint": {
|
||||
"__type__": "cc.Vec2",
|
||||
"x": 0,
|
||||
@@ -32,44 +41,19 @@
|
||||
"width": 0,
|
||||
"height": 0
|
||||
},
|
||||
"_children": [
|
||||
{
|
||||
"__id__": 2
|
||||
}
|
||||
],
|
||||
"_localZOrder": 0,
|
||||
"_globalZOrder": 0,
|
||||
"_ignoreAnchorPointForPosition": false,
|
||||
"_tag": -1,
|
||||
"_opacityModifyRGB": false,
|
||||
"_id": "2d2f792f-a40c-49bb-a189-ed176a246e49"
|
||||
"groupIndex": 0,
|
||||
"autoReleaseAssets": false
|
||||
},
|
||||
{
|
||||
"__type__": "cc.Node",
|
||||
"_name": "Canvas",
|
||||
"_objFlags": 0,
|
||||
"_opacity": 255,
|
||||
"_color": {
|
||||
"__type__": "cc.Color",
|
||||
"r": 252,
|
||||
"g": 252,
|
||||
"b": 252,
|
||||
"a": 255
|
||||
},
|
||||
"_cascadeOpacityEnabled": true,
|
||||
"_parent": {
|
||||
"__id__": 1
|
||||
},
|
||||
"_anchorPoint": {
|
||||
"__type__": "cc.Vec2",
|
||||
"x": 0.5,
|
||||
"y": 0.5
|
||||
},
|
||||
"_contentSize": {
|
||||
"__type__": "cc.Size",
|
||||
"width": 960,
|
||||
"height": 640
|
||||
},
|
||||
"_children": [
|
||||
{
|
||||
"__id__": 3
|
||||
@@ -79,8 +63,42 @@
|
||||
},
|
||||
{
|
||||
"__id__": 8
|
||||
},
|
||||
{
|
||||
"__id__": 10
|
||||
}
|
||||
],
|
||||
"_tag": -1,
|
||||
"_active": true,
|
||||
"_components": [
|
||||
{
|
||||
"__id__": 15
|
||||
},
|
||||
{
|
||||
"__id__": 16
|
||||
}
|
||||
],
|
||||
"_prefab": null,
|
||||
"_id": "a286bbGknJLZpRpxROV6M94",
|
||||
"_opacity": 255,
|
||||
"_color": {
|
||||
"__type__": "cc.Color",
|
||||
"r": 252,
|
||||
"g": 252,
|
||||
"b": 252,
|
||||
"a": 255
|
||||
},
|
||||
"_cascadeOpacityEnabled": true,
|
||||
"_anchorPoint": {
|
||||
"__type__": "cc.Vec2",
|
||||
"x": 0.5,
|
||||
"y": 0.5
|
||||
},
|
||||
"_contentSize": {
|
||||
"__type__": "cc.Size",
|
||||
"width": 960,
|
||||
"height": 640
|
||||
},
|
||||
"_rotationX": 0,
|
||||
"_rotationY": 0,
|
||||
"_scaleX": 1,
|
||||
@@ -94,25 +112,29 @@
|
||||
"_skewY": 0,
|
||||
"_localZOrder": 0,
|
||||
"_globalZOrder": 0,
|
||||
"_ignoreAnchorPointForPosition": false,
|
||||
"_tag": -1,
|
||||
"_opacityModifyRGB": false,
|
||||
"_id": "a286bbGknJLZpRpxROV6M94",
|
||||
"_active": true,
|
||||
"_components": [
|
||||
{
|
||||
"__id__": 10
|
||||
},
|
||||
{
|
||||
"__id__": 11
|
||||
}
|
||||
],
|
||||
"_prefab": null
|
||||
"groupIndex": 0
|
||||
},
|
||||
{
|
||||
"__type__": "cc.Node",
|
||||
"_name": "background",
|
||||
"_objFlags": 0,
|
||||
"_parent": {
|
||||
"__id__": 2
|
||||
},
|
||||
"_children": [],
|
||||
"_tag": -1,
|
||||
"_active": true,
|
||||
"_components": [
|
||||
{
|
||||
"__id__": 4
|
||||
},
|
||||
{
|
||||
"__id__": 5
|
||||
}
|
||||
],
|
||||
"_prefab": null,
|
||||
"_id": "e2e0crkOLxGrpMxpbC4iQg1",
|
||||
"_opacity": 255,
|
||||
"_color": {
|
||||
"__type__": "cc.Color",
|
||||
@@ -122,9 +144,6 @@
|
||||
"a": 255
|
||||
},
|
||||
"_cascadeOpacityEnabled": true,
|
||||
"_parent": {
|
||||
"__id__": 2
|
||||
},
|
||||
"_anchorPoint": {
|
||||
"__type__": "cc.Vec2",
|
||||
"x": 0.5,
|
||||
@@ -135,7 +154,6 @@
|
||||
"width": 960,
|
||||
"height": 640
|
||||
},
|
||||
"_children": [],
|
||||
"_rotationX": 0,
|
||||
"_rotationY": 0,
|
||||
"_scaleX": 1,
|
||||
@@ -149,20 +167,8 @@
|
||||
"_skewY": 0,
|
||||
"_localZOrder": 0,
|
||||
"_globalZOrder": 0,
|
||||
"_ignoreAnchorPointForPosition": false,
|
||||
"_tag": -1,
|
||||
"_opacityModifyRGB": false,
|
||||
"_id": "e2e0crkOLxGrpMxpbC4iQg1",
|
||||
"_active": true,
|
||||
"_components": [
|
||||
{
|
||||
"__id__": 4
|
||||
},
|
||||
{
|
||||
"__id__": 5
|
||||
}
|
||||
],
|
||||
"_prefab": null
|
||||
"groupIndex": 0
|
||||
},
|
||||
{
|
||||
"__type__": "cc.Widget",
|
||||
@@ -172,15 +178,21 @@
|
||||
"__id__": 3
|
||||
},
|
||||
"_enabled": true,
|
||||
"isAlignOnce": true,
|
||||
"_target": null,
|
||||
"_alignFlags": 45,
|
||||
"_left": 0,
|
||||
"_right": 0,
|
||||
"_top": 0,
|
||||
"_bottom": 0,
|
||||
"_verticalCenter": 0,
|
||||
"_horizontalCenter": 0,
|
||||
"_isAbsLeft": true,
|
||||
"_isAbsRight": true,
|
||||
"_isAbsTop": true,
|
||||
"_isAbsBottom": true,
|
||||
"_isAbsHorizontalCenter": true,
|
||||
"_isAbsVerticalCenter": true,
|
||||
"_originalWidth": 200,
|
||||
"_originalHeight": 150
|
||||
},
|
||||
@@ -196,13 +208,37 @@
|
||||
"__uuid__": "410fb916-8721-4663-bab8-34397391ace7"
|
||||
},
|
||||
"_type": 1,
|
||||
"_useOriginalSize": false,
|
||||
"_sizeMode": 0,
|
||||
"_fillType": 0,
|
||||
"_fillCenter": {
|
||||
"__type__": "cc.Vec2",
|
||||
"x": 0,
|
||||
"y": 0
|
||||
},
|
||||
"_fillStart": 0,
|
||||
"_fillRange": 0,
|
||||
"_isTrimmedMode": true,
|
||||
"_srcBlendFactor": 770,
|
||||
"_dstBlendFactor": 771,
|
||||
"_atlas": null
|
||||
},
|
||||
{
|
||||
"__type__": "cc.Node",
|
||||
"_name": "cocos",
|
||||
"_objFlags": 0,
|
||||
"_parent": {
|
||||
"__id__": 2
|
||||
},
|
||||
"_children": [],
|
||||
"_tag": -1,
|
||||
"_active": true,
|
||||
"_components": [
|
||||
{
|
||||
"__id__": 7
|
||||
}
|
||||
],
|
||||
"_prefab": null,
|
||||
"_id": "c4f30YOS65G64U2TwufdJ+2",
|
||||
"_opacity": 255,
|
||||
"_color": {
|
||||
"__type__": "cc.Color",
|
||||
@@ -212,9 +248,6 @@
|
||||
"a": 255
|
||||
},
|
||||
"_cascadeOpacityEnabled": true,
|
||||
"_parent": {
|
||||
"__id__": 2
|
||||
},
|
||||
"_anchorPoint": {
|
||||
"__type__": "cc.Vec2",
|
||||
"x": 0.5,
|
||||
@@ -225,7 +258,6 @@
|
||||
"width": 195,
|
||||
"height": 270
|
||||
},
|
||||
"_children": [],
|
||||
"_rotationX": 0,
|
||||
"_rotationY": 0,
|
||||
"_scaleX": 1,
|
||||
@@ -239,17 +271,8 @@
|
||||
"_skewY": 0,
|
||||
"_localZOrder": 0,
|
||||
"_globalZOrder": 0,
|
||||
"_ignoreAnchorPointForPosition": false,
|
||||
"_tag": -1,
|
||||
"_opacityModifyRGB": false,
|
||||
"_id": "c4f30YOS65G64U2TwufdJ+2",
|
||||
"_active": true,
|
||||
"_components": [
|
||||
{
|
||||
"__id__": 7
|
||||
}
|
||||
],
|
||||
"_prefab": null
|
||||
"groupIndex": 0
|
||||
},
|
||||
{
|
||||
"__type__": "cc.Sprite",
|
||||
@@ -263,13 +286,37 @@
|
||||
"__uuid__": "31bc895a-c003-4566-a9f3-2e54ae1c17dc"
|
||||
},
|
||||
"_type": 0,
|
||||
"_useOriginalSize": true,
|
||||
"_sizeMode": 1,
|
||||
"_fillType": 0,
|
||||
"_fillCenter": {
|
||||
"__type__": "cc.Vec2",
|
||||
"x": 0,
|
||||
"y": 0
|
||||
},
|
||||
"_fillStart": 0,
|
||||
"_fillRange": 0,
|
||||
"_isTrimmedMode": true,
|
||||
"_srcBlendFactor": 770,
|
||||
"_dstBlendFactor": 771,
|
||||
"_atlas": null
|
||||
},
|
||||
{
|
||||
"__type__": "cc.Node",
|
||||
"_name": "label",
|
||||
"_objFlags": 0,
|
||||
"_parent": {
|
||||
"__id__": 2
|
||||
},
|
||||
"_children": [],
|
||||
"_tag": -1,
|
||||
"_active": true,
|
||||
"_components": [
|
||||
{
|
||||
"__id__": 9
|
||||
}
|
||||
],
|
||||
"_prefab": null,
|
||||
"_id": "31f1bH7V69Ajr1iXhluMpTB",
|
||||
"_opacity": 255,
|
||||
"_color": {
|
||||
"__type__": "cc.Color",
|
||||
@@ -279,9 +326,6 @@
|
||||
"a": 255
|
||||
},
|
||||
"_cascadeOpacityEnabled": true,
|
||||
"_parent": {
|
||||
"__id__": 2
|
||||
},
|
||||
"_anchorPoint": {
|
||||
"__type__": "cc.Vec2",
|
||||
"x": 0.5,
|
||||
@@ -289,10 +333,9 @@
|
||||
},
|
||||
"_contentSize": {
|
||||
"__type__": "cc.Size",
|
||||
"width": 492,
|
||||
"height": 112
|
||||
"width": 147,
|
||||
"height": 60
|
||||
},
|
||||
"_children": [],
|
||||
"_rotationX": 0,
|
||||
"_rotationY": 0,
|
||||
"_scaleX": 1,
|
||||
@@ -306,17 +349,8 @@
|
||||
"_skewY": 0,
|
||||
"_localZOrder": 0,
|
||||
"_globalZOrder": 0,
|
||||
"_ignoreAnchorPointForPosition": false,
|
||||
"_tag": -1,
|
||||
"_opacityModifyRGB": false,
|
||||
"_id": "31f1bH7V69Ajr1iXhluMpTB",
|
||||
"_active": true,
|
||||
"_components": [
|
||||
{
|
||||
"__id__": 9
|
||||
}
|
||||
],
|
||||
"_prefab": null
|
||||
"groupIndex": 0
|
||||
},
|
||||
{
|
||||
"__type__": "cc.Label",
|
||||
@@ -327,15 +361,242 @@
|
||||
},
|
||||
"_enabled": true,
|
||||
"_useOriginalSize": false,
|
||||
"_actualFontSize": 40,
|
||||
"_fontSize": 60,
|
||||
"_lineHeight": 60,
|
||||
"_enableWrapText": true,
|
||||
"_N$file": null,
|
||||
"_isSystemFontUsed": true,
|
||||
"_spacingX": 0,
|
||||
"_N$string": "Label",
|
||||
"_N$horizontalAlign": 1,
|
||||
"_N$verticalAlign": 1,
|
||||
"_N$fontFamily": "Arial",
|
||||
"_N$overflow": 0
|
||||
},
|
||||
{
|
||||
"__type__": "cc.Node",
|
||||
"_name": "close",
|
||||
"_objFlags": 0,
|
||||
"_parent": {
|
||||
"__id__": 2
|
||||
},
|
||||
"_children": [
|
||||
{
|
||||
"__id__": 11
|
||||
}
|
||||
],
|
||||
"_tag": -1,
|
||||
"_active": true,
|
||||
"_components": [
|
||||
{
|
||||
"__id__": 13
|
||||
},
|
||||
{
|
||||
"__id__": 14
|
||||
}
|
||||
],
|
||||
"_prefab": null,
|
||||
"_id": "61eJgfskNCOYdO5QmstACT",
|
||||
"_opacity": 255,
|
||||
"_color": {
|
||||
"__type__": "cc.Color",
|
||||
"r": 255,
|
||||
"g": 255,
|
||||
"b": 255,
|
||||
"a": 255
|
||||
},
|
||||
"_cascadeOpacityEnabled": true,
|
||||
"_anchorPoint": {
|
||||
"__type__": "cc.Vec2",
|
||||
"x": 0.5,
|
||||
"y": 0.5
|
||||
},
|
||||
"_contentSize": {
|
||||
"__type__": "cc.Size",
|
||||
"width": 100,
|
||||
"height": 40
|
||||
},
|
||||
"_rotationX": 0,
|
||||
"_rotationY": 0,
|
||||
"_scaleX": 1,
|
||||
"_scaleY": 1,
|
||||
"_position": {
|
||||
"__type__": "cc.Vec2",
|
||||
"x": 0,
|
||||
"y": -250
|
||||
},
|
||||
"_skewX": 0,
|
||||
"_skewY": 0,
|
||||
"_localZOrder": 0,
|
||||
"_globalZOrder": 0,
|
||||
"_opacityModifyRGB": false,
|
||||
"groupIndex": 0
|
||||
},
|
||||
{
|
||||
"__type__": "cc.Node",
|
||||
"_name": "Label",
|
||||
"_objFlags": 0,
|
||||
"_parent": {
|
||||
"__id__": 10
|
||||
},
|
||||
"_children": [],
|
||||
"_tag": -1,
|
||||
"_active": true,
|
||||
"_components": [
|
||||
{
|
||||
"__id__": 12
|
||||
}
|
||||
],
|
||||
"_prefab": null,
|
||||
"_id": "85plCCAcVPNI6T43vWorCF",
|
||||
"_opacity": 255,
|
||||
"_color": {
|
||||
"__type__": "cc.Color",
|
||||
"r": 0,
|
||||
"g": 0,
|
||||
"b": 0,
|
||||
"a": 255
|
||||
},
|
||||
"_cascadeOpacityEnabled": true,
|
||||
"_anchorPoint": {
|
||||
"__type__": "cc.Vec2",
|
||||
"x": 0.5,
|
||||
"y": 0.5
|
||||
},
|
||||
"_contentSize": {
|
||||
"__type__": "cc.Size",
|
||||
"width": 100,
|
||||
"height": 40
|
||||
},
|
||||
"_rotationX": 0,
|
||||
"_rotationY": 0,
|
||||
"_scaleX": 1,
|
||||
"_scaleY": 1,
|
||||
"_position": {
|
||||
"__type__": "cc.Vec2",
|
||||
"x": 0,
|
||||
"y": 0
|
||||
},
|
||||
"_skewX": 0,
|
||||
"_skewY": 0,
|
||||
"_localZOrder": 0,
|
||||
"_globalZOrder": 0,
|
||||
"_opacityModifyRGB": false,
|
||||
"groupIndex": 0
|
||||
},
|
||||
{
|
||||
"__type__": "cc.Label",
|
||||
"_name": "",
|
||||
"_objFlags": 0,
|
||||
"node": {
|
||||
"__id__": 11
|
||||
},
|
||||
"_enabled": true,
|
||||
"_useOriginalSize": false,
|
||||
"_actualFontSize": 40,
|
||||
"_fontSize": 20,
|
||||
"_lineHeight": 40,
|
||||
"_enableWrapText": false,
|
||||
"_N$file": null,
|
||||
"_isSystemFontUsed": true,
|
||||
"_spacingX": 0,
|
||||
"_N$string": "button",
|
||||
"_N$horizontalAlign": 1,
|
||||
"_N$verticalAlign": 1,
|
||||
"_N$fontFamily": "Arial",
|
||||
"_N$overflow": 1
|
||||
},
|
||||
{
|
||||
"__type__": "cc.Sprite",
|
||||
"_name": "",
|
||||
"_objFlags": 0,
|
||||
"node": {
|
||||
"__id__": 10
|
||||
},
|
||||
"_enabled": true,
|
||||
"_spriteFrame": {
|
||||
"__uuid__": "f0048c10-f03e-4c97-b9d3-3506e1d58952"
|
||||
},
|
||||
"_type": 1,
|
||||
"_sizeMode": 0,
|
||||
"_fillType": 0,
|
||||
"_fillCenter": {
|
||||
"__type__": "cc.Vec2",
|
||||
"x": 0,
|
||||
"y": 0
|
||||
},
|
||||
"_fillStart": 0,
|
||||
"_fillRange": 0,
|
||||
"_isTrimmedMode": true,
|
||||
"_srcBlendFactor": 770,
|
||||
"_dstBlendFactor": 771,
|
||||
"_atlas": null
|
||||
},
|
||||
{
|
||||
"__type__": "cc.Button",
|
||||
"_name": "",
|
||||
"_objFlags": 0,
|
||||
"node": {
|
||||
"__id__": 10
|
||||
},
|
||||
"_enabled": true,
|
||||
"transition": 2,
|
||||
"pressedColor": {
|
||||
"__type__": "cc.Color",
|
||||
"r": 255,
|
||||
"g": 255,
|
||||
"b": 255,
|
||||
"a": 255
|
||||
},
|
||||
"hoverColor": {
|
||||
"__type__": "cc.Color",
|
||||
"r": 255,
|
||||
"g": 255,
|
||||
"b": 255,
|
||||
"a": 255
|
||||
},
|
||||
"duration": 0.1,
|
||||
"zoomScale": 1.2,
|
||||
"clickEvents": [],
|
||||
"_N$interactable": true,
|
||||
"_N$enableAutoGrayEffect": false,
|
||||
"_N$normalColor": {
|
||||
"__type__": "cc.Color",
|
||||
"r": 255,
|
||||
"g": 255,
|
||||
"b": 255,
|
||||
"a": 255
|
||||
},
|
||||
"_N$disabledColor": {
|
||||
"__type__": "cc.Color",
|
||||
"r": 255,
|
||||
"g": 255,
|
||||
"b": 255,
|
||||
"a": 255
|
||||
},
|
||||
"_N$normalSprite": {
|
||||
"__uuid__": "f0048c10-f03e-4c97-b9d3-3506e1d58952"
|
||||
},
|
||||
"_N$pressedSprite": {
|
||||
"__uuid__": "e9ec654c-97a2-4787-9325-e6a10375219a"
|
||||
},
|
||||
"pressedSprite": {
|
||||
"__uuid__": "e9ec654c-97a2-4787-9325-e6a10375219a"
|
||||
},
|
||||
"_N$hoverSprite": {
|
||||
"__uuid__": "e9ec654c-97a2-4787-9325-e6a10375219a"
|
||||
},
|
||||
"hoverSprite": {
|
||||
"__uuid__": "e9ec654c-97a2-4787-9325-e6a10375219a"
|
||||
},
|
||||
"_N$disabledSprite": {
|
||||
"__uuid__": "29158224-f8dd-4661-a796-1ffab537140e"
|
||||
},
|
||||
"_N$target": {
|
||||
"__id__": 10
|
||||
}
|
||||
},
|
||||
{
|
||||
"__type__": "cc.Canvas",
|
||||
"_name": "",
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
const net = require('net');
|
||||
const Emitter = require('./Emitter');
|
||||
const emitter = new Emitter();
|
||||
const wsNet = require('./WsNet');
|
||||
cc.Class({
|
||||
extends: cc.Component,
|
||||
|
||||
@@ -17,17 +18,29 @@ cc.Class({
|
||||
onLoad: function () {
|
||||
this.label.string = this.text;
|
||||
|
||||
this.net = net;
|
||||
this.net.connect();
|
||||
this.net.addHandler('test_pust', (data) => {
|
||||
console.log('test_pust', data);
|
||||
// this.net = net;
|
||||
// this.net.connect();
|
||||
// this.net.addHandler('test_pust', (data) => {
|
||||
// console.log('test_pust', data);
|
||||
// });
|
||||
// this.net.send('test_pust', 'hello');
|
||||
// emitter.on('test_1', this.onTest_1, this);
|
||||
// emitter.on('test_2', this.onTest_2);
|
||||
// emitter.emit('test_1', 1, 1, 1);
|
||||
// emitter.emit('test_2', 1, 1, 1);
|
||||
// emitter.off('test_1', this.onTest_1);
|
||||
|
||||
this.initWsNet();
|
||||
},
|
||||
|
||||
initWsNet() {
|
||||
wsNet.init('ws://192.168.2.176:8181', (result) => {
|
||||
console.log('result=', result);
|
||||
});
|
||||
this.net.send('test_pust', 'hello');
|
||||
emitter.on('test_1', this.onTest_1, this);
|
||||
emitter.on('test_2', this.onTest_2);
|
||||
emitter.emit('test_1', 1, 1, 1);
|
||||
emitter.emit('test_2', 1, 1, 1);
|
||||
emitter.off('test_1', this.onTest_1);
|
||||
},
|
||||
|
||||
onEventClick_close() {
|
||||
wsNet.close();
|
||||
},
|
||||
|
||||
|
||||
|
||||
138
Websocket/assets/Script/WsNet.js
Normal file
138
Websocket/assets/Script/WsNet.js
Normal file
@@ -0,0 +1,138 @@
|
||||
const connectType = {
|
||||
ok: 'open',
|
||||
err: 'close'
|
||||
}
|
||||
class WsNet {
|
||||
|
||||
constructor() {
|
||||
this.ws = null;
|
||||
this.wsUrl = '';
|
||||
this.wsTimeout = 2000;
|
||||
this.lockReconnect = false;
|
||||
//心跳检测
|
||||
this.checkTimeout = 60000;//60秒
|
||||
this.timeoutObj = null;
|
||||
this.serverTimeoutObj = null;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置检查
|
||||
*/
|
||||
resetTimeout() {
|
||||
clearTimeout(this.timeoutObj);
|
||||
clearTimeout(this.serverTimeoutObj);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始检查
|
||||
*/
|
||||
startTimeout() {
|
||||
var self = this;
|
||||
this.timeoutObj = setTimeout(function () {
|
||||
//这里发送一个心跳,后端收到后,返回一个心跳消息,
|
||||
//onmessage拿到返回的心跳就说明连接正常
|
||||
self.ws.send("HeartBeat");
|
||||
//如果超过一定时间还没重置,说明后端主动断开了
|
||||
self.serverTimeoutObj = setTimeout(() => {
|
||||
//如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
|
||||
self.ws.close();
|
||||
}, self.checkTimeout);
|
||||
}, this.checkTimeout);
|
||||
}
|
||||
|
||||
init(wsUrl, fn) {
|
||||
this.wsUrl = wsUrl;
|
||||
this.fn = fn;
|
||||
this.lockReconnect = false;
|
||||
//是否关闭连接
|
||||
this.isCloseWs = false;
|
||||
this.createWebSocket(wsUrl);
|
||||
this.isSuccess=false;
|
||||
setTimeout(() => {
|
||||
if(this.isSuccess) return;
|
||||
this.close();
|
||||
if (this.fn) this.fn(connectType.err);
|
||||
}, 10000);
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭
|
||||
*/
|
||||
isClose() {
|
||||
return this.isCloseWs;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个socket
|
||||
* @param {*} url
|
||||
*/
|
||||
createWebSocket(url) {
|
||||
try {
|
||||
this.ws = new WebSocket(url);
|
||||
this.initEventHandle();
|
||||
} catch (e) {
|
||||
this.reconnect(url);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化事件
|
||||
*/
|
||||
initEventHandle() {
|
||||
this.ws.onclose = (evnt) => {
|
||||
console.log('websocket服务关闭了',evnt);
|
||||
if(this.isClose()) return;
|
||||
if (this.fn) this.fn(connectType.err);
|
||||
this.reconnect(this.wsUrl);
|
||||
};
|
||||
this.ws.onerror = (evnt) => {
|
||||
console.log('websocket服务出错了',evnt);
|
||||
if(this.isClose()) return;
|
||||
if (this.fn) this.fn(connectType.err);
|
||||
this.reconnect(this.wsUrl);
|
||||
};
|
||||
this.ws.onopen = (evnt) => {
|
||||
console.log('websocket 连接成功');
|
||||
this.isSuccess=true;
|
||||
//心跳检测重置
|
||||
this.resetTimeout().startTimeout();
|
||||
if (this.fn) this.fn(connectType.ok);
|
||||
};
|
||||
this.ws.onmessage = (event) => {
|
||||
console.log('event=', event);
|
||||
//如果获取到消息,心跳检测重置
|
||||
this.resetTimeout().startTimeout();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重连
|
||||
* @param {*} url
|
||||
*/
|
||||
reconnect(url) {
|
||||
console.log('事件重连');
|
||||
if (this.lockReconnect) return;
|
||||
this.lockReconnect = true;
|
||||
//没连接上会一直重连,设置延迟避免请求过多
|
||||
setTimeout(() => {
|
||||
this.createWebSocket(url);
|
||||
this.lockReconnect = false;
|
||||
}, this.wsTimeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭ws
|
||||
*/
|
||||
close() {
|
||||
this.isCloseWs=true;
|
||||
this.ws.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = new WsNet();
|
||||
|
||||
|
||||
9
Websocket/assets/Script/WsNet.js.meta
Normal file
9
Websocket/assets/Script/WsNet.js.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "1.0.5",
|
||||
"uuid": "97b4fb72-fa2c-46b8-bee5-f0431c8fc549",
|
||||
"isPlugin": false,
|
||||
"loadPluginInWeb": true,
|
||||
"loadPluginInNative": true,
|
||||
"loadPluginInEditor": false,
|
||||
"subMetas": {}
|
||||
}
|
||||
@@ -8,6 +8,4 @@ wss.on('connection', function (ws) {
|
||||
});
|
||||
});
|
||||
|
||||
console.log('consol1');
|
||||
console.log("websocket连接完毕")
|
||||
console.log('a');
|
||||
Reference in New Issue
Block a user