Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

websocket重连 #61

Open
conan1992 opened this issue Aug 12, 2020 · 0 comments
Open

websocket重连 #61

conan1992 opened this issue Aug 12, 2020 · 0 comments

Comments

@conan1992
Copy link
Owner

var ws = new WebSocket("ws://xxx")
ws.onopen = function(){
	heartCheck.reset().start()
	console.log("open")
}
ws.onmessage = function(){
	heartCheck.reset().start()
	console.log("msg")
}
ws.onclose = function(){
	reconnnect()
	console.log("close")
}
ws.onerror = function(){
	reconnnect()
	console.log("error")
}
var heartCheck = {
	timeout: 3000,
	timer: null,
	start: function(){
		let self = this;
		self.timer = setTimeout(function(){
			ws.close()
		}, self.timeout)
	},
	reset: function(){
		clearTimeout(this.timer);
		return this;
	}
}

参考

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant