[英]WebSocket not reconnect if disconnect/connect Web socket receptively
在我的應用程序中,當用戶登錄時,我創建了帶有會話 ID 的 webSocket 連接。 並在注銷后套接字將關閉。 這工作正常。 但是,如果我注銷並在殺死應用程序之前再次使用相同帳戶或不同帳戶登錄 webSocket 拋出錯誤現在received bad response code from server 503
, 如果注銷后殺死應用程序並使用初始渲染和用戶登錄套接字打開應用程序將連接沒有任何錯誤。 我的套接字代碼如下。 在這方面的任何幫助將不勝感激。
import React, {Component} from 'react';
import {connect} from 'react-redux';
class SocketController extends React.PureComponent {
state = {
socket: null
}
closeSocket() {
this
.state
.socket
.close();
this.setState({socket: null})
}
componentDidUpdate() {
if (this.props.user && this.props.user.sessionId) {
this.connectSocket();
} else {
this.closeSocket()
}
}
connectSocket() {
let url = 'ws://localhost/socket';
this.setState({
socket: new WebSocket(url, '', {
headers: {
Cookie: this.props.user.sessionId
}
})
}, () => {
this.state.socket.onerror = (e) => {
console.log(e.message);
}
this.state.socket.onclose = () => {}
this.state.socket.onopen = (e) => {}
this.state.socket.onmessage = (event) => {}
});
}
render() {
return null
}
}
const mapStateToProps = ({user}) => ({user});
export default connect(mapStateToProps)(SocketController);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.