只是对此的更新,我能够采用以下代码:https://github.com/websockets/ws/wiki/Websocket-client-implementation-for-auto-reconnect https://github.com/websockets/ws/wiki/Websocket-client-implementation-for-auto-reconnect
并修改它以在浏览器中为我工作。请参阅下面的代码,请记住这是上述代码的改编,因此我不相信代码。
// Define WSClient instance
window.WSClient = {
// Default reconnect interval
reconnectInterval: 5000,
// Define whether it has ever reconnected
reconnected: false,
// Log messages
debug: false,
// Open the URL
open: function(url) {
// Define that
var that = this;
// Open the URL
this.url = url;
// Create underlying websocket instance
this.instance = new WebSocket(this.url);
// Setup the event handler for onopen
this.instance.onopen = function (ev) {
// If it has ever reconnected lets say that
if (that.reconnected && that.debug) {
console.log('[WS]: Reconnected.');
}
// Run the open function
that.onopen(ev);
}
// Setup the event handler for onmessage
this.instance.onmessage = function(data, flags) {
that.onmessage(data, flags);
}
// Setup the event handler for onclose
this.instance.onclose = function(e) {
switch (e){
// Normal closure
case 1000:
if (that.debug) {
console.log("[WS]: Closed");
}
break;
// Abnormal closure
default:
that.reconnect(e);
break;
}
// Run onclose event
that.onclose(e);
}
// Setup the event handler for onerror
this.instance.onerror = function(e) {
switch (e.code){
// Try and reconnect
case 'ECONNREFUSED':
that.reconnect(e);
break;
// Otherwise run error
default:
that.onerror(e);
break;
}
}
},
// Setup send function
sendRaw: function(data, option) {
try {
this.instance.send(data, option);
} catch (e) {
this.instance.emit('error', e);
}
},
// Send the content
send: function(content) {
this.instance.send(content);
},
// Define the reconnection function
reconnect: function(e) {
// Define that
var that = this;
// Log reconnection
if (that.debug) {
console.log(`[WS]: Reconnecting in ${this.reconnectInterval / 1000} seconds.`);
}
// Set reconnect timeout
setTimeout(function() {
// Log reconnecting
if (that.debug) {
console.log("[WS]: Reconnecting...");
}
// Define has reconnected
that.reconnected = true;
// Try and open the URL
that.open(that.url);
}, this.reconnectInterval);
},
}
所以我在 Vue.JS 框架中使用它,如下所示:
<script type="text/javascript">
// Define the websocket
window.vm['socket_example'] = new Vue({
el: '#socket_example',
name: 'SocketExample',
data: {},
methods: {
// On connection open
socket_open: function(event) {
// Send get lots function
this.$socket.send('some_content_here');
},
// On connection close
socket_close: function(event) {
},
// On connection error
socket_error: function(error) {
},
// On connection message
socket_message: function(event) {
},
},
mounted: function() {
// Setup WebSocket connection
this.$socket = WSClient;
this.$socket.debug = true;
this.$socket.open('<?php echo $endpoint; ?>');
// Setup websocket listeners
this.$socket.onopen = this.socket_open;
this.$socket.onclose = this.socket_close;
this.$socket.onerror = this.socket_error;
this.$socket.onmessage = this.socket_message;
},
});
</script>
无论如何,我希望这有帮助!