Browse Source

Added reject to wsprc.js

luis 7 years ago
parent
commit
1ba9e54fd3
2 changed files with 9 additions and 8 deletions
  1. 8 2
      client/wsrpc.js
  2. 1 6
      wsrpc_test.go

+ 8 - 2
client/wsrpc.js

@@ -45,7 +45,6 @@
           if (obj.method === undefined || this._exports[obj.method] === undefined) {
             var dataObj = { 'op': 'response', 'id': obj.id, 'error': 'Method not found' }
             ctx.ws.send(JSON.stringify(dataObj))
-            // TODO: Send object to inform error
             return
           }
           // Send response
@@ -84,7 +83,14 @@
           'method': method,
           'params': aparam
         }
-        ctx._requests[uuidStr] = resolve
+        ctx._requests[uuidStr] = (res,err) => {
+          if (err !== undefined) {
+            reject(err)
+            return
+          }
+          resolve(res)
+        }
+
         // console.log("Sending a call:", method)
         ctx.ws.send(JSON.stringify(callObj)) // if error delete request
       })

+ 1 - 6
wsrpc_test.go

@@ -6,7 +6,6 @@ import (
 	"net/http/httptest"
 	"net/url"
 	"testing"
-	"time"
 
 	"github.com/gohxs/prettylog"
 	"golang.org/x/net/websocket"
@@ -71,8 +70,8 @@ func TestError(t *testing.T) {
 	ws := prepareClient(t)
 
 	cli := wsrpc.NewClient(ws)
-
 	go cli.Process()
+
 	_, err := cli.Call("nomethod", 1, 2, 3, 4)
 	if err == nil {
 		t.Fatal("It should return an error but didn't")
@@ -81,7 +80,6 @@ func TestError(t *testing.T) {
 	if err != nil {
 		t.Fatal("Ups")
 	}
-
 }
 
 //////////////////////
@@ -100,9 +98,6 @@ func cliTest(ws *wsrpc.ClientCtx) {
 	})
 
 	ready <- struct{}{}
-
-	<-time.After(10 * time.Second)
-	ws.WS.Close()
 }
 
 func prepareClient(t *testing.T) *websocket.Conn {