react组件转换成vue组件(2)

承接上文这次使用我的另一个组件vue-simpleWS来对比下两个框架的使用上的不同

上源码 Vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<template>
<div>
<slot></slot>
</div>
</template>

<script>
export default {
name: "Vue-simple-Ws",
props: {
url: {
type: String,
required: true
},
debug: {
type: Boolean,
default: false
},
reconnect: {
type: Boolean,
default: true
}
},
data() {
const that = this;
return {
instance: null,
reconnectFailureTimes: 0,
reconnectTimer: null,
needReconnet: true,
WS: window.WebSocket
? new window.WebSocket(that.url)
: new window.MozWebSocket(that.url)
};
},
watch: {},
mounted() {
const that = this;
that.structureWebSocket();
},
beforeDestroy() {
const that = this;
that.destroy();
},
destroy() {
const that = this;
that.needReconnet = false;
clearTimeout(that.reconnectTimer);
that.printLog(`WS is close`);
that.WS.close();
},
methods: {
structureWebSocket() {
const that = this;
that.WS.onopen = () => {
that.printLog("ws is connected");
that.$emit("onOpen", that.sendData);
};
that.WS.onmessage = data => {
that.printLog(`wsData:${data.data}`);
that.$emit("onMessage", data.data);
};
that.WS.onclose = e => {
const { code, reason, wasClean } = e;
that.printLog(`WS is disconneted,reason:${reason}`);
that.$emit("onClose", code, reason, wasClean);
if (that.reconnect && that.needReconnet) {
that.reconnectFailureTimes++;
if (that.reconnectFailureTimes < 3) {
that.reconnectTimer = window.setTimeout(() => {
that.setState({
WS: window.WebSocket
? new window.WebSocket(that.url)
: new window.MozWebSocket(that.url)
});
that.structureWebSocket();
}, 15 * 1000);
}
}
};
that.WS.onerror = e => {
that.$emit("onError", e);
};
},
sender(msg) {
const that = this;
if (that.WS && that.WS.readyState === 1) {
that.WS.send(msg);
}
},
sendData(msg) {
const that = this;
return that.sender(msg);
},
printLog(val) {
const that = this;
if (that.debug) {
console.log(val);
}
}
}
};
</script>
<style scoped></style>

react

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import React from 'react';
import PropTypes from 'prop-types';

function isFunction(arg) {
if (typeof arg === 'function') {
return true;
}
}

class Websocket extends React.Component {
static propTypes = {
url: PropTypes.string.isRequired,
onOpen: PropTypes.func,
onMessage: PropTypes.func.isRequired,
onError: PropTypes.func,
onClose: PropTypes.func,
debug: PropTypes.bool,
reconnect: PropTypes.bool,
};

static defaultProps = {
debug: false,
reconnect: true,
};

constructor(props) {
super(props);
const that = this;
const { url } = that.props;

that.state = {
WS: window.WebSocket ? new window.WebSocket(url) : new window.MozWebSocket(url),
reconnectTime: 0,
};

that.reconnectFailureTimes = 0;
that.reconnectTimer = null;
that.needReconnet = true;
}

componentDidMount() {
const that = this;

that.structureWebSocket();
}

componentWillUnmount() {
const that = this;
const { WS } = that.state;

that.needReconnet = false;

clearTimeout(that.reconnectTimer);

that.printLog(`WS is close`);

WS.close();
}

structureWebSocket = () => {
const that = this;
const { WS } = that.state;
const { onOpen, onMessage, onError, onClose, reconnect, url } = that.props;

WS.onopen = () => {
that.printLog('ws is connected');

if (isFunction(onOpen)) {
onOpen(that.sendData);
}
};

WS.onmessage = data => {
that.printLog(`wsData:${data.data}`);

if (isFunction(onMessage)) {
onMessage(data.data);
}
};

WS.onclose = e => {
const { code, reason, wasClean } = e;

that.printLog(`WS is disconneted,reason:${reason}`);

if (isFunction(onClose)) {
onClose(code, reason, wasClean);
}

if (reconnect && that.needReconnet) {
that.reconnectFailureTimes++;

if (that.reconnectFailureTimes < 3) {
that.reconnectTimer = window.setTimeout(() => {
that.setState({
WS: window.WebSocket ? new window.WebSocket(url) : new window.MozWebSocket(url),
});
that.structureWebSocket();
}, 15 * 1000);
}
}
};

WS.onerror = e => {
if (isFunction(onError)) {
onError();
}
};
};

sender = msg => {
const that = this;
const { WS } = that.state;

if (WS && WS.readyState === 1) {
WS.send(msg);
}
};

sendData = msg => {
const that = this;

return that.sender(msg);
};

printLog = val => {
const that = this;
const { debug } = that.props;

if (debug) {
console.log(val);
}
};

render() {
const that = this;
const { children } = that.props;

return <div>{children}</div>;
}
}

export default Websocket;

代码比较

  1. 生命周期
    react image.png
    vue image.png
  2. 变量声明
    react image.png
    vue image.png
  3. 函数
    react 组件内的函数直接放在 class 类内部走箭头函数即可
    vue 组件的内定义的函数要放在 methods 对象内

个人还是喜欢react api较少 比较天马行空 vue支持jsx之后无需使用模版语法也挺不错的
反正react 真香 一giao我里giao

源代码以及项目地址
react react-websocket
vue vue-simpleWS

PenZ wechat
交个朋友吧
点滴分享,您的支持将极大的鼓励我!