first commit
This commit is contained in:
22
server/chatserver/logic/entity.go
Normal file
22
server/chatserver/logic/entity.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package logic
|
||||
|
||||
import "time"
|
||||
|
||||
type User struct {
|
||||
rid int
|
||||
nickName string
|
||||
}
|
||||
|
||||
func NewUser(rid int, nickName string) *User {
|
||||
return &User{
|
||||
rid: rid,
|
||||
nickName: nickName,
|
||||
}
|
||||
}
|
||||
|
||||
type Msg struct {
|
||||
RId int `json:"rid"`
|
||||
NickName string `json:"nickName"`
|
||||
Msg string `json:"msg"`
|
||||
Time time.Time `json:"time"`
|
||||
}
|
||||
79
server/chatserver/logic/group.go
Normal file
79
server/chatserver/logic/group.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"slgserver/net"
|
||||
"slgserver/server/chatserver/proto"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Group struct {
|
||||
userMutex sync.RWMutex
|
||||
msgMutex sync.RWMutex
|
||||
users map[int]*User
|
||||
msgs ItemQueue
|
||||
}
|
||||
|
||||
func NewGroup() *Group {
|
||||
return &Group{users: map[int]*User{}}
|
||||
}
|
||||
|
||||
func (this*Group) Enter(u *User) {
|
||||
this.userMutex.Lock()
|
||||
defer this.userMutex.Unlock()
|
||||
this.users[u.rid] = u
|
||||
}
|
||||
|
||||
func (this*Group) Exit(rid int) {
|
||||
this.userMutex.Lock()
|
||||
defer this.userMutex.Unlock()
|
||||
delete(this.users, rid)
|
||||
}
|
||||
|
||||
func (this*Group) GetUser(rid int) *User {
|
||||
this.userMutex.Lock()
|
||||
defer this.userMutex.Unlock()
|
||||
return this.users[rid]
|
||||
}
|
||||
|
||||
func (this*Group) PutMsg(text string, rid int, t int8) *proto.ChatMsg {
|
||||
|
||||
this.userMutex.RLock()
|
||||
u, ok := this.users[rid]
|
||||
this.userMutex.RUnlock()
|
||||
if ok == false{
|
||||
return nil
|
||||
}
|
||||
|
||||
msg := &Msg{Msg: text, RId: rid, Time: time.Now(), NickName: u.nickName}
|
||||
this.msgMutex.Lock()
|
||||
size := this.msgs.Size()
|
||||
if size > 100 {
|
||||
this.msgs.Dequeue()
|
||||
}
|
||||
this.msgs.Enqueue(msg)
|
||||
this.msgMutex.Unlock()
|
||||
|
||||
//广播
|
||||
this.userMutex.RLock()
|
||||
c := &proto.ChatMsg{RId: msg.RId, NickName: msg.NickName, Time: msg.Time.Unix(), Msg: msg.Msg, Type: t}
|
||||
for _, user := range this.users {
|
||||
net.ConnMgr.PushByRoleId(user.rid, "chat.push", c)
|
||||
}
|
||||
this.userMutex.RUnlock()
|
||||
return c
|
||||
}
|
||||
|
||||
func (this*Group) History() []proto.ChatMsg {
|
||||
r := make([]proto.ChatMsg, 0)
|
||||
this.msgMutex.RLock()
|
||||
items := this.msgs.items
|
||||
for _, item := range items {
|
||||
msg := item.(*Msg)
|
||||
c := proto.ChatMsg{RId: msg.RId, NickName: msg.NickName, Time: msg.Time.Unix(), Msg: msg.Msg}
|
||||
r = append(r, c)
|
||||
}
|
||||
this.msgMutex.RUnlock()
|
||||
|
||||
return r
|
||||
}
|
||||
45
server/chatserver/logic/queue.go
Normal file
45
server/chatserver/logic/queue.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package logic
|
||||
|
||||
type Item interface {
|
||||
}
|
||||
|
||||
// Item the type of the queue
|
||||
type ItemQueue struct {
|
||||
items []Item
|
||||
}
|
||||
|
||||
type ItemQueuer interface {
|
||||
New() ItemQueue
|
||||
Enqueue(t Item)
|
||||
Dequeue() *Item
|
||||
IsEmpty() bool
|
||||
Size() int
|
||||
}
|
||||
|
||||
// New creates a new ItemQueue
|
||||
func (s *ItemQueue) New() *ItemQueue {
|
||||
s.items = []Item{}
|
||||
return s
|
||||
}
|
||||
|
||||
// Enqueue adds an Item to the end of the queue
|
||||
func (s *ItemQueue) Enqueue(t Item) {
|
||||
s.items = append(s.items, t)
|
||||
}
|
||||
|
||||
// dequeue
|
||||
func (s *ItemQueue) Dequeue() *Item {
|
||||
item := s.items[0] // 先进先出
|
||||
s.items = s.items[1:len(s.items)]
|
||||
|
||||
return &item
|
||||
}
|
||||
|
||||
func (s *ItemQueue) IsEmpty() bool {
|
||||
return len(s.items) == 0
|
||||
}
|
||||
|
||||
// Size returns the number of Items in the queue
|
||||
func (s *ItemQueue) Size() int {
|
||||
return len(s.items)
|
||||
}
|
||||
Reference in New Issue
Block a user