|
|
@ -24,7 +24,7 @@ Zinx框架的项目制作采用编码和学习教程同步进行,将开发的 |
|
|
|
### 快速开始 |
|
|
|
|
|
|
|
#### server |
|
|
|
基于Zinx框架开发的服务器应用,主函数步骤比较精简,最多主需要4步即可。 |
|
|
|
基于Zinx框架开发的服务器应用,主函数步骤比较精简,最多主需要3步即可。 |
|
|
|
1. 创建server句柄 |
|
|
|
2. 配置自定义路由及业务 |
|
|
|
3. 启动服务 |
|
|
@ -150,43 +150,49 @@ func main() { |
|
|
|
"WorkerPoolSize":10 |
|
|
|
} |
|
|
|
``` |
|
|
|
|
|
|
|
`Name`:服务器应用名称 |
|
|
|
|
|
|
|
`Host`:服务器IP |
|
|
|
`TcpPort`:服务器监听端口 |
|
|
|
|
|
|
|
`TcpPort`:服务器监听端口 |
|
|
|
|
|
|
|
`MaxConn`:允许的客户端链接最大数量 |
|
|
|
|
|
|
|
`WorkerPoolSize`:工作任务池最大工作Goroutine数量 |
|
|
|
|
|
|
|
###I.服务器模块Server |
|
|
|
|
|
|
|
### I.服务器模块Server |
|
|
|
```go |
|
|
|
func NewServer () ziface.IServer |
|
|
|
``` |
|
|
|
创建一个Zinx服务器句柄,该句柄作为当前服务器应用程序的主枢纽,包括如下功能: |
|
|
|
|
|
|
|
####1) 开启服务 |
|
|
|
#### 1)开启服务 |
|
|
|
```go |
|
|
|
func (s *Server) Start() |
|
|
|
``` |
|
|
|
####2) 停止服务 |
|
|
|
#### 2)停止服务 |
|
|
|
```go |
|
|
|
func (s *Server) Stop() |
|
|
|
``` |
|
|
|
####3) 运行服务 |
|
|
|
#### 3)运行服务 |
|
|
|
```go |
|
|
|
func (s *Server) Serve() |
|
|
|
``` |
|
|
|
####4) 注册路由 |
|
|
|
#### 4)注册路由 |
|
|
|
```go |
|
|
|
func (s *Server) AddRouter (msgId uint32, router ziface.IRouter) |
|
|
|
``` |
|
|
|
####5) 注册链接创建Hook函数 |
|
|
|
#### 5)注册链接创建Hook函数 |
|
|
|
```go |
|
|
|
func (s *Server) SetOnConnStart(hookFunc func (ziface.IConnection)) |
|
|
|
``` |
|
|
|
####6) 注册链接销毁Hook函数 |
|
|
|
#### 6)注册链接销毁Hook函数 |
|
|
|
```go |
|
|
|
func (s *Server) SetOnConnStop(hookFunc func (ziface.IConnection)) |
|
|
|
``` |
|
|
|
###II. 路由模块 |
|
|
|
### II.路由模块 |
|
|
|
|
|
|
|
```go |
|
|
|
//实现router时,先嵌入这个基类,然后根据需要对这个基类的方法进行重写 |
|
|
@ -201,25 +207,25 @@ func (br *BaseRouter)PostHandle(req ziface.IRequest){} |
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
###III. 链接模块 |
|
|
|
####1) 获取原始的socket TCPConn |
|
|
|
### III.链接模块 |
|
|
|
#### 1)获取原始的socket TCPConn |
|
|
|
```go |
|
|
|
func (c *Connection) GetTCPConnection() *net.TCPConn |
|
|
|
``` |
|
|
|
####2) 获取链接ID |
|
|
|
#### 2)获取链接ID |
|
|
|
```go |
|
|
|
func (c *Connection) GetConnID() uint32 |
|
|
|
``` |
|
|
|
####3) 获取远程客户端地址信息 |
|
|
|
#### 3)获取远程客户端地址信息 |
|
|
|
```go |
|
|
|
func (c *Connection) RemoteAddr() net.Addr |
|
|
|
``` |
|
|
|
####4) 发送消息 |
|
|
|
#### 4)发送消息 |
|
|
|
```go |
|
|
|
func (c *Connection) SendMsg(msgId uint32, data []byte) error |
|
|
|
func (c *Connection) SendBuffMsg(msgId uint32, data []byte) error |
|
|
|
``` |
|
|
|
####5) 链接属性 |
|
|
|
#### 5)链接属性 |
|
|
|
```go |
|
|
|
//设置链接属性 |
|
|
|
func (c *Connection) SetProperty(key string, value interface{}) |
|
|
@ -233,7 +239,7 @@ func (c *Connection) RemoveProperty(key string) |
|
|
|
|
|
|
|
|
|
|
|
--- |
|
|
|
###关于作者: |
|
|
|
### 关于作者: |
|
|
|
|
|
|
|
作者:`Aceld(刘丹冰)` |
|
|
|
简书号:`IT无崖子` |
|
|
@ -245,7 +251,7 @@ func (c *Connection) RemoveProperty(key string) |
|
|
|
`原创书籍gitbook`: |
|
|
|
[http://legacy.gitbook.com/@aceld](http://legacy.gitbook.com/@aceld) |
|
|
|
|
|
|
|
###Zinx技术讨论社区 |
|
|
|
### Zinx技术讨论社区 |
|
|
|
|
|
|
|
QQ技术讨论群: |
|
|
|
|
|
|
|