本文整理汇总了Golang中github.com/jsix/gof/net/nc.SocketServer类的典型用法代码示例。如果您正苦于以下问题:Golang SocketServer类的具体用法?Golang SocketServer怎么用?Golang SocketServer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SocketServer类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: connAuth
// auth connection
func connAuth(s *nc.SocketServer, conn net.Conn, line string) error {
if strings.HasPrefix(line, "AUTH:") {
arr := strings.Split(line[5:], "#") // AUTH:API_ID#SECRET#VERSION
if len(arr) == 3 {
var af nc.AuthFunc = func() (int, error) {
merchantId := dps.MerchantService.GetMerchantIdByApiId(arr[0])
apiInfo := dps.MerchantService.GetApiInfo(merchantId)
if apiInfo != nil && apiInfo.ApiSecret == arr[1] {
if apiInfo.Enabled == 0 {
return merchantId, errors.New("api has exipres")
}
}
return merchantId, nil
}
if err := s.Auth(conn, af); err != nil {
return err
}
s.Printf("[ CLIENT] - Version = %s", arr[2])
return nil
}
}
return errors.New("conn reject")
}
开发者ID:yonglehou,项目名称:go2o,代码行数:26,代码来源:tcp_serve.go
示例2: pushMemberSummary
// push member summary to tcp client
func pushMemberSummary(s *nc.SocketServer, connList []net.Conn, memberId int) {
s.Printf("[ TCP][ NOTIFY] - notify member update - %d", memberId)
sm := GetMemberSummary(memberId, 0)
if d, err := json.Marshal(sm); err == nil {
d = append([]byte("MSUM:"), d...)
for _, conn := range connList {
conn.Write(append(d, '\n'))
}
}
}
开发者ID:yonglehou,项目名称:go2o,代码行数:11,代码来源:tcp_observer.go
示例3: pushMemberAccount
// push member summary to tcp client
func pushMemberAccount(s *nc.SocketServer, connList []net.Conn, memberId int) {
s.Printf("[ TCP][ NOTIFY] - notify account update - %d", memberId)
sm := getMemberAccount(memberId, 0)
if sm != nil {
if d, err := json.Marshal(sm); err == nil {
d = append([]byte("MACC:"), d...)
for _, conn := range connList {
conn.Write(append(d, '\n'))
}
}
}
}
开发者ID:yonglehou,项目名称:go2o,代码行数:13,代码来源:tcp_observer.go
示例4: MemberSummaryNotifyJob
func MemberSummaryNotifyJob(s *nc.SocketServer) {
conn := core.GetRedisConn()
defer conn.Close()
for {
values, err := redis.Values(conn.Do("BLPOP",
variable.KvMemberUpdateTcpNotifyQueue, 0))
if err == nil {
id, err := strconv.Atoi(string(values[1].([]byte)))
if err == nil {
connList := s.GetConnections(id)
if len(connList) > 0 {
go pushMemberSummary(s, connList, id)
}
}
}
}
}
开发者ID:yonglehou,项目名称:go2o,代码行数:17,代码来源:tcp_observer.go
示例5: handleCommand
// Handle command of client sending.
func handleCommand(s *nc.SocketServer, ci *nc.Client, cmd string) ([]byte, error) {
if time.Now().Sub(ci.LatestConnectTime) > disconnectDuration { //主动关闭没有活动的连接
//s.Print("--disconnect ---",ci.Addr.String())
ci.Conn.Close()
return nil, nil
}
if !strings.HasPrefix(cmd, "PING") {
s.Printf("[ CLIENT][ MESSAGE] - send by %d ; %s", ci.Source, cmd)
ci.LatestConnectTime = time.Now()
}
i := strings.Index(cmd, ":")
if i != -1 {
plan := cmd[i+1:]
if v, ok := handlers[cmd[:i]]; ok {
return v(ci, plan)
}
}
return nil, errors.New("unknown command:" + cmd)
}
开发者ID:yonglehou,项目名称:go2o,代码行数:20,代码来源:tcp_serve.go
示例6: memberAuth
// member auth,command like 'MAUTH:jarrysix#3234234242342342'
func memberAuth(s *nc.SocketServer, id *nc.Client, param string) ([]byte, error) {
var err error
arr := strings.Split(param, "#")
if len(arr) == 2 {
f := func() (int, error) {
memberId, _ := strconv.Atoi(arr[0])
authOk := util.CompareMemberApiToken(gof.CurrentApp.Storage(),
memberId, arr[1])
if !authOk {
return memberId, errors.New("auth fail")
}
return memberId, nil
}
if err = s.UAuth(id.Conn, f); err == nil { //验证成功
return []byte("ok"), nil
}
}
return nil, err
}
开发者ID:yonglehou,项目名称:go2o,代码行数:22,代码来源:tcp_serve.go
示例7: NewServe
func NewServe(output bool) *nc.SocketServer {
var s *nc.SocketServer
r := func(conn net.Conn, b []byte) ([]byte, error) {
cmd := string(b)
id, ok := s.GetCli(conn)
if !ok {
// not join,auth first!
if err := connAuth(s, conn, cmd); err != nil {
return nil, err
}
return []byte("ok"), nil
}
if strings.HasPrefix(cmd, "MAUTH:") {
//auth member
return memberAuth(s, id, cmd[6:])
}
return handleCommand(s, id, cmd)
}
s = nc.NewSocketServer(r)
s.ReadDeadLine = defaultReadDeadLine
if !output {
s.OutputOff()
}
return s
}
开发者ID:yonglehou,项目名称:go2o,代码行数:26,代码来源:tcp_serve.go
注:本文中的github.com/jsix/gof/net/nc.SocketServer类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论