예제 #1
0
파일: processor.go 프로젝트: ironbay/drs
func (this *Processor) Invoke(conn *Connection, cmd *Command) (result interface{}, err error) {
	message := &Message{
		Conn:    conn,
		Command: cmd,
		Context: dynamic.Empty(),
	}
	defer func() {
		this.After(message, result, err)
	}()
	defer func() {
		if r := recover(); r != nil {
			log.Println(r)
			console.JSON(cmd)
			log.Println(string(debug.Stack()))
			var ok bool
			if err, ok = r.(error); !ok {
				err = Exception(fmt.Sprint(r))
			}
		}
	}()
	this.Before(message)
	handlers := this.handlers[cmd.Action]
	if handlers == nil {
		return nil, Error("No handlers for " + cmd.Action)
	}
	for _, cb := range handlers {
		result, err = cb(message)
		if err != nil {
			panic(err)
		}
	}
	return result, nil
}
예제 #2
0
파일: ws_test.go 프로젝트: ironbay/drs
func TestConnection(t *testing.T) {
	server := drs.New(New(dynamic.Empty()), protocol.JSON)
	server.On("drs.ping", func(msg *drs.Message) (interface{}, error) {
		return time.Now().UnixNano() / int64(time.Millisecond), nil
	})
	go server.Listen(":12000")

	transport := New(map[string]interface{}{"token": "djkhaled"})
	conn, err := drs.Dial(protocol.JSON, transport, "localhost:12000")
	log.Println("Connected")
	if err != nil {
		t.Fatal(err)
	}
	go conn.Read()

	result, err := conn.Call(&drs.Command{
		Action: "drs.ping",
	})
	if err != nil {
		t.Fatal(err)
	}
	log.Println(result)
}