简单使用:
import React from 'react';
import Socket from '@/components/WebSocket/Socket';
class MessageList extends React.Component {
constructor(props) {
super(props);
this.state = {
};
this.socket = null;
}
//渲染前调用
componentWillMount() {
this.setsocket()
}
//卸载时调用
componentWillUnmount() {
if (this.socket) {
//当页面卸载时关闭websocket
this.socket.close();
}