mxd 3 years ago
parent
commit
f5664324eb
1 changed files with 19 additions and 15 deletions
  1. 19 15
      src/scripts/websocket.js

+ 19 - 15
src/scripts/websocket.js

@@ -10,20 +10,23 @@ function MagicWebSocket(url) {
 		}
 		}
 	}
 	}
 	this.listeners = {};
 	this.listeners = {};
-	this.socket = new ReconnectingWebSocket(url);
-	openedSocket[url] = this
-	this.socket.onmessage = this.messageReceived;
-	this.socket.onconnecting = () => {
-        bus.status('message.connectDebugServer')
-    }
-	this.socket.onopen = () => {
-        bus.status('message.connectDebugServerSuccess')
-		bus.$emit('ws_open')
-	}
-	this.socket.onclose = () => {
-        bus.status('message.debugServerClose')
-		bus.$emit('ws_close')
-	}
+	this.future = new Promise(r => {
+		this.socket = new ReconnectingWebSocket(url);
+		openedSocket[url] = this
+		this.socket.onmessage = this.messageReceived;
+		this.socket.onconnecting = () => {
+			bus.status('message.connectDebugServer')
+		}
+		this.socket.onopen = () => {
+			bus.status('message.connectDebugServerSuccess')
+			bus.$emit('ws_open')
+			r()
+		}
+		this.socket.onclose = () => {
+			bus.status('message.debugServerClose')
+			bus.$emit('ws_close')
+		}
+	})
 }
 }
 
 
 MagicWebSocket.prototype.on = function (msgType, callback) {
 MagicWebSocket.prototype.on = function (msgType, callback) {
@@ -48,7 +51,8 @@ MagicWebSocket.prototype.messageReceived = function (e) {
 	bus.$emit('ws_' + msgType, args)
 	bus.$emit('ws_' + msgType, args)
 }
 }
 MagicWebSocket.prototype.send = function(msg){
 MagicWebSocket.prototype.send = function(msg){
-	this.socket.send(msg)
+	this.future.then(() => this.socket.send(msg))
+	
 }
 }
 MagicWebSocket.prototype.close = function () {
 MagicWebSocket.prototype.close = function () {
 	this.socket.close()
 	this.socket.close()