黄梓健
5 years ago
23 changed files with 652 additions and 553 deletions
-
65connect/logics/login.go
-
12connect/logics/msg.go
-
8main.go
-
11ws/client.go
-
2ws/define.go
-
2ws/http.go
-
65ws/jwt_go.go
-
71ws/login.go
-
2ws/message.go
-
10ws/msg.go
-
26ws/node.go
-
2ws/room.go
-
9ws/timer.go
-
18ws/util.go
-
8ws/ws.go
@ -1,65 +0,0 @@ |
|||
package logics |
|||
|
|||
import ( |
|||
"fmt" |
|||
"hudongzhuanjia/hdws/connect" |
|||
"hudongzhuanjia/libs/jwt" |
|||
) |
|||
|
|||
func init() { |
|||
connect.N.RegisterLogic(connect.LogicLogin, loginFunc) |
|||
} |
|||
|
|||
func loginFunc(c *connect.Client, msg *connect.Message) { |
|||
if _, ok := msg.Data["token"]; !ok { |
|||
c.Write([]byte("token不能为空")) |
|||
return |
|||
} |
|||
claims, err := jwt.ParseAccessToken(msg.Data["token"].(string)) |
|||
if err != nil { |
|||
c.Write([]byte(err.Error())) |
|||
return |
|||
} |
|||
c.Online = true |
|||
c.Id = fmt.Sprintf("%s:%d", claims.AccountType, claims.AccountId) |
|||
c.AccountId = claims.AccountId |
|||
c.AccountType = claims.AccountType |
|||
c.Pid = claims.CustomerPid |
|||
if err := c.Register(); err != nil { |
|||
c.Write([]byte(err.Error())) |
|||
return |
|||
} |
|||
|
|||
if claims.AccountType == connect.IDCustomer { |
|||
if c.Pid == 0 { // 主账号发给子账号
|
|||
msg := &connect.Message{ |
|||
Type: connect.TypeNotice, |
|||
Dest: 0, |
|||
Tag: connect.IDCustomer, |
|||
RoomId: c.RoomId, |
|||
From: c.Id, |
|||
Data: map[string]interface{}{ |
|||
"content": "主账号上线", |
|||
"nickname": "", |
|||
"username": claims.Username, |
|||
}, |
|||
} |
|||
c.Send(msg) |
|||
} else { // 子账号发给主账号
|
|||
msg := &connect.Message{ |
|||
Type: connect.TypeNotice, |
|||
Dest: claims.CustomerPid, |
|||
Tag: connect.IDCustomer, |
|||
RoomId: c.RoomId, |
|||
From: c.Id, |
|||
Data: map[string]interface{}{ |
|||
"content": "子账号上线", |
|||
"nickname": "", |
|||
"username": claims.Username, |
|||
}, |
|||
} |
|||
c.Send(msg) |
|||
} |
|||
} |
|||
c.Write([]byte("登录成功")) |
|||
} |
@ -1,12 +0,0 @@ |
|||
package logics |
|||
|
|||
import "hudongzhuanjia/hdws/connect" |
|||
|
|||
func init() { |
|||
connect.N.RegisterLogic("msg", msgFunc) |
|||
} |
|||
|
|||
// 简单明了
|
|||
func msgFunc(c *connect.Client, msg *connect.Message) { |
|||
c.Send(msg) |
|||
} |
@ -1,4 +1,4 @@ |
|||
package connect |
|||
package ws |
|||
|
|||
type logic func(client *Client, msg *Message) |
|||
|
@ -1,4 +1,4 @@ |
|||
package connect |
|||
package ws |
|||
|
|||
import ( |
|||
"fmt" |
@ -0,0 +1,65 @@ |
|||
package ws |
|||
|
|||
import ( |
|||
"errors" |
|||
"fmt" |
|||
"time" |
|||
|
|||
"github.com/dgrijalva/jwt-go" |
|||
"github.com/ouxuanserver/osmanthuswine/src/helper" |
|||
) |
|||
|
|||
type Claims struct { |
|||
AccountType string |
|||
AccountId int64 |
|||
CustomerId int64 |
|||
CustomerPid int64 |
|||
ActivityId int64 |
|||
AreaId int64 |
|||
jwt.StandardClaims |
|||
} |
|||
|
|||
func GenJwtToken(accountType string, accountId, customerId, customerPid, areaId, activityId int64) (string, error) { |
|||
claims := Claims{ |
|||
accountType, |
|||
accountId, |
|||
customerId, |
|||
customerPid, |
|||
activityId, |
|||
areaId, |
|||
jwt.StandardClaims{ |
|||
ExpiresAt: time.Now().Add(time.Duration(24000) * time.Hour).Unix(), |
|||
Id: helper.CreateUUID(), |
|||
Issuer: Issuer, |
|||
Subject: Subject, |
|||
Audience: Audience, |
|||
}, |
|||
} |
|||
|
|||
t := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) |
|||
return t.SignedString([]byte(Secret)) |
|||
} |
|||
|
|||
const Secret = "osmanthuswine-very-secret" |
|||
const Issuer = "osmanthuswine-issuer-ox" |
|||
const Subject = "osmanthuswine-subject-ox" |
|||
const Audience = "osmanthuswine-audience-ox" |
|||
|
|||
func ParseAccessToken(accessToken string) (*Claims, error) { |
|||
var claims = &Claims{} |
|||
token, err := jwt.ParseWithClaims(accessToken, claims, func(token *jwt.Token) (i interface{}, e error) { |
|||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { |
|||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) |
|||
} |
|||
return []byte(Secret), nil |
|||
}) |
|||
|
|||
if token == nil { |
|||
return claims, errors.New("token invalid") |
|||
} |
|||
|
|||
if !claims.VerifyExpiresAt(time.Now().Unix(), true) { |
|||
return nil, errors.New("token expired") |
|||
} |
|||
return claims, err |
|||
} |
@ -0,0 +1,71 @@ |
|||
package ws |
|||
|
|||
import ( |
|||
"fmt" |
|||
) |
|||
|
|||
func init() { |
|||
N.RegisterLogic(LogicLogin, loginFunc) |
|||
} |
|||
|
|||
func loginFunc(c *Client, msg *Message) { |
|||
if _, ok := msg.Data["token"]; !ok { |
|||
_ = c.WriteJson(map[string]interface{}{ |
|||
"msg": "token不能为空", |
|||
"code": 506, |
|||
}) |
|||
return |
|||
} |
|||
claims, err := ParseAccessToken(msg.Data["token"].(string)) |
|||
if err != nil { |
|||
_ = c.WriteJson(map[string]interface{}{ |
|||
"msg": "token解析出错, " + err.Error(), |
|||
"code": 507, |
|||
}) |
|||
return |
|||
} |
|||
c.Online = true |
|||
c.Id = fmt.Sprintf("%s:%d", claims.AccountType, claims.AccountId) |
|||
c.AccountId = claims.AccountId |
|||
c.AccountType = claims.AccountType |
|||
c.Pid = claims.CustomerPid |
|||
if err = c.Register(); err != nil { |
|||
_ = c.WriteJson(map[string]interface{}{ |
|||
"msg": err.Error(), |
|||
"code": 404, |
|||
}) |
|||
return |
|||
} |
|||
|
|||
if claims.AccountType == IDCustomer { |
|||
if c.Pid == 0 { // 主账号发给子账号
|
|||
m := &Message{ |
|||
Type: TypeNotice, |
|||
Dest: 0, |
|||
Tag: IDCustomer, |
|||
RoomId: c.RoomId, |
|||
From: c.Id, |
|||
Data: map[string]interface{}{ |
|||
"content": "主账号上线", |
|||
}, |
|||
} |
|||
c.Send(m) |
|||
} else { // 子账号发给主账号
|
|||
m := &Message{ |
|||
Type: TypeNotice, |
|||
Dest: claims.CustomerPid, |
|||
Tag: IDCustomer, |
|||
RoomId: c.RoomId, |
|||
From: c.Id, |
|||
Data: map[string]interface{}{ |
|||
"content": "子账号上线", |
|||
}, |
|||
} |
|||
c.Send(m) |
|||
} |
|||
} |
|||
_ = c.WriteJson(map[string]interface{}{ |
|||
"msg": "登录成功", |
|||
"code": 200, |
|||
}) |
|||
} |
@ -1,4 +1,4 @@ |
|||
package connect |
|||
package ws |
|||
|
|||
import "encoding/json" |
|||
|
@ -0,0 +1,10 @@ |
|||
package ws |
|||
|
|||
func init() { |
|||
N.RegisterLogic("msg", msgFunc) |
|||
} |
|||
|
|||
// 简单明了
|
|||
func msgFunc(c *Client, msg *Message) { |
|||
c.Send(msg) |
|||
} |
@ -1,4 +1,4 @@ |
|||
package connect |
|||
package ws |
|||
|
|||
import ( |
|||
"sync" |
@ -1,18 +1,17 @@ |
|||
package logics |
|||
package ws |
|||
|
|||
import ( |
|||
"encoding/json" |
|||
"hudongzhuanjia/hdws/connect" |
|||
"hudongzhuanjia/models" |
|||
"time" |
|||
) |
|||
|
|||
func init() { |
|||
connect.N.RegisterLogic(connect.LogicSync, syncFunc) |
|||
N.RegisterLogic(LogicSync, syncFunc) |
|||
} |
|||
|
|||
func syncFunc(c *connect.Client, msg *connect.Message) { |
|||
if c.AccountType != connect.IDCustomer && c.Pid != 0 { |
|||
func syncFunc(c *Client, msg *Message) { |
|||
if c.AccountType != IDCustomer && c.Pid != 0 { |
|||
c.Write([]byte("此账号无此权限")) |
|||
return |
|||
} |
@ -0,0 +1,18 @@ |
|||
package ws |
|||
|
|||
import ( |
|||
"encoding/json" |
|||
"errors" |
|||
"gopkg.in/olahol/melody.v1" |
|||
) |
|||
|
|||
func WriteJsonWithSession(s *melody.Session, body interface{}) error { |
|||
if s == nil { |
|||
return errors.New("session is nil") |
|||
} |
|||
bs, err := json.Marshal(body) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
return s.Write(bs) |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue