## 1.简介
一款操作友好的golang实现的tcp框架
仓库地址: https://github.com/fwhezfwhez/tcpx
服务端与客户端示例: https://github.com/fwhezfwhez/tcpx/tree/master/examples
## 2. 预览
```go
package main
import (
"fmt"
"github.com/fwhezfwhez/tcpx"
)
func main() {
srv := tcpx.NewTcpX(tcpx.JsonMarshaller{})
srv.OnMessage = OnMessage
srv.ListenAndServe("tcp", ":7171")
}
func OnMessage(c *tcpx.Context) {
fmt.Println("receive pack from client:", c.Stream)
c.Reply(1, "hello,this is server")
}
```
## 3. 路由
框架协议自带messageID,所以天生自带路由功能。
#### 3.1 路由预览
```go
package main
import (
"fmt"
"github.com/fwhezfwhez/tcpx"
)
func main() {
srv := tcpx.NewTcpX(tcpx.JsonMarshaller{})
srv.AddHandler(1, SayHello)
srv.AddHandler(3, SayGoodBye)
srv.ListenAndServe("tcp", ":7171")
}
func SayHello(c *tcpx.Context) {
var name string
c.Bind(&name)
c.JSON(2, fmt.Sprintf("hello, %s", name))
}
func SayGoodBye(c *tcpx.Context) {
var name string
c.Bind(&name)
c.JSON(2, fmt.Sprintf("bye, %s", name))
}
```
## 4. 中间件
中间件分类与描述:
**全局中间件**: OnMessage与mux模式同时适用。
**锚中间件**: OnMessage与mux同时适用,被UnUse()的锚中间件对OnMessage依然有效。
**路由中间件/自相关中间件**: OnMessage无效, mux有效。
关于执行顺序:
在三种中间件都有的情况下,执行链依次是:
全局中间件-----------> 锚中间件 ----------> 路由中间件
**mux路由与OnMessage不可同时存在,同时存在时,取OnMessage,路由中间件无效,全局中间件与锚中间件有效**
#### 4.1 使用中间件预览
**锚中间件**
```go
package main
import (
"fmt"
"github.com/fwhezfwhez/tcpx"
)
func main() {
srv := tcpx.NewTcpX(tcpx.JsonMarshaller{})
srv.Use("middleware1", Middleware1, "middleware2", Middleware2)
// middleware1, middleware2 同时适用在SayHello
srv.AddHandler(1, SayHello)
srv.UnUse("middleware2")
// middleware1适用SayGoodBye, middleware2依然适用SayHello而不使用SayGoodBye
srv.AddHandler(3, SayGoodBye)
srv.ListenAndServe("tcp", ":7171")
}
```
**全局中间件**
```go
package main
import (
"fmt"
"github.com/fwhezfwhez/tcpx"
)
func main() {
srv := tcpx.NewTcpX(tcpx.JsonMarshaller{})
srv.UseGlobal(GlobalMiddleware)
srv.AddHandler(1, SayHello)
srv.AddHandler(3, SayGoodBye)
srv.ListenAndServe("tcp", ":7171")
}
func GlobalMiddleware(c *tcpx.Context) {
// if fail-condition `c.Abort()`
// else c.Next()
}
```
**路由中间件**
```go
package main
import (
"fmt"
"github.com/fwhezfwhez/tcpx"
)
func main() {
srv := tcpx.NewTcpX(tcpx.JsonMarshaller{})
srv.AddHandler(1,RouterMiddleware, SayHello)
srv.AddHandler(3, SayGoodBye)
srv.ListenAndServe("tcp", ":7171")
}
func RouterMiddleware(c *tcpx.Context) {
// if fail-condition `c.Abort()`
// else c.Next()
}
```
## 5. 支持udp与tcp双发布
#### 5.1 预览
```go
package main
import (
"fmt"
"github.com/fwhezfwhez/tcpx"
)
func main() {
srv := tcpx.NewTcpX(tcpx.JsonMarshaller{})
srv.OnMessage = OnMessage
// tcp
go func(){
fmt.Println("tcp srv listen on 7171")
if e := srv.ListenAndServe("tcp", ":7171"); e != nil {
panic(e)
}
}()
// udp
go func(){
fmt.Println("udp srv listen on 7172")
if e := srv.ListenAndServe("udp", ":7172"); e != nil {
panic(e)
}
}()
select {}
}
```
## 6. 客户端网关协议转换
框架提供网关,为多语言客户端实现协议转换
#### 6.1 框架的协议详情:
```go
// [4]byte -- length fixed_size,binary big endian encode
// [4]byte -- messageID fixed_size,binary big endian encode
// [4]byte -- headerLength fixed_size,binary big endian encode
// [4]byte -- bodyLength fixed_size,binary big endian encode
// []byte -- header marshal by json
// []byte -- body marshal by marshaller
```
#### 6.2 网关仓库与文档说明
https://github.com/fwhezfwhez/tcpx/tree/master/gateway/pack-transfer
有疑问加站长微信联系(非本文作者)