Exemplo n.º 1
0
func clientHandler(ctx context.Context, c *wsock.Client, f *RPCFunction) {
	var (
	//	err error
	)
	defer c.Done()
	//	state := make(ScalarState)
	log.Println("clientProcessor Client connected. ", c.Request())
	fromWS, toWS, doneCh := c.GetChannels()
Loop:
	for {
		log.Println("Reading loop")
		select {
		case request, ok := <-fromWS:
			if !ok {
				break Loop
			}
			log.Println("Request", (*request)["method"])

			method := (*request)["method"].(string)
			params := (*request)["params"]
			log.Println("type of params", reflect.TypeOf(params))
			m := reflect.ValueOf(f).MethodByName(method)
			if !m.IsValid() {
				js := wsock.MessageT{}
				js["error"] = "ERROR: No method found. " + method
				toWS <- &js
				return
			}
			mInterface := m.Interface()
			mm := mInterface.(func([]interface{}) (interface{}, error))
			ch, err := mm(params.([]interface{}))
			if err != nil {
				log.Println("Error calling method. ", err)
				return
			}
			log.Println("Function returned", ch)
			js := wsock.MessageT{}
			js["jsonrpc"] = (*request)["jsonrpc"]
			js["id"] = (*request)["id"]
			js["result"] = ch

			toWS <- &js
			break
		case <-doneCh:
			log.Println("Client disconnected. Exit goroutine")
			break Loop
		}
	}
	return
}
Exemplo n.º 2
0
func clientHandler(ctx context.Context, c *wsock.Client, evStore evstore.Connection) {
	var (
		err error
	)
	//	state := make(ScalarState)
	log.Println("clientProcessor Client connected. ", c.Request())
	id := c.Request().FormValue("id")
	tag := c.Request().FormValue("tag")
	_, toWS, doneCh := c.GetChannels()
	if tag != "" {
		err = evStore.Listenner2().Subscribe2(tag, messageHandler)
		if err != nil {
			log.Println("Can't subscribe to evStore", err)
			return
		}
		ctx2 := context.WithValue(ctx, "toWS", toWS)
		ctx3, cancel := context.WithCancel(ctx2)
		defer cancel()
		go evStore.Listenner2().Listen(ctx3, id)
	} else {
		js := wsock.MessageT{}
		js["response"] = "ERROR: No tag to subscribe"
		toWS <- &js
	}

	log.Println("Enter main loop serving client")
Loop:
	for {
		select {
		case <-doneCh:
			log.Println("Client disconnected. Exit goroutine")
			break Loop
		}
	}
	log.Println("Exit clientProcessor")
}