[hidecontent type="logged" desc="隐藏内容:登录后可查看"]
curl -fsSL https://get.yomo.run | sh
验证 CLI 是否安装成功
$ yomo version
YoMo CLI Version: v1.1.1
Runtime Version: v1.8.1
$ yomo init yomo-app-demo
⌛ Initializing the Stream Function...
✅ Congratulations! You have initialized the stream function successfully.
ℹ️ You can enjoy the YoMo Stream Function via the command:
ℹ️ DEV: yomo dev -n Noise yomo-app-demo/app.go
ℹ️ PROD: First run source application, eg: go run example/source/main.go
Second: yomo run -n yomo-app-demo yomo-app-demo/app.go
$ cd yomo-app-demo
CLI 将自动创建app.go
:
package main
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/yomorun/yomo/rx"
)
// NoiseData represents the structure of data
type NoiseData struct {
Noise float32 `json:"noise"` // Noise value
Time int64 `json:"time"` // Timestamp (ms)
From string `json:"from"` // Source IP
}
var echo = func(_ context.Context, i interface{}) (interface{}, error) {
value := i.(*NoiseData)
value.Noise = value.Noise / 10
rightNow := time.Now().UnixNano() / int64(time.Millisecond)
fmt.Println(fmt.Sprintf("[%s] %d > value: %f ⚡️=%dms", value.From, value.Time, value.Noise, rightNow-value.Time))
return value.Noise, nil
}
// Handler will handle data in Rx way
func Handler(rxstream rx.Stream) rx.Stream {
stream := rxstream.
Unmarshal(json.Unmarshal, func() interface{} { return &NoiseData{} }).
Debounce(50).
Map(echo).
StdOut()
return stream
}
func DataTags() []byte {
return []byte{0x33}
}
yomo dev
从终端运行。您将看到以下消息:$ yomo dev
ℹ️ YoMo Stream Function file: app.go
⌛ Create YoMo Stream Function instance...
⌛ YoMo Stream Function building...
✅ Success! YoMo Stream Function build.
ℹ️ YoMo Stream Function is running...
2021/11/16 10:02:43 [core:client] has connected to yomo-app-demo (dev.yomo.run:9140)
[localhost] 1637028164050 > value: 6.575044 ⚡️=9ms
[StdOut]: 6.5750437
[localhost] 1637028164151 > value: 10.076103 ⚡️=5ms
[StdOut]: 10.076103
[localhost] 1637028164251 > value: 15.560066 ⚡️=8ms
[StdOut]: 15.560066
[localhost] 1637028164352 > value: 15.330824 ⚡️=2ms
[StdOut]: 15.330824
[localhost] 1637028164453 > value: 10.859857 ⚡️=7ms
[StdOut]: 10.859857
恭喜!您已经完成了第一个 YoMo Stream Function。
[/hidecontent]