go get -u github.com/futurez/freedom
# 克隆项目
$ git clone https://github.com/futurez/freedom.git
# 进入manager服务端样例目录
$ cd ./freedom/examples/manager
# 服务端编译
$ go build -o manager.bin
# 启动服务器
$ ./manager.bin
# 进入login服务器样例目录
$ cd ../login
# 服务器编译
$ go build -o login.bin
# 启动login服务器进行测试
$ ./login.bin
基于freedom框架开发的服务器应用,主函数步骤比较精简,最多只需要3步即可。
- 创建server句柄
- 配置自定义路由及业务
- 启动服务
package main
import (
"github.com/futurez/freedom/fmessage"
"github.com/futurez/freedom/fnet"
)
func main() {
// 1. 创建一个server句柄
s := fnet.NewWsServer("/msg", fmessage.NewJsonPack(), nil)
// 2. 配置路由
s.AddHandle(1, &PingHandle{}) // 路由消息对象
s.AddHandleFunc(2, HelloHandleFunc) // 路由消息函数
// 3. 开启服务
s.Run()
}
其中自定义路由及业务配置方式如下:
package main
import (
"github.com/futurez/freedom/finterface"
"github.com/futurez/freedom/flog"
"github.com/futurez/freedom/fnet"
)
// PingHandle 消息处理对象
type PingHandle struct {
fnet.MsgHandle
}
func (p *PingHandle) Handle(ctx finterface.IContext) {
flog.Debugf("PingHandle MsgId[%d] - [%s]", ctx.GetMsgId(), string(ctx.GetMsgData()))
ctx.GetConnection().SendMsgData(1, 0, []byte("Pong"))
}
// HelloHandleFunc 消息处理函数
func HelloHandleFunc(ctx finterface.IContext) {
flog.Debugf("HelloHandleFunc MsgId[%d] - [%s]", ctx.GetMsgId(), string(ctx.GetMsgData()))
ctx.GetConnection().SendMsgData(1, 0, []byte("Hello"))
}
freedom主动链接以及消息处理
package main
import (
"github.com/futurez/freedom/finterface"
"github.com/futurez/freedom/flog"
"github.com/futurez/freedom/fnet"
"time"
)
func main() {
// 消息注册
fnet.WsAddHandleFunc(1, func(ctx finterface.IContext) {
flog.Debugf("Ping MsgId[%d] - [%s]", ctx.GetMsgId(), string(ctx.GetMsgData()))
ctx.GetConnection().SendMsgData(1, 0, []byte("Pong"))
})
// 主动链接
wc := fnet.NewWsClient("127.0.0.1", 8080, "/msg", true, &ServerClient{}, nil)
wc.ConnectWebSocket()
//
time.Sleep(time.Minute)
}
客户端链接对象函数:
package main
import "github.com/futurez/freedom/finterface"
type ServerClient struct {
}
func (s *ServerClient) OnConnect(conn finterface.IConnection) {
conn.SendMsgData(1, 0, []byte("ping"))
conn.SendMsgData(2, 0, []byte("hello"))
}
func (s *ServerClient) OnDisconnect(conn finterface.IConnection) {
}
{
"serverName" : "freedomServer",
"websocketIP" : "0.0.0.0",
"websocketPort" : 8888,
"serverType" : 1,
"serverId" : 2,
"maxWsConn" : 10000,
"workerPoolSize": 10,
"workerMsgSize" : 100,
"sendMsgChanLen": 100
}
serverName
:服务器应用名称
websocketIP
:服务器IP
websocketPort
:服务器监听端口
serverType
: 服务器类型
serverId
: 服务器编号
maxWsConn
:允许的客户端链接最大数量
workerPoolSize
:工作任务池最大工作Goroutine数量
workerMsgSize
: 工作任务池消息队列长度
sendMsgChanLen
: 发送消息队列长度
func DefaultWsServer() finterface.IServer
func NewWsServer(pattern string, pack finterface.IMsgPack, notify finterface.IConnNotify) finterface.IServer
func (s *WsServer) Run(addr ...string)()
func (s *WsServer) Stop()
func (s *WsServer) GetMsgPack() finterface.IMsgPack
func (s *WsServer) GetRouter() finterface.IRouter
func (s *WsServer) AddHandle(msgId uint32, handle finterface.IMsgHandle)
func (s *WsServer) AddHandleFunc(msgId uint32, handle func(finterface.IContext))
func (s *WsServer)GetConnManager() finterface.IConnManager
//实现router时,先嵌入这个基类,然后根据需要对这个基类的方法进行重写
type BaseHandle struct {}
// 这里之所以 BaseHandle 的方法都为空,
// 是因为有的Handle不希望有PreHandle或PostHandle
// 所以Handle全部继承BaseRouter的好处是,不需要实现PreHandle和PostHandle也可以实例化
func (b *MsgHandle) PreHandle(ctx finterface.IContext) {}
func (b *MsgHandle) Handle(ctx finterface.IContext) {}
func (b *MsgHandle) PostHandle(ctx finterface.IContext) {}
//实现type HandlerFunc func(ctx finterface.IContext)类型函数
func MsgHandle(ctx finterface.IContext) {}
func (c *WsConn) Connected()
func (c *WsConn) Close() bool
func (c *WsConn) GetConnID() int64
func (c *WsConn) RemoteAddr() string
func (c *WsConn) SendMessage(msg finterface.IMessage) (err error)
func (c *WsConn) SendMsgData(msgId uint32, code int32, data []byte) error
func (c *WsConn) GetConnStats()
func (c *WsConn) SetCache(key string, val interface{})
func (c *WsConn) GetCache(key string) (interface{}, bool)