koding / kite.js

Kite client in JavaScript
kite.koding.com
MIT License
75 stars 16 forks source link

[kite] ERROR Websocket error! #62

Closed jakelacey2012 closed 7 years ago

jakelacey2012 commented 7 years ago

I'm getting this issue and I'm not to sure why, could anybody guide me, do I need to configure some extra stuff?

[kite] INFO      Trying to connect to ws://localhost:3636/kite
[kite] ERROR   Websocket error!
[kite] INFO      ws://localhost:3636/kite: disconnected
        import { Kite } from 'kite.js';

        const k = new Kite("ws://localhost:3636/kite");
        k.tell('square', 3).then(data => console.log(data));

Server works fine...

    k := kite.New("math", "1.0.0")

    k.HandleFunc("square", func(r *kite.Request) (interface{}, error) {
        a := r.Args.One().MustFloat64()
        result := a * a
        return result, nil
    })

    k.Config.Port = 3636
    k.Config.DisableAuthentication = true
    k.Run()

Client works fine...

        k := kite.New("exp2", "1.0.0")

    mathWorker := k.NewClient("http://localhost:3636/kite")
    mathWorker.Dial()

    response, _ := mathWorker.Tell("square", 4)
    fmt.Println("result: ", response.MustFloat64())

If you need me to provide extra information, just let me know I'll get back to you ASAP.

usirin commented 7 years ago

Hello @jakelacey2012, thanks for reporting this issue. This is an undocumented behavior because our golang server uses the SockJS to accept connections, therefore the connection is not available via WebSocket transport which is the default transportClass of Kite class.

So you will need to use SockJS transport class, exported via Kite.transport.SockJS, here is the example that is working with your case:

import { Kite } from 'kite.js'

const k = new Kite({
  // since there is no `websocket` connection, the url starts with http://...
  url: 'http://localhost:3636/kite',
  // transport layers are exported from `Kite.transport` object, and here we
  // are using the SockJS transport class.
  transportClass: Kite.transport.SockJS,
})

// and you need to wrap arguments in an array
k.tell('square', [3]).then(data => console.log(data))

Note that, you need to wrap arguments in an array.

The code above will work with the current version, but we are currently in the process of creating version 2 of kite.js library. One of the plans is to remove the requirement of wrapping arguments in an array.

jakelacey2012 commented 7 years ago

Awesome thank you for getting back to me @usirin. That seems to have solved the initial connectivity issues. At least this is documented somewhere now :joy:

I'm now getting an error when using the tell method my go server crashes for some reason 😕 , I'll have a look and open a separate issue if I cannot solve.

Again thank you for taking the time :)