完全使用 Go 的 VSOA 开发库,不需要任何 C 库、汇编和 CGO 的依赖。 VSOA是翼辉自主设计的任务关键型分布式微服务架构。此项目是翼辉在原有基础上推出的 Go 语言官方 SDK。
- Fix client auto reconnect set error.
- Add IsSubscribed API.
- Add auto reconnect check.
- Add onconnect/ondisconnect default implementation.
- Fix defaultOnClientHandler error.
- Add client delete API.
- Add client onconnect implementation.
- Add client reconnect implementation.
- Fix server connection push error.
- Fix server connection receive error.
- Add client default option.
- Fix some bugs on Pub/Sub
- Drop Assembly
- Optimized message processing
- Change module name to vsoa
- Fix some bugs
- Add Server NEW Feature: OnClient func
- Add Server NEW Feature: RAW Publish by trigger
- Happy Christmas Day!
- Server add Count API
- Better stream test example with
client_file_transfer_test.go - Add Client NEW Feature: Regulator
- Change Server API!
- Server now can handle widecard match
- Change module URL to gitee.com
- Release V1.0.1 ver
- Support RPC/SUB/UNSUB/PUB/DATAGRAM
- Not Supoort QoS
| 系统 | 支持 |
|---|---|
| Windows | YES |
| MacOS | YES |
| Linux | YES |
| FreeBSD | YES |
| SylixOS | YES* |
SylixOS 需要使用支持编译 SylixOS 系统的 Golang 编译器编译。发布在 GO SylixOS SDK
本范例假设使用者已经学习过 Go 语言基础,同时能够理解 go module 的工作逻辑。 我们将写一个可以开关控制灯光,且能够读取当前灯光状态的C/S例程。 其他示例代码在配套发布的example文件夹中可以找到。
Golang编译器需要设置为启用go module模式。
go env -w GO111MODULE=on在src文件夹下创建两个文件夹vsoa-server、vsoa-client,分别放置客户端和服务端
分别进入两个文件夹并将以下文件保存为go.mod
vsoa-server文件夹下:
module vsoa-server
go 1.24
require github.com/acoinfo/vsoa v1.0.5vsoa-client文件夹下:
module vsoa-client
go 1.24
require github.com/acoinfo/vsoa v1.0.5文件名:server.go
package main
import (
"encoding/json"
"time"
"github.com/acoinfo/vsoa/protocol"
"github.com/acoinfo/vsoa/server"
)
type RpcLightParam struct {
LightStatus bool `json:"Light On"`
}
var lightstatus = true
func getLight(req, resp *protocol.Message) {
status, _ := json.Marshal(lightstatus)
resp.Param, _ = json.RawMessage(`{"Light On":` + string(status) + `}`).MarshalJSON()
}
func setLight(req, resp *protocol.Message) {
var p RpcLightParam
if err := json.Unmarshal(req.Param, &p); err != nil {
return
}
lightstatus = p.LightStatus
status, _ := json.Marshal(lightstatus)
resp.Param, _ = json.RawMessage(`{"Light On":` + string(status) + `}`).MarshalJSON()
}
func main() {
s := server.NewServer("golang VSOA server",
server.Option{Password: "123456"})
s.On("/light", protocol.RpcMethodGet, getLight)
s.On("/light", protocol.RpcMethodSet, setLight)
if err := s.Serve("localhost:3001"); err != nil {
log.Fatal(err)
}
}文件名:client.go
package main
import (
"encoding/json"
"fmt"
"vsoa-examples/rpc"
"log"
"github.com/acoinfo/vsoa/client"
"github.com/acoinfo/vsoa/protocol"
)
func vsoaRpcCall(c *client.Client, method protocol.RpcMessageType, param json.RawMessage) bool {
req := protocol.NewMessage()
req.Param = param
reply, err := c.Call(rpc.RpcExampleURL, protocol.TypeRPC, method, req)
if err != nil {
fmt.Println("call error:", err)
return false
}
var dst rpc.RpcLightParam
if err := json.Unmarshal(reply.Param, &dst); err != nil {
log.Println("unmarshal error:", err)
return false
}
fmt.Printf("Seq:%d RPC %s Light On:%v\n", reply.SeqNo(), protocol.RpcMethodText(method), dst.LightStatus)
return dst.LightStatus
}
func main() {
c := client.NewClient(client.Option{Password: rpc.Password})
if _, err := c.Connect("vsoa", rpc.ServerAddr); err != nil {
fmt.Println(err)
return
}
defer c.Close()
lightOn := vsoaRpcCall(c, protocol.RpcMethodGet, nil)
cmd, _ := json.Marshal(map[string]bool{"Light On": !lightOn})
fmt.Println("cmd:", string(cmd))
vsoaRpcCall(c, protocol.RpcMethodSet, cmd)
vsoaRpcCall(c, protocol.RpcMethodGet, nil)
}通过命令行/终端程序分别进入vsoa-server、vsoa-client文件夹
执行 go build 指令,后运行生成的可执行文件:
cd vsoa-server
go build
./vsoa-server
# Windows下
.\vsoa-server.execd vsoa-client
go build
./vsoa-client
# Windows下
.\vsoa-client.exe服务器程序不显示任何输出。 客户端程序输出类似如下打印:
Seq: 1 RPC Get Light On: true
Seq: 2 RPC Set Light On: false
Seq: 3 RPC Get Light On: false